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