comparison app/controllers/account_controller.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents bb32da3bea34 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 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.
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 if User.current.logged?
29 redirect_to home_url
30 end
29 else 31 else
30 authenticate_user 32 authenticate_user
31 end 33 end
32 rescue AuthSourceException => e 34 rescue AuthSourceException => e
33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" 35 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
34 render_error :message => e.message 36 render_error :message => e.message
35 end 37 end
36 38
37 # Log out current user and redirect to welcome page 39 # Log out current user and redirect to welcome page
38 def logout 40 def logout
39 logout_user 41 if User.current.anonymous?
40 redirect_to home_url 42 redirect_to home_url
43 elsif request.post?
44 logout_user
45 redirect_to home_url
46 end
47 # display the logout form
41 end 48 end
42 49
43 # Lets user choose a new password 50 # Lets user choose a new password
44 def lost_password 51 def lost_password
45 redirect_to(home_url) && return unless Setting.lost_password? 52 (redirect_to(home_url); return) unless Setting.lost_password?
46 if params[:token] 53 if params[:token]
47 @token = Token.find_by_action_and_value("recovery", params[:token].to_s) 54 @token = Token.find_token("recovery", params[:token].to_s)
48 if @token.nil? || @token.expired? 55 if @token.nil? || @token.expired?
49 redirect_to home_url 56 redirect_to home_url
50 return 57 return
51 end 58 end
52 @user = @token.user 59 @user = @token.user
90 end 97 end
91 end 98 end
92 99
93 # User self-registration 100 # User self-registration
94 def register 101 def register
95 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] 102 (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
96 103
97 if request.get? 104 if request.get?
98 session[:auth_source_registration] = nil 105 session[:auth_source_registration] = nil
99 @user = User.new(:language => Setting.default_language) 106 @user = User.new(:language => current_language.to_s)
100 107
101 @ssamr_user_details = SsamrUserDetail.new 108 @ssamr_user_details = SsamrUserDetail.new
102 109
103 else 110 else
104 user_params = params[:user] || {} 111 user_params = params[:user] || {}
114 @user.auth_source_id = session[:auth_source_registration][:auth_source_id] 121 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
115 if @user.save 122 if @user.save
116 session[:auth_source_registration] = nil 123 session[:auth_source_registration] = nil
117 self.logged_user = @user 124 self.logged_user = @user
118 flash[:notice] = l(:notice_account_activated) 125 flash[:notice] = l(:notice_account_activated)
119 redirect_to :controller => 'my', :action => 'account' 126 redirect_to my_account_path
120 end 127 end
121 else 128 else
122 @user.login = params[:user][:login] 129 @user.login = params[:user][:login]
123 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? 130 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
124 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] 131 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
143 end 150 end
144 end 151 end
145 152
146 # Token based account activation 153 # Token based account activation
147 def activate 154 def activate
148 redirect_to(home_url) && return unless Setting.self_registration? && params[:token] 155 (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
149 token = Token.find_by_action_and_value('register', params[:token]) 156 token = Token.find_token('register', params[:token].to_s)
150 redirect_to(home_url) && return unless token and !token.expired? 157 (redirect_to(home_url); return) unless token and !token.expired?
151 user = token.user 158 user = token.user
152 redirect_to(home_url) && return unless user.registered? 159 (redirect_to(home_url); return) unless user.registered?
153 user.activate 160 user.activate
154 if user.save 161 if user.save
155 token.destroy 162 token.destroy
156 flash[:notice] = l(:notice_account_activated) 163 flash[:notice] = l(:notice_account_activated)
157 end 164 end
180 successful_authentication(user) 187 successful_authentication(user)
181 end 188 end
182 end 189 end
183 190
184 def open_id_authenticate(openid_url) 191 def open_id_authenticate(openid_url)
185 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| 192 back_url = signin_url(:autologin => params[:autologin])
193
194 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => back_url, :method => :post) do |result, identity_url, registration|
186 if result.successful? 195 if result.successful?
187 user = User.find_or_initialize_by_identity_url(identity_url) 196 user = User.find_or_initialize_by_identity_url(identity_url)
188 if user.new_record? 197 if user.new_record?
189 # Self-registration off 198 # Self-registration off
190 redirect_to(home_url) && return unless Setting.self_registration? 199 (redirect_to(home_url); return) unless Setting.self_registration?
191 200
192 # Create on the fly 201 # Create on the fly
193 user.login = registration['nickname'] unless registration['nickname'].nil? 202 user.login = registration['nickname'] unless registration['nickname'].nil?
194 user.mail = registration['email'] unless registration['email'].nil? 203 user.mail = registration['email'] unless registration['email'].nil?
195 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? 204 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
229 # generate a key and set cookie if autologin 238 # generate a key and set cookie if autologin
230 if params[:autologin] && Setting.autologin? 239 if params[:autologin] && Setting.autologin?
231 set_autologin_cookie(user) 240 set_autologin_cookie(user)
232 end 241 end
233 call_hook(:controller_account_success_authentication_after, {:user => user }) 242 call_hook(:controller_account_success_authentication_after, {:user => user })
234 redirect_back_or_default :controller => 'my', :action => 'page' 243 redirect_back_or_default my_page_path
235 end 244 end
236 245
237 def set_autologin_cookie(user) 246 def set_autologin_cookie(user)
238 token = Token.create(:user => user, :action => 'autologin') 247 token = Token.create(:user => user, :action => 'autologin')
239 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
240 cookie_options = { 248 cookie_options = {
241 :value => token.value, 249 :value => token.value,
242 :expires => 1.year.from_now, 250 :expires => 1.year.from_now,
243 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), 251 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
244 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), 252 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
245 :httponly => true 253 :httponly => true
246 } 254 }
247 cookies[cookie_name] = cookie_options 255 cookies[autologin_cookie_name] = cookie_options
248 end 256 end
249 257
250 # Onthefly creation failed, display the registration form to fill/fix attributes 258 # Onthefly creation failed, display the registration form to fill/fix attributes
251 def onthefly_creation_failed(user, auth_source_options = { }) 259 def onthefly_creation_failed(user, auth_source_options = { })
252 @user = user 260 @user = user
281 user.activate 289 user.activate
282 user.last_login_on = Time.now 290 user.last_login_on = Time.now
283 if user.save 291 if user.save
284 self.logged_user = user 292 self.logged_user = user
285 flash[:notice] = l(:notice_account_activated) 293 flash[:notice] = l(:notice_account_activated)
286 redirect_to :controller => 'my', :action => 'account' 294 redirect_to my_account_path
287 else 295 else
288 yield if block_given? 296 yield if block_given?
289 end 297 end
290 end 298 end
291 299