##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r9759:a1d0acd632d7
parent child
Show More
@@ -1,282 +1,285
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class AccountController < ApplicationController
18 class AccountController < ApplicationController
19 helper :custom_fields
19 helper :custom_fields
20 include CustomFieldsHelper
20 include CustomFieldsHelper
21
21
22 # prevents login action to be filtered by check_if_login_required application scope filter
22 # prevents login action to be filtered by check_if_login_required application scope filter
23 skip_before_filter :check_if_login_required
23 skip_before_filter :check_if_login_required
24
24
25 # Login request and validation
25 # Login request and validation
26 def login
26 def login
27 if request.get?
27 if request.get?
28 logout_user
28 logout_user
29 else
29 else
30 authenticate_user
30 authenticate_user
31 end
31 end
32 rescue AuthSourceException => e
32 rescue AuthSourceException => e
33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
34 render_error :message => e.message
34 render_error :message => e.message
35 end
35 end
36
36
37 # Log out current user and redirect to welcome page
37 # Log out current user and redirect to welcome page
38 def logout
38 def logout
39 logout_user
39 logout_user
40 redirect_to home_url
40 redirect_to home_url
41 end
41 end
42
42
43 # Enable user to choose a new password
43 # Lets user choose a new password
44 def lost_password
44 def lost_password
45 redirect_to(home_url) && return unless Setting.lost_password?
45 redirect_to(home_url) && return unless Setting.lost_password?
46 if params[:token]
46 if params[:token]
47 @token = Token.find_by_action_and_value("recovery", params[:token])
47 @token = Token.find_by_action_and_value("recovery", params[:token].to_s)
48 redirect_to(home_url) && return unless @token and !@token.expired?
48 if @token.nil? || @token.expired?
49 redirect_to home_url
50 return
51 end
49 @user = @token.user
52 @user = @token.user
50 if request.post?
53 if request.post?
51 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
54 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
52 if @user.save
55 if @user.save
53 @token.destroy
56 @token.destroy
54 flash[:notice] = l(:notice_account_password_updated)
57 flash[:notice] = l(:notice_account_password_updated)
55 redirect_to :action => 'login'
58 redirect_to signin_path
56 return
59 return
57 end
60 end
58 end
61 end
59 render :template => "account/password_recovery"
62 render :template => "account/password_recovery"
60 return
63 return
61 else
64 else
62 if request.post?
65 if request.post?
63 user = User.find_by_mail(params[:mail])
66 user = User.find_by_mail(params[:mail])
64 # user not found in db
67 # user not found in db
65 (flash.now[:error] = l(:notice_account_unknown_email); return) unless user
68 (flash.now[:error] = l(:notice_account_unknown_email); return) unless user
66 # user uses an external authentification
69 # user uses an external authentification
67 (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id
70 (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id
68 # create a new token for password recovery
71 # create a new token for password recovery
69 token = Token.new(:user => user, :action => "recovery")
72 token = Token.new(:user => user, :action => "recovery")
70 if token.save
73 if token.save
71 Mailer.lost_password(token).deliver
74 Mailer.lost_password(token).deliver
72 flash[:notice] = l(:notice_account_lost_email_sent)
75 flash[:notice] = l(:notice_account_lost_email_sent)
73 redirect_to signin_path
76 redirect_to signin_path
74 return
77 return
75 end
78 end
76 end
79 end
77 end
80 end
78 end
81 end
79
82
80 # User self-registration
83 # User self-registration
81 def register
84 def register
82 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
85 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
83 if request.get?
86 if request.get?
84 session[:auth_source_registration] = nil
87 session[:auth_source_registration] = nil
85 @user = User.new(:language => Setting.default_language)
88 @user = User.new(:language => Setting.default_language)
86 else
89 else
87 user_params = params[:user] || {}
90 user_params = params[:user] || {}
88 @user = User.new
91 @user = User.new
89 @user.safe_attributes = user_params
92 @user.safe_attributes = user_params
90 @user.admin = false
93 @user.admin = false
91 @user.register
94 @user.register
92 if session[:auth_source_registration]
95 if session[:auth_source_registration]
93 @user.activate
96 @user.activate
94 @user.login = session[:auth_source_registration][:login]
97 @user.login = session[:auth_source_registration][:login]
95 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
98 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
96 if @user.save
99 if @user.save
97 session[:auth_source_registration] = nil
100 session[:auth_source_registration] = nil
98 self.logged_user = @user
101 self.logged_user = @user
99 flash[:notice] = l(:notice_account_activated)
102 flash[:notice] = l(:notice_account_activated)
100 redirect_to :controller => 'my', :action => 'account'
103 redirect_to :controller => 'my', :action => 'account'
101 end
104 end
102 else
105 else
103 @user.login = params[:user][:login]
106 @user.login = params[:user][:login]
104 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
107 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
105 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
108 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
106 end
109 end
107
110
108 case Setting.self_registration
111 case Setting.self_registration
109 when '1'
112 when '1'
110 register_by_email_activation(@user)
113 register_by_email_activation(@user)
111 when '3'
114 when '3'
112 register_automatically(@user)
115 register_automatically(@user)
113 else
116 else
114 register_manually_by_administrator(@user)
117 register_manually_by_administrator(@user)
115 end
118 end
116 end
119 end
117 end
120 end
118 end
121 end
119
122
120 # Token based account activation
123 # Token based account activation
121 def activate
124 def activate
122 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
125 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
123 token = Token.find_by_action_and_value('register', params[:token])
126 token = Token.find_by_action_and_value('register', params[:token])
124 redirect_to(home_url) && return unless token and !token.expired?
127 redirect_to(home_url) && return unless token and !token.expired?
125 user = token.user
128 user = token.user
126 redirect_to(home_url) && return unless user.registered?
129 redirect_to(home_url) && return unless user.registered?
127 user.activate
130 user.activate
128 if user.save
131 if user.save
129 token.destroy
132 token.destroy
130 flash[:notice] = l(:notice_account_activated)
133 flash[:notice] = l(:notice_account_activated)
131 end
134 end
132 redirect_to signin_path
135 redirect_to signin_path
133 end
136 end
134
137
135 private
138 private
136
139
137 def authenticate_user
140 def authenticate_user
138 if Setting.openid? && using_open_id?
141 if Setting.openid? && using_open_id?
139 open_id_authenticate(params[:openid_url])
142 open_id_authenticate(params[:openid_url])
140 else
143 else
141 password_authentication
144 password_authentication
142 end
145 end
143 end
146 end
144
147
145 def password_authentication
148 def password_authentication
146 user = User.try_to_login(params[:username], params[:password])
149 user = User.try_to_login(params[:username], params[:password])
147
150
148 if user.nil?
151 if user.nil?
149 invalid_credentials
152 invalid_credentials
150 elsif user.new_record?
153 elsif user.new_record?
151 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
154 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
152 else
155 else
153 # Valid user
156 # Valid user
154 successful_authentication(user)
157 successful_authentication(user)
155 end
158 end
156 end
159 end
157
160
158 def open_id_authenticate(openid_url)
161 def open_id_authenticate(openid_url)
159 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
162 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
160 if result.successful?
163 if result.successful?
161 user = User.find_or_initialize_by_identity_url(identity_url)
164 user = User.find_or_initialize_by_identity_url(identity_url)
162 if user.new_record?
165 if user.new_record?
163 # Self-registration off
166 # Self-registration off
164 redirect_to(home_url) && return unless Setting.self_registration?
167 redirect_to(home_url) && return unless Setting.self_registration?
165
168
166 # Create on the fly
169 # Create on the fly
167 user.login = registration['nickname'] unless registration['nickname'].nil?
170 user.login = registration['nickname'] unless registration['nickname'].nil?
168 user.mail = registration['email'] unless registration['email'].nil?
171 user.mail = registration['email'] unless registration['email'].nil?
169 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
172 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
170 user.random_password
173 user.random_password
171 user.register
174 user.register
172
175
173 case Setting.self_registration
176 case Setting.self_registration
174 when '1'
177 when '1'
175 register_by_email_activation(user) do
178 register_by_email_activation(user) do
176 onthefly_creation_failed(user)
179 onthefly_creation_failed(user)
177 end
180 end
178 when '3'
181 when '3'
179 register_automatically(user) do
182 register_automatically(user) do
180 onthefly_creation_failed(user)
183 onthefly_creation_failed(user)
181 end
184 end
182 else
185 else
183 register_manually_by_administrator(user) do
186 register_manually_by_administrator(user) do
184 onthefly_creation_failed(user)
187 onthefly_creation_failed(user)
185 end
188 end
186 end
189 end
187 else
190 else
188 # Existing record
191 # Existing record
189 if user.active?
192 if user.active?
190 successful_authentication(user)
193 successful_authentication(user)
191 else
194 else
192 account_pending
195 account_pending
193 end
196 end
194 end
197 end
195 end
198 end
196 end
199 end
197 end
200 end
198
201
199 def successful_authentication(user)
202 def successful_authentication(user)
200 # Valid user
203 # Valid user
201 self.logged_user = user
204 self.logged_user = user
202 # generate a key and set cookie if autologin
205 # generate a key and set cookie if autologin
203 if params[:autologin] && Setting.autologin?
206 if params[:autologin] && Setting.autologin?
204 set_autologin_cookie(user)
207 set_autologin_cookie(user)
205 end
208 end
206 call_hook(:controller_account_success_authentication_after, {:user => user })
209 call_hook(:controller_account_success_authentication_after, {:user => user })
207 redirect_back_or_default :controller => 'my', :action => 'page'
210 redirect_back_or_default :controller => 'my', :action => 'page'
208 end
211 end
209
212
210 def set_autologin_cookie(user)
213 def set_autologin_cookie(user)
211 token = Token.create(:user => user, :action => 'autologin')
214 token = Token.create(:user => user, :action => 'autologin')
212 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
215 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
213 cookie_options = {
216 cookie_options = {
214 :value => token.value,
217 :value => token.value,
215 :expires => 1.year.from_now,
218 :expires => 1.year.from_now,
216 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
219 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
217 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
220 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
218 :httponly => true
221 :httponly => true
219 }
222 }
220 cookies[cookie_name] = cookie_options
223 cookies[cookie_name] = cookie_options
221 end
224 end
222
225
223 # Onthefly creation failed, display the registration form to fill/fix attributes
226 # Onthefly creation failed, display the registration form to fill/fix attributes
224 def onthefly_creation_failed(user, auth_source_options = { })
227 def onthefly_creation_failed(user, auth_source_options = { })
225 @user = user
228 @user = user
226 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
229 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
227 render register_path
230 render register_path
228 end
231 end
229
232
230 def invalid_credentials
233 def invalid_credentials
231 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
234 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
232 flash.now[:error] = l(:notice_account_invalid_creditentials)
235 flash.now[:error] = l(:notice_account_invalid_creditentials)
233 end
236 end
234
237
235 # Register a user for email activation.
238 # Register a user for email activation.
236 #
239 #
237 # Pass a block for behavior when a user fails to save
240 # Pass a block for behavior when a user fails to save
238 def register_by_email_activation(user, &block)
241 def register_by_email_activation(user, &block)
239 token = Token.new(:user => user, :action => "register")
242 token = Token.new(:user => user, :action => "register")
240 if user.save and token.save
243 if user.save and token.save
241 Mailer.register(token).deliver
244 Mailer.register(token).deliver
242 flash[:notice] = l(:notice_account_register_done)
245 flash[:notice] = l(:notice_account_register_done)
243 redirect_to signin_path
246 redirect_to signin_path
244 else
247 else
245 yield if block_given?
248 yield if block_given?
246 end
249 end
247 end
250 end
248
251
249 # Automatically register a user
252 # Automatically register a user
250 #
253 #
251 # Pass a block for behavior when a user fails to save
254 # Pass a block for behavior when a user fails to save
252 def register_automatically(user, &block)
255 def register_automatically(user, &block)
253 # Automatic activation
256 # Automatic activation
254 user.activate
257 user.activate
255 user.last_login_on = Time.now
258 user.last_login_on = Time.now
256 if user.save
259 if user.save
257 self.logged_user = user
260 self.logged_user = user
258 flash[:notice] = l(:notice_account_activated)
261 flash[:notice] = l(:notice_account_activated)
259 redirect_to :controller => 'my', :action => 'account'
262 redirect_to :controller => 'my', :action => 'account'
260 else
263 else
261 yield if block_given?
264 yield if block_given?
262 end
265 end
263 end
266 end
264
267
265 # Manual activation by the administrator
268 # Manual activation by the administrator
266 #
269 #
267 # Pass a block for behavior when a user fails to save
270 # Pass a block for behavior when a user fails to save
268 def register_manually_by_administrator(user, &block)
271 def register_manually_by_administrator(user, &block)
269 if user.save
272 if user.save
270 # Sends an email to the administrators
273 # Sends an email to the administrators
271 Mailer.account_activation_request(user).deliver
274 Mailer.account_activation_request(user).deliver
272 account_pending
275 account_pending
273 else
276 else
274 yield if block_given?
277 yield if block_given?
275 end
278 end
276 end
279 end
277
280
278 def account_pending
281 def account_pending
279 flash[:notice] = l(:notice_account_pending)
282 flash[:notice] = l(:notice_account_pending)
280 redirect_to signin_path
283 redirect_to signin_path
281 end
284 end
282 end
285 end
General Comments 0
You need to be logged in to leave comments. Login now