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