##// END OF EJS Templates
code layout cleanup AccountController#open_id_authenticate...
Toshi MARUYAMA -
r11309:5984adc3df46
parent child
Show More
@@ -1,304 +1,304
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 AccountController < ApplicationController
19 19 helper :custom_fields
20 20 include CustomFieldsHelper
21 21
22 22 # prevents login action to be filtered by check_if_login_required application scope filter
23 23 skip_before_filter :check_if_login_required
24 24
25 25 # Login request and validation
26 26 def login
27 27 if request.get?
28 28 if User.current.logged?
29 29 redirect_to home_url
30 30 end
31 31 else
32 32 authenticate_user
33 33 end
34 34 rescue AuthSourceException => e
35 35 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
36 36 render_error :message => e.message
37 37 end
38 38
39 39 # Log out current user and redirect to welcome page
40 40 def logout
41 41 if User.current.anonymous?
42 42 redirect_to home_url
43 43 elsif request.post?
44 44 logout_user
45 45 redirect_to home_url
46 46 end
47 47 # display the logout form
48 48 end
49 49
50 50 # Lets user choose a new password
51 51 def lost_password
52 52 (redirect_to(home_url); return) unless Setting.lost_password?
53 53 if params[:token]
54 54 @token = Token.find_token("recovery", params[:token].to_s)
55 55 if @token.nil? || @token.expired?
56 56 redirect_to home_url
57 57 return
58 58 end
59 59 @user = @token.user
60 60 unless @user && @user.active?
61 61 redirect_to home_url
62 62 return
63 63 end
64 64 if request.post?
65 65 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
66 66 if @user.save
67 67 @token.destroy
68 68 flash[:notice] = l(:notice_account_password_updated)
69 69 redirect_to signin_path
70 70 return
71 71 end
72 72 end
73 73 render :template => "account/password_recovery"
74 74 return
75 75 else
76 76 if request.post?
77 77 user = User.find_by_mail(params[:mail].to_s)
78 78 # user not found or not active
79 79 unless user && user.active?
80 80 flash.now[:error] = l(:notice_account_unknown_email)
81 81 return
82 82 end
83 83 # user cannot change its password
84 84 unless user.change_password_allowed?
85 85 flash.now[:error] = l(:notice_can_t_change_password)
86 86 return
87 87 end
88 88 # create a new token for password recovery
89 89 token = Token.new(:user => user, :action => "recovery")
90 90 if token.save
91 91 Mailer.lost_password(token).deliver
92 92 flash[:notice] = l(:notice_account_lost_email_sent)
93 93 redirect_to signin_path
94 94 return
95 95 end
96 96 end
97 97 end
98 98 end
99 99
100 100 # User self-registration
101 101 def register
102 102 (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
103 103 if request.get?
104 104 session[:auth_source_registration] = nil
105 105 @user = User.new(:language => current_language.to_s)
106 106 else
107 107 user_params = params[:user] || {}
108 108 @user = User.new
109 109 @user.safe_attributes = user_params
110 110 @user.admin = false
111 111 @user.register
112 112 if session[:auth_source_registration]
113 113 @user.activate
114 114 @user.login = session[:auth_source_registration][:login]
115 115 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
116 116 if @user.save
117 117 session[:auth_source_registration] = nil
118 118 self.logged_user = @user
119 119 flash[:notice] = l(:notice_account_activated)
120 120 redirect_to my_account_path
121 121 end
122 122 else
123 123 @user.login = params[:user][:login]
124 124 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
125 125 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
126 126 end
127 127
128 128 case Setting.self_registration
129 129 when '1'
130 130 register_by_email_activation(@user)
131 131 when '3'
132 132 register_automatically(@user)
133 133 else
134 134 register_manually_by_administrator(@user)
135 135 end
136 136 end
137 137 end
138 138 end
139 139
140 140 # Token based account activation
141 141 def activate
142 142 (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
143 143 token = Token.find_token('register', params[:token].to_s)
144 144 (redirect_to(home_url); return) unless token and !token.expired?
145 145 user = token.user
146 146 (redirect_to(home_url); return) unless user.registered?
147 147 user.activate
148 148 if user.save
149 149 token.destroy
150 150 flash[:notice] = l(:notice_account_activated)
151 151 end
152 152 redirect_to signin_path
153 153 end
154 154
155 155 private
156 156
157 157 def authenticate_user
158 158 if Setting.openid? && using_open_id?
159 159 open_id_authenticate(params[:openid_url])
160 160 else
161 161 password_authentication
162 162 end
163 163 end
164 164
165 165 def password_authentication
166 166 user = User.try_to_login(params[:username], params[:password])
167 167
168 168 if user.nil?
169 169 invalid_credentials
170 170 elsif user.new_record?
171 171 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
172 172 else
173 173 # Valid user
174 174 successful_authentication(user)
175 175 end
176 176 end
177 177
178 178 def open_id_authenticate(openid_url)
179 179 back_url = signin_url(:autologin => params[:autologin])
180
181 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => back_url, :method => :post) do |result, identity_url, registration|
180 authenticate_with_open_id(
181 openid_url, :required => [:nickname, :fullname, :email],
182 :return_to => back_url, :method => :post
183 ) do |result, identity_url, registration|
182 184 if result.successful?
183 185 user = User.find_or_initialize_by_identity_url(identity_url)
184 186 if user.new_record?
185 187 # Self-registration off
186 188 (redirect_to(home_url); return) unless Setting.self_registration?
187
188 189 # Create on the fly
189 190 user.login = registration['nickname'] unless registration['nickname'].nil?
190 191 user.mail = registration['email'] unless registration['email'].nil?
191 192 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
192 193 user.random_password
193 194 user.register
194
195 195 case Setting.self_registration
196 196 when '1'
197 197 register_by_email_activation(user) do
198 198 onthefly_creation_failed(user)
199 199 end
200 200 when '3'
201 201 register_automatically(user) do
202 202 onthefly_creation_failed(user)
203 203 end
204 204 else
205 205 register_manually_by_administrator(user) do
206 206 onthefly_creation_failed(user)
207 207 end
208 208 end
209 209 else
210 210 # Existing record
211 211 if user.active?
212 212 successful_authentication(user)
213 213 else
214 214 account_pending
215 215 end
216 216 end
217 217 end
218 218 end
219 219 end
220 220
221 221 def successful_authentication(user)
222 222 logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
223 223 # Valid user
224 224 self.logged_user = user
225 225 # generate a key and set cookie if autologin
226 226 if params[:autologin] && Setting.autologin?
227 227 set_autologin_cookie(user)
228 228 end
229 229 call_hook(:controller_account_success_authentication_after, {:user => user })
230 230 redirect_back_or_default my_page_path
231 231 end
232 232
233 233 def set_autologin_cookie(user)
234 234 token = Token.create(:user => user, :action => 'autologin')
235 235 cookie_options = {
236 236 :value => token.value,
237 237 :expires => 1.year.from_now,
238 238 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
239 239 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
240 240 :httponly => true
241 241 }
242 242 cookies[autologin_cookie_name] = cookie_options
243 243 end
244 244
245 245 # Onthefly creation failed, display the registration form to fill/fix attributes
246 246 def onthefly_creation_failed(user, auth_source_options = { })
247 247 @user = user
248 248 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
249 249 render :action => 'register'
250 250 end
251 251
252 252 def invalid_credentials
253 253 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
254 254 flash.now[:error] = l(:notice_account_invalid_creditentials)
255 255 end
256 256
257 257 # Register a user for email activation.
258 258 #
259 259 # Pass a block for behavior when a user fails to save
260 260 def register_by_email_activation(user, &block)
261 261 token = Token.new(:user => user, :action => "register")
262 262 if user.save and token.save
263 263 Mailer.register(token).deliver
264 264 flash[:notice] = l(:notice_account_register_done)
265 265 redirect_to signin_path
266 266 else
267 267 yield if block_given?
268 268 end
269 269 end
270 270
271 271 # Automatically register a user
272 272 #
273 273 # Pass a block for behavior when a user fails to save
274 274 def register_automatically(user, &block)
275 275 # Automatic activation
276 276 user.activate
277 277 user.last_login_on = Time.now
278 278 if user.save
279 279 self.logged_user = user
280 280 flash[:notice] = l(:notice_account_activated)
281 281 redirect_to my_account_path
282 282 else
283 283 yield if block_given?
284 284 end
285 285 end
286 286
287 287 # Manual activation by the administrator
288 288 #
289 289 # Pass a block for behavior when a user fails to save
290 290 def register_manually_by_administrator(user, &block)
291 291 if user.save
292 292 # Sends an email to the administrators
293 293 Mailer.account_activation_request(user).deliver
294 294 account_pending
295 295 else
296 296 yield if block_given?
297 297 end
298 298 end
299 299
300 300 def account_pending
301 301 flash[:notice] = l(:notice_account_pending)
302 302 redirect_to signin_path
303 303 end
304 304 end
General Comments 0
You need to be logged in to leave comments. Login now