##// END OF EJS Templates
Merged r11521 from trunk (#3371)....
Jean-Philippe Lang -
r11333:511099e9cac4
parent child
Show More
@@ -1,303 +1,305
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 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
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 182 if result.successful?
181 183 user = User.find_or_initialize_by_identity_url(identity_url)
182 184 if user.new_record?
183 185 # Self-registration off
184 186 (redirect_to(home_url); return) unless Setting.self_registration?
185 187
186 188 # Create on the fly
187 189 user.login = registration['nickname'] unless registration['nickname'].nil?
188 190 user.mail = registration['email'] unless registration['email'].nil?
189 191 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
190 192 user.random_password
191 193 user.register
192 194
193 195 case Setting.self_registration
194 196 when '1'
195 197 register_by_email_activation(user) do
196 198 onthefly_creation_failed(user)
197 199 end
198 200 when '3'
199 201 register_automatically(user) do
200 202 onthefly_creation_failed(user)
201 203 end
202 204 else
203 205 register_manually_by_administrator(user) do
204 206 onthefly_creation_failed(user)
205 207 end
206 208 end
207 209 else
208 210 # Existing record
209 211 if user.active?
210 212 successful_authentication(user)
211 213 else
212 214 account_pending
213 215 end
214 216 end
215 217 end
216 218 end
217 219 end
218 220
219 221 def successful_authentication(user)
220 222 logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
221 223 # Valid user
222 224 self.logged_user = user
223 225 # generate a key and set cookie if autologin
224 226 if params[:autologin] && Setting.autologin?
225 227 set_autologin_cookie(user)
226 228 end
227 229 call_hook(:controller_account_success_authentication_after, {:user => user })
228 230 redirect_back_or_default my_page_path
229 231 end
230 232
231 233 def set_autologin_cookie(user)
232 234 token = Token.create(:user => user, :action => 'autologin')
233 235 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
234 236 cookie_options = {
235 237 :value => token.value,
236 238 :expires => 1.year.from_now,
237 239 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
238 240 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
239 241 :httponly => true
240 242 }
241 243 cookies[cookie_name] = cookie_options
242 244 end
243 245
244 246 # Onthefly creation failed, display the registration form to fill/fix attributes
245 247 def onthefly_creation_failed(user, auth_source_options = { })
246 248 @user = user
247 249 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
248 250 render :action => 'register'
249 251 end
250 252
251 253 def invalid_credentials
252 254 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
253 255 flash.now[:error] = l(:notice_account_invalid_creditentials)
254 256 end
255 257
256 258 # Register a user for email activation.
257 259 #
258 260 # Pass a block for behavior when a user fails to save
259 261 def register_by_email_activation(user, &block)
260 262 token = Token.new(:user => user, :action => "register")
261 263 if user.save and token.save
262 264 Mailer.register(token).deliver
263 265 flash[:notice] = l(:notice_account_register_done)
264 266 redirect_to signin_path
265 267 else
266 268 yield if block_given?
267 269 end
268 270 end
269 271
270 272 # Automatically register a user
271 273 #
272 274 # Pass a block for behavior when a user fails to save
273 275 def register_automatically(user, &block)
274 276 # Automatic activation
275 277 user.activate
276 278 user.last_login_on = Time.now
277 279 if user.save
278 280 self.logged_user = user
279 281 flash[:notice] = l(:notice_account_activated)
280 282 redirect_to my_account_path
281 283 else
282 284 yield if block_given?
283 285 end
284 286 end
285 287
286 288 # Manual activation by the administrator
287 289 #
288 290 # Pass a block for behavior when a user fails to save
289 291 def register_manually_by_administrator(user, &block)
290 292 if user.save
291 293 # Sends an email to the administrators
292 294 Mailer.account_activation_request(user).deliver
293 295 account_pending
294 296 else
295 297 yield if block_given?
296 298 end
297 299 end
298 300
299 301 def account_pending
300 302 flash[:notice] = l(:notice_account_pending)
301 303 redirect_to signin_path
302 304 end
303 305 end
General Comments 0
You need to be logged in to leave comments. Login now