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