##// END OF EJS Templates
Makes the autologin cookie configurable (#1763)....
Jean-Philippe Lang -
r4636:55acbcb560cc
parent child
Show More
@@ -1,272 +1,283
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.register
86 @user.register
87 if session[:auth_source_registration]
87 if session[:auth_source_registration]
88 @user.activate
88 @user.activate
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.registered?
119 redirect_to(home_url) && return unless user.registered?
120 user.activate
120 user.activate
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 if User.current.logged?
132 cookies.delete :autologin
132 cookies.delete :autologin
133 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
133 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
134 self.logged_user = nil
134 self.logged_user = nil
135 end
135 end
136 end
136 end
137
137
138 def authenticate_user
138 def authenticate_user
139 if Setting.openid? && using_open_id?
139 if Setting.openid? && using_open_id?
140 open_id_authenticate(params[:openid_url])
140 open_id_authenticate(params[:openid_url])
141 else
141 else
142 password_authentication
142 password_authentication
143 end
143 end
144 end
144 end
145
145
146 def password_authentication
146 def password_authentication
147 user = User.try_to_login(params[:username], params[:password])
147 user = User.try_to_login(params[:username], params[:password])
148
148
149 if user.nil?
149 if user.nil?
150 invalid_credentials
150 invalid_credentials
151 elsif user.new_record?
151 elsif user.new_record?
152 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 })
153 else
153 else
154 # Valid user
154 # Valid user
155 successful_authentication(user)
155 successful_authentication(user)
156 end
156 end
157 end
157 end
158
158
159
159
160 def open_id_authenticate(openid_url)
160 def open_id_authenticate(openid_url)
161 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|
162 if result.successful?
162 if result.successful?
163 user = User.find_or_initialize_by_identity_url(identity_url)
163 user = User.find_or_initialize_by_identity_url(identity_url)
164 if user.new_record?
164 if user.new_record?
165 # Self-registration off
165 # Self-registration off
166 redirect_to(home_url) && return unless Setting.self_registration?
166 redirect_to(home_url) && return unless Setting.self_registration?
167
167
168 # Create on the fly
168 # Create on the fly
169 user.login = registration['nickname'] unless registration['nickname'].nil?
169 user.login = registration['nickname'] unless registration['nickname'].nil?
170 user.mail = registration['email'] unless registration['email'].nil?
170 user.mail = registration['email'] unless registration['email'].nil?
171 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
171 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
172 user.random_password
172 user.random_password
173 user.register
173 user.register
174
174
175 case Setting.self_registration
175 case Setting.self_registration
176 when '1'
176 when '1'
177 register_by_email_activation(user) do
177 register_by_email_activation(user) do
178 onthefly_creation_failed(user)
178 onthefly_creation_failed(user)
179 end
179 end
180 when '3'
180 when '3'
181 register_automatically(user) do
181 register_automatically(user) do
182 onthefly_creation_failed(user)
182 onthefly_creation_failed(user)
183 end
183 end
184 else
184 else
185 register_manually_by_administrator(user) do
185 register_manually_by_administrator(user) do
186 onthefly_creation_failed(user)
186 onthefly_creation_failed(user)
187 end
187 end
188 end
188 end
189 else
189 else
190 # Existing record
190 # Existing record
191 if user.active?
191 if user.active?
192 successful_authentication(user)
192 successful_authentication(user)
193 else
193 else
194 account_pending
194 account_pending
195 end
195 end
196 end
196 end
197 end
197 end
198 end
198 end
199 end
199 end
200
200
201 def successful_authentication(user)
201 def successful_authentication(user)
202 # Valid user
202 # Valid user
203 self.logged_user = user
203 self.logged_user = user
204 # generate a key and set cookie if autologin
204 # generate a key and set cookie if autologin
205 if params[:autologin] && Setting.autologin?
205 if params[:autologin] && Setting.autologin?
206 token = Token.create(:user => user, :action => 'autologin')
206 set_autologin_cookie(user)
207 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
208 end
207 end
209 call_hook(:controller_account_success_authentication_after, {:user => user })
208 call_hook(:controller_account_success_authentication_after, {:user => user })
210 redirect_back_or_default :controller => 'my', :action => 'page'
209 redirect_back_or_default :controller => 'my', :action => 'page'
211 end
210 end
211
212 def set_autologin_cookie(user)
213 token = Token.create(:user => user, :action => 'autologin')
214 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
215 cookie_options = {
216 :value => token.value,
217 :expires => 1.year.from_now,
218 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
219 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false)
220 }
221 cookies[cookie_name] = cookie_options
222 end
212
223
213 # Onthefly creation failed, display the registration form to fill/fix attributes
224 # Onthefly creation failed, display the registration form to fill/fix attributes
214 def onthefly_creation_failed(user, auth_source_options = { })
225 def onthefly_creation_failed(user, auth_source_options = { })
215 @user = user
226 @user = user
216 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
227 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
217 render :action => 'register'
228 render :action => 'register'
218 end
229 end
219
230
220 def invalid_credentials
231 def invalid_credentials
221 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
232 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
222 flash.now[:error] = l(:notice_account_invalid_creditentials)
233 flash.now[:error] = l(:notice_account_invalid_creditentials)
223 end
234 end
224
235
225 # Register a user for email activation.
236 # Register a user for email activation.
226 #
237 #
227 # Pass a block for behavior when a user fails to save
238 # Pass a block for behavior when a user fails to save
228 def register_by_email_activation(user, &block)
239 def register_by_email_activation(user, &block)
229 token = Token.new(:user => user, :action => "register")
240 token = Token.new(:user => user, :action => "register")
230 if user.save and token.save
241 if user.save and token.save
231 Mailer.deliver_register(token)
242 Mailer.deliver_register(token)
232 flash[:notice] = l(:notice_account_register_done)
243 flash[:notice] = l(:notice_account_register_done)
233 redirect_to :action => 'login'
244 redirect_to :action => 'login'
234 else
245 else
235 yield if block_given?
246 yield if block_given?
236 end
247 end
237 end
248 end
238
249
239 # Automatically register a user
250 # Automatically register a user
240 #
251 #
241 # Pass a block for behavior when a user fails to save
252 # Pass a block for behavior when a user fails to save
242 def register_automatically(user, &block)
253 def register_automatically(user, &block)
243 # Automatic activation
254 # Automatic activation
244 user.activate
255 user.activate
245 user.last_login_on = Time.now
256 user.last_login_on = Time.now
246 if user.save
257 if user.save
247 self.logged_user = user
258 self.logged_user = user
248 flash[:notice] = l(:notice_account_activated)
259 flash[:notice] = l(:notice_account_activated)
249 redirect_to :controller => 'my', :action => 'account'
260 redirect_to :controller => 'my', :action => 'account'
250 else
261 else
251 yield if block_given?
262 yield if block_given?
252 end
263 end
253 end
264 end
254
265
255 # Manual activation by the administrator
266 # Manual activation by the administrator
256 #
267 #
257 # Pass a block for behavior when a user fails to save
268 # Pass a block for behavior when a user fails to save
258 def register_manually_by_administrator(user, &block)
269 def register_manually_by_administrator(user, &block)
259 if user.save
270 if user.save
260 # Sends an email to the administrators
271 # Sends an email to the administrators
261 Mailer.deliver_account_activation_request(user)
272 Mailer.deliver_account_activation_request(user)
262 account_pending
273 account_pending
263 else
274 else
264 yield if block_given?
275 yield if block_given?
265 end
276 end
266 end
277 end
267
278
268 def account_pending
279 def account_pending
269 flash[:notice] = l(:notice_account_pending)
280 flash[:notice] = l(:notice_account_pending)
270 redirect_to :action => 'login'
281 redirect_to :action => 'login'
271 end
282 end
272 end
283 end
@@ -1,108 +1,116
1 # = Redmine configuration file
1 # = Redmine configuration file
2 #
2 #
3 # Each environment has it's own configuration options. If you are only
3 # Each environment has it's own configuration options. If you are only
4 # running in production, only the production block needs to be configured.
4 # running in production, only the production block needs to be configured.
5 # Environment specific configuration options override the default ones.
5 # Environment specific configuration options override the default ones.
6 #
6 #
7 # Note that this file needs to be a valid YAML file.
7 # Note that this file needs to be a valid YAML file.
8 #
8 #
9 # == Outgoing email settings (email_delivery setting)
9 # == Outgoing email settings (email_delivery setting)
10 #
10 #
11 # === Common configurations
11 # === Common configurations
12 #
12 #
13 # ==== Sendmail command
13 # ==== Sendmail command
14 #
14 #
15 # production:
15 # production:
16 # email_delivery:
16 # email_delivery:
17 # delivery_method: :sendmail
17 # delivery_method: :sendmail
18 #
18 #
19 # ==== Simple SMTP server at localhost
19 # ==== Simple SMTP server at localhost
20 #
20 #
21 # production:
21 # production:
22 # email_delivery:
22 # email_delivery:
23 # delivery_method: :smtp
23 # delivery_method: :smtp
24 # smtp_settings:
24 # smtp_settings:
25 # address: "localhost"
25 # address: "localhost"
26 # port: 25
26 # port: 25
27 #
27 #
28 # ==== SMTP server at example.com using LOGIN authentication and checking HELO for foo.com
28 # ==== SMTP server at example.com using LOGIN authentication and checking HELO for foo.com
29 #
29 #
30 # production:
30 # production:
31 # email_delivery:
31 # email_delivery:
32 # delivery_method: :smtp
32 # delivery_method: :smtp
33 # smtp_settings:
33 # smtp_settings:
34 # address: "example.com"
34 # address: "example.com"
35 # port: 25
35 # port: 25
36 # authentication: :login
36 # authentication: :login
37 # domain: 'foo.com'
37 # domain: 'foo.com'
38 # user_name: 'myaccount'
38 # user_name: 'myaccount'
39 # password: 'password'
39 # password: 'password'
40 #
40 #
41 # ==== SMTP server at example.com using PLAIN authentication
41 # ==== SMTP server at example.com using PLAIN authentication
42 #
42 #
43 # production:
43 # production:
44 # email_delivery:
44 # email_delivery:
45 # delivery_method: :smtp
45 # delivery_method: :smtp
46 # smtp_settings:
46 # smtp_settings:
47 # address: "example.com"
47 # address: "example.com"
48 # port: 25
48 # port: 25
49 # authentication: :plain
49 # authentication: :plain
50 # domain: 'example.com'
50 # domain: 'example.com'
51 # user_name: 'myaccount'
51 # user_name: 'myaccount'
52 # password: 'password'
52 # password: 'password'
53 #
53 #
54 # ==== SMTP server at using TLS (GMail)
54 # ==== SMTP server at using TLS (GMail)
55 #
55 #
56 # This requires some additional configuration. See the article at:
56 # This requires some additional configuration. See the article at:
57 # http://redmineblog.com/articles/setup-redmine-to-send-email-using-gmail/
57 # http://redmineblog.com/articles/setup-redmine-to-send-email-using-gmail/
58 #
58 #
59 # production:
59 # production:
60 # email_delivery:
60 # email_delivery:
61 # delivery_method: :smtp
61 # delivery_method: :smtp
62 # smtp_settings:
62 # smtp_settings:
63 # tls: true
63 # tls: true
64 # address: "smtp.gmail.com"
64 # address: "smtp.gmail.com"
65 # port: 587
65 # port: 587
66 # domain: "smtp.gmail.com" # 'your.domain.com' for GoogleApps
66 # domain: "smtp.gmail.com" # 'your.domain.com' for GoogleApps
67 # authentication: :plain
67 # authentication: :plain
68 # user_name: "your_email@gmail.com"
68 # user_name: "your_email@gmail.com"
69 # password: "your_password"
69 # password: "your_password"
70 #
70 #
71 #
71 #
72 # === More configuration options
72 # === More configuration options
73 #
73 #
74 # See the "Configuration options" at the following website for a list of the
74 # See the "Configuration options" at the following website for a list of the
75 # full options allowed:
75 # full options allowed:
76 #
76 #
77 # http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer
77 # http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer
78
78
79
79
80 # default configuration options for all environments
80 # default configuration options for all environments
81 default:
81 default:
82 # Outgoing emails configuration (see examples above)
82 # Outgoing emails configuration (see examples above)
83 email_delivery:
83 email_delivery:
84 delivery_method: :smtp
84 delivery_method: :smtp
85 smtp_settings:
85 smtp_settings:
86 address: smtp.example.net
86 address: smtp.example.net
87 port: 25
87 port: 25
88 domain: example.net
88 domain: example.net
89 authentication: :login
89 authentication: :login
90 user_name: "redmine@example.net"
90 user_name: "redmine@example.net"
91 password: "redmine"
91 password: "redmine"
92
92
93 # Absolute path to the directory where attachments are stored.
93 # Absolute path to the directory where attachments are stored.
94 # The default is the 'files' directory in your Redmine instance.
94 # The default is the 'files' directory in your Redmine instance.
95 # Your Redmine instance needs to have write permission on this
95 # Your Redmine instance needs to have write permission on this
96 # directory.
96 # directory.
97 # Examples:
97 # Examples:
98 # attachments_storage_path: /var/redmine/files
98 # attachments_storage_path: /var/redmine/files
99 # attachments_storage_path: D:/redmine/files
99 # attachments_storage_path: D:/redmine/files
100 attachments_storage_path:
100 attachments_storage_path:
101
101
102 # Configuration of the autologin cookie.
103 # autologin_cookie_name: the name of the cookie (default: autologin)
104 # autologin_cookie_path: the cookie path (default: /)
105 # autologin_cookie_secure: true sets the cookie secure flag (default: false)
106 autologin_cookie_name:
107 autologin_cookie_path:
108 autologin_cookie_secure:
109
102 # specific configuration options for production environment
110 # specific configuration options for production environment
103 # that overrides the default ones
111 # that overrides the default ones
104 production:
112 production:
105
113
106 # specific configuration options for development environment
114 # specific configuration options for development environment
107 # that overrides the default ones
115 # that overrides the default ones
108 development:
116 development:
General Comments 0
You need to be logged in to leave comments. Login now