##// END OF EJS Templates
Fixed: autologin broken....
Jean-Philippe Lang -
r672:1187ad96ac49
parent child
Show More
@@ -1,170 +1,170
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class ApplicationController < ActionController::Base
19 19 before_filter :user_setup, :check_if_login_required, :set_localization
20 20 filter_parameter_logging :password
21 21
22 22 REDMINE_SUPPORTED_SCM.each do |scm|
23 23 require_dependency "repository/#{scm.underscore}"
24 24 end
25 25
26 26 def logged_in_user
27 27 User.current.logged? ? User.current : nil
28 28 end
29 29
30 30 def current_role
31 31 @current_role ||= User.current.role_for_project(@project)
32 32 end
33 33
34 34 def user_setup
35 35 if session[:user_id]
36 36 # existing session
37 37 User.current = User.find(session[:user_id])
38 38 elsif cookies[:autologin] && Setting.autologin?
39 39 # auto-login feature
40 User.current = User.find_by_autologin_key(autologin_key)
40 User.current = User.find_by_autologin_key(cookies[:autologin])
41 41 elsif params[:key] && accept_key_auth_actions.include?(params[:action])
42 42 # RSS key authentication
43 43 User.current = User.find_by_rss_key(params[:key])
44 44 else
45 45 User.current = User.anonymous
46 46 end
47 47 end
48 48
49 49 # check if login is globally required to access the application
50 50 def check_if_login_required
51 51 # no check needed if user is already logged in
52 52 return true if User.current.logged?
53 53 require_login if Setting.login_required?
54 54 end
55 55
56 56 def set_localization
57 57 lang = begin
58 58 if !User.current.language.blank? and GLoc.valid_languages.include? User.current.language.to_sym
59 59 User.current.language
60 60 elsif request.env['HTTP_ACCEPT_LANGUAGE']
61 61 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.split('-').first
62 62 if accept_lang and !accept_lang.empty? and GLoc.valid_languages.include? accept_lang.to_sym
63 63 accept_lang
64 64 end
65 65 end
66 66 rescue
67 67 nil
68 68 end || Setting.default_language
69 69 set_language_if_valid(lang)
70 70 end
71 71
72 72 def require_login
73 73 if !User.current.logged?
74 74 store_location
75 75 redirect_to :controller => "account", :action => "login"
76 76 return false
77 77 end
78 78 true
79 79 end
80 80
81 81 def require_admin
82 82 return unless require_login
83 83 if !User.current.admin?
84 84 render_403
85 85 return false
86 86 end
87 87 true
88 88 end
89 89
90 90 # Authorize the user for the requested action
91 91 def authorize(ctrl = params[:controller], action = params[:action])
92 92 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project)
93 93 allowed ? true : (User.current.logged? ? render_403 : require_login)
94 94 end
95 95
96 96 # make sure that the user is a member of the project (or admin) if project is private
97 97 # used as a before_filter for actions that do not require any particular permission on the project
98 98 def check_project_privacy
99 99 unless @project.active?
100 100 @project = nil
101 101 render_404
102 102 return false
103 103 end
104 104 return true if @project.is_public? || User.current.member_of?(@project) || User.current.admin?
105 105 User.current.logged? ? render_403 : require_login
106 106 end
107 107
108 108 # store current uri in session.
109 109 # return to this location by calling redirect_back_or_default
110 110 def store_location
111 111 session[:return_to_params] = params
112 112 end
113 113
114 114 # move to the last store_location call or to the passed default one
115 115 def redirect_back_or_default(default)
116 116 if session[:return_to_params].nil?
117 117 redirect_to default
118 118 else
119 119 redirect_to session[:return_to_params]
120 120 session[:return_to_params] = nil
121 121 end
122 122 end
123 123
124 124 def render_403
125 125 @html_title = "403"
126 126 @project = nil
127 127 render :template => "common/403", :layout => true, :status => 403
128 128 return false
129 129 end
130 130
131 131 def render_404
132 132 @html_title = "404"
133 133 render :template => "common/404", :layout => true, :status => 404
134 134 return false
135 135 end
136 136
137 137 def render_feed(items, options={})
138 138 @items = items.sort {|x,y| y.event_datetime <=> x.event_datetime }
139 139 @title = options[:title] || Setting.app_title
140 140 render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml'
141 141 end
142 142
143 143 def self.accept_key_auth(*actions)
144 144 actions = actions.flatten.map(&:to_s)
145 145 write_inheritable_attribute('accept_key_auth_actions', actions)
146 146 end
147 147
148 148 def accept_key_auth_actions
149 149 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
150 150 end
151 151
152 152 # qvalues http header parser
153 153 # code taken from webrick
154 154 def parse_qvalues(value)
155 155 tmp = []
156 156 if value
157 157 parts = value.split(/,\s*/)
158 158 parts.each {|part|
159 159 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
160 160 val = m[1]
161 161 q = (m[2] or 1).to_f
162 162 tmp.push([val, q])
163 163 end
164 164 }
165 165 tmp = tmp.sort_by{|val, q| -q}
166 166 tmp.collect!{|val, q| val}
167 167 end
168 168 return tmp
169 169 end
170 170 end
General Comments 0
You need to be logged in to leave comments. Login now