Mercurial > hg > soundsoftware-site
comparison app/controllers/account_controller.rb @ 1338:25603efa57b5
Merge from live branch
author | Chris Cannam |
---|---|
date | Thu, 20 Jun 2013 13:14:14 +0100 |
parents | bb32da3bea34 |
children | 4f746d8966dd 51364c0cd58f |
comparison
equal
deleted
inserted
replaced
1209:1b1138f6f55e | 1338:25603efa57b5 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 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. |
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 | |
33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" | |
34 render_error :message => e.message | |
32 end | 35 end |
33 | 36 |
34 # Log out current user and redirect to welcome page | 37 # Log out current user and redirect to welcome page |
35 def logout | 38 def logout |
36 logout_user | 39 logout_user |
37 redirect_to home_url | 40 redirect_to home_url |
38 end | 41 end |
39 | 42 |
40 # Enable user to choose a new password | 43 # Lets user choose a new password |
41 def lost_password | 44 def lost_password |
42 redirect_to(home_url) && return unless Setting.lost_password? | 45 redirect_to(home_url) && return unless Setting.lost_password? |
43 if params[:token] | 46 if params[:token] |
44 @token = Token.find_by_action_and_value("recovery", params[:token]) | 47 @token = Token.find_by_action_and_value("recovery", params[:token].to_s) |
45 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 | |
46 @user = @token.user | 52 @user = @token.user |
53 unless @user && @user.active? | |
54 redirect_to home_url | |
55 return | |
56 end | |
47 if request.post? | 57 if request.post? |
48 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | 58 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
49 if @user.save | 59 if @user.save |
50 @token.destroy | 60 @token.destroy |
51 flash[:notice] = l(:notice_account_password_updated) | 61 flash[:notice] = l(:notice_account_password_updated) |
52 redirect_to :action => 'login' | 62 redirect_to signin_path |
53 return | 63 return |
54 end | 64 end |
55 end | 65 end |
56 render :template => "account/password_recovery" | 66 render :template => "account/password_recovery" |
57 return | 67 return |
58 else | 68 else |
59 if request.post? | 69 if request.post? |
60 user = User.find_by_mail(params[:mail]) | 70 user = User.find_by_mail(params[:mail].to_s) |
61 # user not found in db | 71 # user not found or not active |
62 (flash.now[:error] = l(:notice_account_unknown_email); return) unless user | 72 unless user && user.active? |
63 # user uses an external authentification | 73 flash.now[:error] = l(:notice_account_unknown_email) |
64 (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id | 74 return |
75 end | |
76 # user cannot change its password | |
77 unless user.change_password_allowed? | |
78 flash.now[:error] = l(:notice_can_t_change_password) | |
79 return | |
80 end | |
65 # create a new token for password recovery | 81 # create a new token for password recovery |
66 token = Token.new(:user => user, :action => "recovery") | 82 token = Token.new(:user => user, :action => "recovery") |
67 if token.save | 83 if token.save |
68 Mailer.deliver_lost_password(token) | 84 Mailer.lost_password(token).deliver |
69 flash[:notice] = l(:notice_account_lost_email_sent) | 85 flash[:notice] = l(:notice_account_lost_email_sent) |
70 redirect_to :action => 'login' | 86 redirect_to signin_path |
71 return | 87 return |
72 end | 88 end |
73 end | 89 end |
74 end | 90 end |
75 end | 91 end |
83 @user = User.new(:language => Setting.default_language) | 99 @user = User.new(:language => Setting.default_language) |
84 | 100 |
85 @ssamr_user_details = SsamrUserDetail.new | 101 @ssamr_user_details = SsamrUserDetail.new |
86 | 102 |
87 else | 103 else |
88 @user = User.new(params[:user]) | 104 user_params = params[:user] || {} |
105 @user = User.new | |
106 @user.safe_attributes = user_params | |
89 @user.admin = false | 107 @user.admin = false |
90 | 108 |
91 @user.register | 109 @user.register |
92 | 110 |
93 if session[:auth_source_registration] | 111 if session[:auth_source_registration] |
100 flash[:notice] = l(:notice_account_activated) | 118 flash[:notice] = l(:notice_account_activated) |
101 redirect_to :controller => 'my', :action => 'account' | 119 redirect_to :controller => 'my', :action => 'account' |
102 end | 120 end |
103 else | 121 else |
104 @user.login = params[:user][:login] | 122 @user.login = params[:user][:login] |
105 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] | 123 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] | |
125 end | |
106 | 126 |
107 @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details]) | 127 @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details]) |
108 | 128 |
109 # associates the 2 objects | 129 # associates the 2 objects |
110 @user.ssamr_user_detail = @ssamr_user_details | 130 @user.ssamr_user_detail = @ssamr_user_details |
133 user.activate | 153 user.activate |
134 if user.save | 154 if user.save |
135 token.destroy | 155 token.destroy |
136 flash[:notice] = l(:notice_account_activated) | 156 flash[:notice] = l(:notice_account_activated) |
137 end | 157 end |
138 redirect_to :action => 'login' | 158 redirect_to signin_path |
139 end | 159 end |
140 | 160 |
141 private | 161 private |
142 | |
143 def logout_user | |
144 if User.current.logged? | |
145 cookies.delete :autologin | |
146 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) | |
147 self.logged_user = nil | |
148 end | |
149 end | |
150 | 162 |
151 def authenticate_user | 163 def authenticate_user |
152 if Setting.openid? && using_open_id? | 164 if Setting.openid? && using_open_id? |
153 open_id_authenticate(params[:openid_url]) | 165 open_id_authenticate(params[:openid_url]) |
154 else | 166 else |
168 successful_authentication(user) | 180 successful_authentication(user) |
169 end | 181 end |
170 end | 182 end |
171 | 183 |
172 def open_id_authenticate(openid_url) | 184 def open_id_authenticate(openid_url) |
173 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration| | 185 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| |
174 if result.successful? | 186 if result.successful? |
175 user = User.find_or_initialize_by_identity_url(identity_url) | 187 user = User.find_or_initialize_by_identity_url(identity_url) |
176 if user.new_record? | 188 if user.new_record? |
177 # Self-registration off | 189 # Self-registration off |
178 redirect_to(home_url) && return unless Setting.self_registration? | 190 redirect_to(home_url) && return unless Setting.self_registration? |
209 end | 221 end |
210 end | 222 end |
211 end | 223 end |
212 | 224 |
213 def successful_authentication(user) | 225 def successful_authentication(user) |
226 logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" | |
214 # Valid user | 227 # Valid user |
215 self.logged_user = user | 228 self.logged_user = user |
216 # generate a key and set cookie if autologin | 229 # generate a key and set cookie if autologin |
217 if params[:autologin] && Setting.autologin? | 230 if params[:autologin] && Setting.autologin? |
218 set_autologin_cookie(user) | 231 set_autologin_cookie(user) |
250 # | 263 # |
251 # Pass a block for behavior when a user fails to save | 264 # Pass a block for behavior when a user fails to save |
252 def register_by_email_activation(user, &block) | 265 def register_by_email_activation(user, &block) |
253 token = Token.new(:user => user, :action => "register") | 266 token = Token.new(:user => user, :action => "register") |
254 if user.save and token.save | 267 if user.save and token.save |
255 Mailer.deliver_register(token) | 268 Mailer.register(token).deliver |
256 flash[:notice] = l(:notice_account_register_done) | 269 flash[:notice] = l(:notice_account_register_done) |
257 redirect_to :action => 'login' | 270 redirect_to signin_path |
258 else | 271 else |
259 yield if block_given? | 272 yield if block_given? |
260 end | 273 end |
261 end | 274 end |
262 | 275 |
283 if user.save | 296 if user.save |
284 | 297 |
285 @ssamr_user_details.save! | 298 @ssamr_user_details.save! |
286 | 299 |
287 # Sends an email to the administrators | 300 # Sends an email to the administrators |
288 Mailer.deliver_account_activation_request(user) | 301 Mailer.account_activation_request(user).deliver |
289 account_pending | 302 account_pending |
290 else | 303 else |
291 yield if block_given? | 304 yield if block_given? |
292 end | 305 end |
293 end | 306 end |
294 | 307 |
295 def account_pending | 308 def account_pending |
296 flash[:notice] = l(:notice_account_pending) | 309 flash[:notice] = l(:notice_account_pending) |
297 redirect_to :action => 'login' | 310 redirect_to signin_path |
298 end | 311 end |
299 end | 312 end |