@@ -1,270 +1,272 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class AccountController < ApplicationController |
|
18 | class AccountController < ApplicationController | |
19 | helper :custom_fields |
|
19 | helper :custom_fields | |
20 | include CustomFieldsHelper |
|
20 | include CustomFieldsHelper | |
21 |
|
21 | |||
22 | # prevents login action to be filtered by check_if_login_required application scope filter |
|
22 | # prevents login action to be filtered by check_if_login_required application scope filter | |
23 | skip_before_filter :check_if_login_required |
|
23 | skip_before_filter :check_if_login_required | |
24 |
|
24 | |||
25 | # Login request and validation |
|
25 | # Login request and validation | |
26 | def login |
|
26 | def login | |
27 | if request.get? |
|
27 | if request.get? | |
28 | logout_user |
|
28 | logout_user | |
29 | else |
|
29 | else | |
30 | authenticate_user |
|
30 | authenticate_user | |
31 | end |
|
31 | end | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | # Log out current user and redirect to welcome page |
|
34 | # Log out current user and redirect to welcome page | |
35 | def logout |
|
35 | def logout | |
36 | logout_user |
|
36 | logout_user | |
37 | redirect_to home_url |
|
37 | redirect_to home_url | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | # Enable user to choose a new password |
|
40 | # Enable user to choose a new password | |
41 | def lost_password |
|
41 | def lost_password | |
42 | redirect_to(home_url) && return unless Setting.lost_password? |
|
42 | redirect_to(home_url) && return unless Setting.lost_password? | |
43 | if params[:token] |
|
43 | if params[:token] | |
44 | @token = Token.find_by_action_and_value("recovery", params[:token]) |
|
44 | @token = Token.find_by_action_and_value("recovery", params[:token]) | |
45 | redirect_to(home_url) && return unless @token and !@token.expired? |
|
45 | redirect_to(home_url) && return unless @token and !@token.expired? | |
46 | @user = @token.user |
|
46 | @user = @token.user | |
47 | if request.post? |
|
47 | if request.post? | |
48 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
48 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
49 | if @user.save |
|
49 | if @user.save | |
50 | @token.destroy |
|
50 | @token.destroy | |
51 | flash[:notice] = l(:notice_account_password_updated) |
|
51 | flash[:notice] = l(:notice_account_password_updated) | |
52 | redirect_to :action => 'login' |
|
52 | redirect_to :action => 'login' | |
53 | return |
|
53 | return | |
54 | end |
|
54 | end | |
55 | end |
|
55 | end | |
56 | render :template => "account/password_recovery" |
|
56 | render :template => "account/password_recovery" | |
57 | return |
|
57 | return | |
58 | else |
|
58 | else | |
59 | if request.post? |
|
59 | if request.post? | |
60 | user = User.find_by_mail(params[:mail]) |
|
60 | user = User.find_by_mail(params[:mail]) | |
61 | # user not found in db |
|
61 | # user not found in db | |
62 | (flash.now[:error] = l(:notice_account_unknown_email); return) unless user |
|
62 | (flash.now[:error] = l(:notice_account_unknown_email); return) unless user | |
63 | # user uses an external authentification |
|
63 | # user uses an external authentification | |
64 | (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id |
|
64 | (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id | |
65 | # create a new token for password recovery |
|
65 | # create a new token for password recovery | |
66 | token = Token.new(:user => user, :action => "recovery") |
|
66 | token = Token.new(:user => user, :action => "recovery") | |
67 | if token.save |
|
67 | if token.save | |
68 | Mailer.deliver_lost_password(token) |
|
68 | Mailer.deliver_lost_password(token) | |
69 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
69 | flash[:notice] = l(:notice_account_lost_email_sent) | |
70 | redirect_to :action => 'login' |
|
70 | redirect_to :action => 'login' | |
71 | return |
|
71 | return | |
72 | end |
|
72 | end | |
73 | end |
|
73 | end | |
74 | end |
|
74 | end | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | # User self-registration |
|
77 | # User self-registration | |
78 | def register |
|
78 | def register | |
79 | redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] |
|
79 | redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] | |
80 | if request.get? |
|
80 | if request.get? | |
81 | session[:auth_source_registration] = nil |
|
81 | session[:auth_source_registration] = nil | |
82 | @user = User.new(:language => Setting.default_language) |
|
82 | @user = User.new(:language => Setting.default_language) | |
83 | else |
|
83 | else | |
84 | @user = User.new(params[:user]) |
|
84 | @user = User.new(params[:user]) | |
85 | @user.admin = false |
|
85 | @user.admin = false | |
86 | @user.status = User::STATUS_REGISTERED |
|
86 | @user.status = User::STATUS_REGISTERED | |
87 | if session[:auth_source_registration] |
|
87 | if session[:auth_source_registration] | |
88 | @user.status = User::STATUS_ACTIVE |
|
88 | @user.status = User::STATUS_ACTIVE | |
89 | @user.login = session[:auth_source_registration][:login] |
|
89 | @user.login = session[:auth_source_registration][:login] | |
90 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
90 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] | |
91 | if @user.save |
|
91 | if @user.save | |
92 | session[:auth_source_registration] = nil |
|
92 | session[:auth_source_registration] = nil | |
93 | self.logged_user = @user |
|
93 | self.logged_user = @user | |
94 | flash[:notice] = l(:notice_account_activated) |
|
94 | flash[:notice] = l(:notice_account_activated) | |
95 | redirect_to :controller => 'my', :action => 'account' |
|
95 | redirect_to :controller => 'my', :action => 'account' | |
96 | end |
|
96 | end | |
97 | else |
|
97 | else | |
98 | @user.login = params[:user][:login] |
|
98 | @user.login = params[:user][:login] | |
99 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
|
99 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] | |
100 |
|
100 | |||
101 | case Setting.self_registration |
|
101 | case Setting.self_registration | |
102 | when '1' |
|
102 | when '1' | |
103 | register_by_email_activation(@user) |
|
103 | register_by_email_activation(@user) | |
104 | when '3' |
|
104 | when '3' | |
105 | register_automatically(@user) |
|
105 | register_automatically(@user) | |
106 | else |
|
106 | else | |
107 | register_manually_by_administrator(@user) |
|
107 | register_manually_by_administrator(@user) | |
108 | end |
|
108 | end | |
109 | end |
|
109 | end | |
110 | end |
|
110 | end | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
113 | # Token based account activation |
|
113 | # Token based account activation | |
114 | def activate |
|
114 | def activate | |
115 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] |
|
115 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] | |
116 | token = Token.find_by_action_and_value('register', params[:token]) |
|
116 | token = Token.find_by_action_and_value('register', params[:token]) | |
117 | redirect_to(home_url) && return unless token and !token.expired? |
|
117 | redirect_to(home_url) && return unless token and !token.expired? | |
118 | user = token.user |
|
118 | user = token.user | |
119 | redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED |
|
119 | redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED | |
120 | user.status = User::STATUS_ACTIVE |
|
120 | user.status = User::STATUS_ACTIVE | |
121 | if user.save |
|
121 | if user.save | |
122 | token.destroy |
|
122 | token.destroy | |
123 | flash[:notice] = l(:notice_account_activated) |
|
123 | flash[:notice] = l(:notice_account_activated) | |
124 | end |
|
124 | end | |
125 | redirect_to :action => 'login' |
|
125 | redirect_to :action => 'login' | |
126 | end |
|
126 | end | |
127 |
|
127 | |||
128 | private |
|
128 | private | |
129 |
|
129 | |||
130 | def logout_user |
|
130 | def logout_user | |
|
131 | if User.current.logged? | |||
131 | cookies.delete :autologin |
|
132 | cookies.delete :autologin | |
132 |
Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) |
|
133 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) | |
133 | self.logged_user = nil |
|
134 | self.logged_user = nil | |
134 | end |
|
135 | end | |
|
136 | end | |||
135 |
|
137 | |||
136 | def authenticate_user |
|
138 | def authenticate_user | |
137 | if Setting.openid? && using_open_id? |
|
139 | if Setting.openid? && using_open_id? | |
138 | open_id_authenticate(params[:openid_url]) |
|
140 | open_id_authenticate(params[:openid_url]) | |
139 | else |
|
141 | else | |
140 | password_authentication |
|
142 | password_authentication | |
141 | end |
|
143 | end | |
142 | end |
|
144 | end | |
143 |
|
145 | |||
144 | def password_authentication |
|
146 | def password_authentication | |
145 | user = User.try_to_login(params[:username], params[:password]) |
|
147 | user = User.try_to_login(params[:username], params[:password]) | |
146 |
|
148 | |||
147 | if user.nil? |
|
149 | if user.nil? | |
148 | invalid_credentials |
|
150 | invalid_credentials | |
149 | elsif user.new_record? |
|
151 | elsif user.new_record? | |
150 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) |
|
152 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) | |
151 | else |
|
153 | else | |
152 | # Valid user |
|
154 | # Valid user | |
153 | successful_authentication(user) |
|
155 | successful_authentication(user) | |
154 | end |
|
156 | end | |
155 | end |
|
157 | end | |
156 |
|
158 | |||
157 |
|
159 | |||
158 | def open_id_authenticate(openid_url) |
|
160 | def open_id_authenticate(openid_url) | |
159 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration| |
|
161 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration| | |
160 | if result.successful? |
|
162 | if result.successful? | |
161 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
163 | user = User.find_or_initialize_by_identity_url(identity_url) | |
162 | if user.new_record? |
|
164 | if user.new_record? | |
163 | # Self-registration off |
|
165 | # Self-registration off | |
164 | redirect_to(home_url) && return unless Setting.self_registration? |
|
166 | redirect_to(home_url) && return unless Setting.self_registration? | |
165 |
|
167 | |||
166 | # Create on the fly |
|
168 | # Create on the fly | |
167 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
169 | user.login = registration['nickname'] unless registration['nickname'].nil? | |
168 | user.mail = registration['email'] unless registration['email'].nil? |
|
170 | user.mail = registration['email'] unless registration['email'].nil? | |
169 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
171 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? | |
170 | user.random_password |
|
172 | user.random_password | |
171 | user.status = User::STATUS_REGISTERED |
|
173 | user.status = User::STATUS_REGISTERED | |
172 |
|
174 | |||
173 | case Setting.self_registration |
|
175 | case Setting.self_registration | |
174 | when '1' |
|
176 | when '1' | |
175 | register_by_email_activation(user) do |
|
177 | register_by_email_activation(user) do | |
176 | onthefly_creation_failed(user) |
|
178 | onthefly_creation_failed(user) | |
177 | end |
|
179 | end | |
178 | when '3' |
|
180 | when '3' | |
179 | register_automatically(user) do |
|
181 | register_automatically(user) do | |
180 | onthefly_creation_failed(user) |
|
182 | onthefly_creation_failed(user) | |
181 | end |
|
183 | end | |
182 | else |
|
184 | else | |
183 | register_manually_by_administrator(user) do |
|
185 | register_manually_by_administrator(user) do | |
184 | onthefly_creation_failed(user) |
|
186 | onthefly_creation_failed(user) | |
185 | end |
|
187 | end | |
186 | end |
|
188 | end | |
187 | else |
|
189 | else | |
188 | # Existing record |
|
190 | # Existing record | |
189 | if user.active? |
|
191 | if user.active? | |
190 | successful_authentication(user) |
|
192 | successful_authentication(user) | |
191 | else |
|
193 | else | |
192 | account_pending |
|
194 | account_pending | |
193 | end |
|
195 | end | |
194 | end |
|
196 | end | |
195 | end |
|
197 | end | |
196 | end |
|
198 | end | |
197 | end |
|
199 | end | |
198 |
|
200 | |||
199 | def successful_authentication(user) |
|
201 | def successful_authentication(user) | |
200 | # Valid user |
|
202 | # Valid user | |
201 | self.logged_user = user |
|
203 | self.logged_user = user | |
202 | # generate a key and set cookie if autologin |
|
204 | # generate a key and set cookie if autologin | |
203 | if params[:autologin] && Setting.autologin? |
|
205 | if params[:autologin] && Setting.autologin? | |
204 | token = Token.create(:user => user, :action => 'autologin') |
|
206 | token = Token.create(:user => user, :action => 'autologin') | |
205 | cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } |
|
207 | cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } | |
206 | end |
|
208 | end | |
207 | call_hook(:controller_account_success_authentication_after, {:user => user }) |
|
209 | call_hook(:controller_account_success_authentication_after, {:user => user }) | |
208 | redirect_back_or_default :controller => 'my', :action => 'page' |
|
210 | redirect_back_or_default :controller => 'my', :action => 'page' | |
209 | end |
|
211 | end | |
210 |
|
212 | |||
211 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
213 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
212 | def onthefly_creation_failed(user, auth_source_options = { }) |
|
214 | def onthefly_creation_failed(user, auth_source_options = { }) | |
213 | @user = user |
|
215 | @user = user | |
214 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
|
216 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? | |
215 | render :action => 'register' |
|
217 | render :action => 'register' | |
216 | end |
|
218 | end | |
217 |
|
219 | |||
218 | def invalid_credentials |
|
220 | def invalid_credentials | |
219 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" |
|
221 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" | |
220 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
222 | flash.now[:error] = l(:notice_account_invalid_creditentials) | |
221 | end |
|
223 | end | |
222 |
|
224 | |||
223 | # Register a user for email activation. |
|
225 | # Register a user for email activation. | |
224 | # |
|
226 | # | |
225 | # Pass a block for behavior when a user fails to save |
|
227 | # Pass a block for behavior when a user fails to save | |
226 | def register_by_email_activation(user, &block) |
|
228 | def register_by_email_activation(user, &block) | |
227 | token = Token.new(:user => user, :action => "register") |
|
229 | token = Token.new(:user => user, :action => "register") | |
228 | if user.save and token.save |
|
230 | if user.save and token.save | |
229 | Mailer.deliver_register(token) |
|
231 | Mailer.deliver_register(token) | |
230 | flash[:notice] = l(:notice_account_register_done) |
|
232 | flash[:notice] = l(:notice_account_register_done) | |
231 | redirect_to :action => 'login' |
|
233 | redirect_to :action => 'login' | |
232 | else |
|
234 | else | |
233 | yield if block_given? |
|
235 | yield if block_given? | |
234 | end |
|
236 | end | |
235 | end |
|
237 | end | |
236 |
|
238 | |||
237 | # Automatically register a user |
|
239 | # Automatically register a user | |
238 | # |
|
240 | # | |
239 | # Pass a block for behavior when a user fails to save |
|
241 | # Pass a block for behavior when a user fails to save | |
240 | def register_automatically(user, &block) |
|
242 | def register_automatically(user, &block) | |
241 | # Automatic activation |
|
243 | # Automatic activation | |
242 | user.status = User::STATUS_ACTIVE |
|
244 | user.status = User::STATUS_ACTIVE | |
243 | user.last_login_on = Time.now |
|
245 | user.last_login_on = Time.now | |
244 | if user.save |
|
246 | if user.save | |
245 | self.logged_user = user |
|
247 | self.logged_user = user | |
246 | flash[:notice] = l(:notice_account_activated) |
|
248 | flash[:notice] = l(:notice_account_activated) | |
247 | redirect_to :controller => 'my', :action => 'account' |
|
249 | redirect_to :controller => 'my', :action => 'account' | |
248 | else |
|
250 | else | |
249 | yield if block_given? |
|
251 | yield if block_given? | |
250 | end |
|
252 | end | |
251 | end |
|
253 | end | |
252 |
|
254 | |||
253 | # Manual activation by the administrator |
|
255 | # Manual activation by the administrator | |
254 | # |
|
256 | # | |
255 | # Pass a block for behavior when a user fails to save |
|
257 | # Pass a block for behavior when a user fails to save | |
256 | def register_manually_by_administrator(user, &block) |
|
258 | def register_manually_by_administrator(user, &block) | |
257 | if user.save |
|
259 | if user.save | |
258 | # Sends an email to the administrators |
|
260 | # Sends an email to the administrators | |
259 | Mailer.deliver_account_activation_request(user) |
|
261 | Mailer.deliver_account_activation_request(user) | |
260 | account_pending |
|
262 | account_pending | |
261 | else |
|
263 | else | |
262 | yield if block_given? |
|
264 | yield if block_given? | |
263 | end |
|
265 | end | |
264 | end |
|
266 | end | |
265 |
|
267 | |||
266 | def account_pending |
|
268 | def account_pending | |
267 | flash[:notice] = l(:notice_account_pending) |
|
269 | flash[:notice] = l(:notice_account_pending) | |
268 | redirect_to :action => 'login' |
|
270 | redirect_to :action => 'login' | |
269 | end |
|
271 | end | |
270 | end |
|
272 | end |
General Comments 0
You need to be logged in to leave comments.
Login now