@@ -1,284 +1,287 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | rescue AuthSourceException => e | |
|
33 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" | |
|
34 | render_error :message => e.message | |
|
32 | 35 | end |
|
33 | 36 | |
|
34 | 37 | # Log out current user and redirect to welcome page |
|
35 | 38 | def logout |
|
36 | 39 | logout_user |
|
37 | 40 | redirect_to home_url |
|
38 | 41 | end |
|
39 | 42 | |
|
40 | 43 | # Enable user to choose a new password |
|
41 | 44 | def lost_password |
|
42 | 45 | redirect_to(home_url) && return unless Setting.lost_password? |
|
43 | 46 | if params[:token] |
|
44 | 47 | @token = Token.find_by_action_and_value("recovery", params[:token]) |
|
45 | 48 | redirect_to(home_url) && return unless @token and !@token.expired? |
|
46 | 49 | @user = @token.user |
|
47 | 50 | if request.post? |
|
48 | 51 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
49 | 52 | if @user.save |
|
50 | 53 | @token.destroy |
|
51 | 54 | flash[:notice] = l(:notice_account_password_updated) |
|
52 | 55 | redirect_to :action => 'login' |
|
53 | 56 | return |
|
54 | 57 | end |
|
55 | 58 | end |
|
56 | 59 | render :template => "account/password_recovery" |
|
57 | 60 | return |
|
58 | 61 | else |
|
59 | 62 | if request.post? |
|
60 | 63 | user = User.find_by_mail(params[:mail]) |
|
61 | 64 | # user not found in db |
|
62 | 65 | (flash.now[:error] = l(:notice_account_unknown_email); return) unless user |
|
63 | 66 | # user uses an external authentification |
|
64 | 67 | (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id |
|
65 | 68 | # create a new token for password recovery |
|
66 | 69 | token = Token.new(:user => user, :action => "recovery") |
|
67 | 70 | if token.save |
|
68 | 71 | Mailer.deliver_lost_password(token) |
|
69 | 72 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
70 | 73 | redirect_to :action => 'login' |
|
71 | 74 | return |
|
72 | 75 | end |
|
73 | 76 | end |
|
74 | 77 | end |
|
75 | 78 | end |
|
76 | 79 | |
|
77 | 80 | # User self-registration |
|
78 | 81 | def register |
|
79 | 82 | redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] |
|
80 | 83 | if request.get? |
|
81 | 84 | session[:auth_source_registration] = nil |
|
82 | 85 | @user = User.new(:language => Setting.default_language) |
|
83 | 86 | else |
|
84 | 87 | @user = User.new |
|
85 | 88 | @user.safe_attributes = params[:user] |
|
86 | 89 | @user.admin = false |
|
87 | 90 | @user.register |
|
88 | 91 | if session[:auth_source_registration] |
|
89 | 92 | @user.activate |
|
90 | 93 | @user.login = session[:auth_source_registration][:login] |
|
91 | 94 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
92 | 95 | if @user.save |
|
93 | 96 | session[:auth_source_registration] = nil |
|
94 | 97 | self.logged_user = @user |
|
95 | 98 | flash[:notice] = l(:notice_account_activated) |
|
96 | 99 | redirect_to :controller => 'my', :action => 'account' |
|
97 | 100 | end |
|
98 | 101 | else |
|
99 | 102 | @user.login = params[:user][:login] |
|
100 | 103 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
101 | 104 | |
|
102 | 105 | case Setting.self_registration |
|
103 | 106 | when '1' |
|
104 | 107 | register_by_email_activation(@user) |
|
105 | 108 | when '3' |
|
106 | 109 | register_automatically(@user) |
|
107 | 110 | else |
|
108 | 111 | register_manually_by_administrator(@user) |
|
109 | 112 | end |
|
110 | 113 | end |
|
111 | 114 | end |
|
112 | 115 | end |
|
113 | 116 | |
|
114 | 117 | # Token based account activation |
|
115 | 118 | def activate |
|
116 | 119 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] |
|
117 | 120 | token = Token.find_by_action_and_value('register', params[:token]) |
|
118 | 121 | redirect_to(home_url) && return unless token and !token.expired? |
|
119 | 122 | user = token.user |
|
120 | 123 | redirect_to(home_url) && return unless user.registered? |
|
121 | 124 | user.activate |
|
122 | 125 | if user.save |
|
123 | 126 | token.destroy |
|
124 | 127 | flash[:notice] = l(:notice_account_activated) |
|
125 | 128 | end |
|
126 | 129 | redirect_to :action => 'login' |
|
127 | 130 | end |
|
128 | 131 | |
|
129 | 132 | private |
|
130 | 133 | |
|
131 | 134 | def logout_user |
|
132 | 135 | if User.current.logged? |
|
133 | 136 | cookies.delete :autologin |
|
134 | 137 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) |
|
135 | 138 | self.logged_user = nil |
|
136 | 139 | end |
|
137 | 140 | end |
|
138 | 141 | |
|
139 | 142 | def authenticate_user |
|
140 | 143 | if Setting.openid? && using_open_id? |
|
141 | 144 | open_id_authenticate(params[:openid_url]) |
|
142 | 145 | else |
|
143 | 146 | password_authentication |
|
144 | 147 | end |
|
145 | 148 | end |
|
146 | 149 | |
|
147 | 150 | def password_authentication |
|
148 | 151 | user = User.try_to_login(params[:username], params[:password]) |
|
149 | 152 | |
|
150 | 153 | if user.nil? |
|
151 | 154 | invalid_credentials |
|
152 | 155 | elsif user.new_record? |
|
153 | 156 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) |
|
154 | 157 | else |
|
155 | 158 | # Valid user |
|
156 | 159 | successful_authentication(user) |
|
157 | 160 | end |
|
158 | 161 | end |
|
159 | 162 | |
|
160 | 163 | def open_id_authenticate(openid_url) |
|
161 | 164 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration| |
|
162 | 165 | if result.successful? |
|
163 | 166 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
164 | 167 | if user.new_record? |
|
165 | 168 | # Self-registration off |
|
166 | 169 | redirect_to(home_url) && return unless Setting.self_registration? |
|
167 | 170 | |
|
168 | 171 | # Create on the fly |
|
169 | 172 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
170 | 173 | user.mail = registration['email'] unless registration['email'].nil? |
|
171 | 174 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
172 | 175 | user.random_password |
|
173 | 176 | user.register |
|
174 | 177 | |
|
175 | 178 | case Setting.self_registration |
|
176 | 179 | when '1' |
|
177 | 180 | register_by_email_activation(user) do |
|
178 | 181 | onthefly_creation_failed(user) |
|
179 | 182 | end |
|
180 | 183 | when '3' |
|
181 | 184 | register_automatically(user) do |
|
182 | 185 | onthefly_creation_failed(user) |
|
183 | 186 | end |
|
184 | 187 | else |
|
185 | 188 | register_manually_by_administrator(user) do |
|
186 | 189 | onthefly_creation_failed(user) |
|
187 | 190 | end |
|
188 | 191 | end |
|
189 | 192 | else |
|
190 | 193 | # Existing record |
|
191 | 194 | if user.active? |
|
192 | 195 | successful_authentication(user) |
|
193 | 196 | else |
|
194 | 197 | account_pending |
|
195 | 198 | end |
|
196 | 199 | end |
|
197 | 200 | end |
|
198 | 201 | end |
|
199 | 202 | end |
|
200 | 203 | |
|
201 | 204 | def successful_authentication(user) |
|
202 | 205 | # Valid user |
|
203 | 206 | self.logged_user = user |
|
204 | 207 | # generate a key and set cookie if autologin |
|
205 | 208 | if params[:autologin] && Setting.autologin? |
|
206 | 209 | set_autologin_cookie(user) |
|
207 | 210 | end |
|
208 | 211 | call_hook(:controller_account_success_authentication_after, {:user => user }) |
|
209 | 212 | redirect_back_or_default :controller => 'my', :action => 'page' |
|
210 | 213 | end |
|
211 | 214 | |
|
212 | 215 | def set_autologin_cookie(user) |
|
213 | 216 | token = Token.create(:user => user, :action => 'autologin') |
|
214 | 217 | cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin' |
|
215 | 218 | cookie_options = { |
|
216 | 219 | :value => token.value, |
|
217 | 220 | :expires => 1.year.from_now, |
|
218 | 221 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), |
|
219 | 222 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), |
|
220 | 223 | :httponly => true |
|
221 | 224 | } |
|
222 | 225 | cookies[cookie_name] = cookie_options |
|
223 | 226 | end |
|
224 | 227 | |
|
225 | 228 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
226 | 229 | def onthefly_creation_failed(user, auth_source_options = { }) |
|
227 | 230 | @user = user |
|
228 | 231 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
|
229 | 232 | render :action => 'register' |
|
230 | 233 | end |
|
231 | 234 | |
|
232 | 235 | def invalid_credentials |
|
233 | 236 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" |
|
234 | 237 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
235 | 238 | end |
|
236 | 239 | |
|
237 | 240 | # Register a user for email activation. |
|
238 | 241 | # |
|
239 | 242 | # Pass a block for behavior when a user fails to save |
|
240 | 243 | def register_by_email_activation(user, &block) |
|
241 | 244 | token = Token.new(:user => user, :action => "register") |
|
242 | 245 | if user.save and token.save |
|
243 | 246 | Mailer.deliver_register(token) |
|
244 | 247 | flash[:notice] = l(:notice_account_register_done) |
|
245 | 248 | redirect_to :action => 'login' |
|
246 | 249 | else |
|
247 | 250 | yield if block_given? |
|
248 | 251 | end |
|
249 | 252 | end |
|
250 | 253 | |
|
251 | 254 | # Automatically register a user |
|
252 | 255 | # |
|
253 | 256 | # Pass a block for behavior when a user fails to save |
|
254 | 257 | def register_automatically(user, &block) |
|
255 | 258 | # Automatic activation |
|
256 | 259 | user.activate |
|
257 | 260 | user.last_login_on = Time.now |
|
258 | 261 | if user.save |
|
259 | 262 | self.logged_user = user |
|
260 | 263 | flash[:notice] = l(:notice_account_activated) |
|
261 | 264 | redirect_to :controller => 'my', :action => 'account' |
|
262 | 265 | else |
|
263 | 266 | yield if block_given? |
|
264 | 267 | end |
|
265 | 268 | end |
|
266 | 269 | |
|
267 | 270 | # Manual activation by the administrator |
|
268 | 271 | # |
|
269 | 272 | # Pass a block for behavior when a user fails to save |
|
270 | 273 | def register_manually_by_administrator(user, &block) |
|
271 | 274 | if user.save |
|
272 | 275 | # Sends an email to the administrators |
|
273 | 276 | Mailer.deliver_account_activation_request(user) |
|
274 | 277 | account_pending |
|
275 | 278 | else |
|
276 | 279 | yield if block_given? |
|
277 | 280 | end |
|
278 | 281 | end |
|
279 | 282 | |
|
280 | 283 | def account_pending |
|
281 | 284 | flash[:notice] = l(:notice_account_pending) |
|
282 | 285 | redirect_to :action => 'login' |
|
283 | 286 | end |
|
284 | 287 | end |
@@ -1,68 +1,72 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | # Generic exception for when the AuthSource can not be reached | |
|
19 | # (eg. can not connect to the LDAP) | |
|
20 | class AuthSourceException < Exception; end | |
|
21 | ||
|
18 | 22 | class AuthSource < ActiveRecord::Base |
|
19 | 23 | include Redmine::Ciphering |
|
20 | 24 | |
|
21 | 25 | has_many :users |
|
22 | 26 | |
|
23 | 27 | validates_presence_of :name |
|
24 | 28 | validates_uniqueness_of :name |
|
25 | 29 | validates_length_of :name, :maximum => 60 |
|
26 | 30 | |
|
27 | 31 | def authenticate(login, password) |
|
28 | 32 | end |
|
29 | 33 | |
|
30 | 34 | def test_connection |
|
31 | 35 | end |
|
32 | 36 | |
|
33 | 37 | def auth_method_name |
|
34 | 38 | "Abstract" |
|
35 | 39 | end |
|
36 | 40 | |
|
37 | 41 | def account_password |
|
38 | 42 | read_ciphered_attribute(:account_password) |
|
39 | 43 | end |
|
40 | 44 | |
|
41 | 45 | def account_password=(arg) |
|
42 | 46 | write_ciphered_attribute(:account_password, arg) |
|
43 | 47 | end |
|
44 | 48 | |
|
45 | 49 | def allow_password_changes? |
|
46 | 50 | self.class.allow_password_changes? |
|
47 | 51 | end |
|
48 | 52 | |
|
49 | 53 | # Does this auth source backend allow password changes? |
|
50 | 54 | def self.allow_password_changes? |
|
51 | 55 | false |
|
52 | 56 | end |
|
53 | 57 | |
|
54 | 58 | # Try to authenticate a user not yet registered against available sources |
|
55 | 59 | def self.authenticate(login, password) |
|
56 | 60 | AuthSource.find(:all, :conditions => ["onthefly_register=?", true]).each do |source| |
|
57 | 61 | begin |
|
58 | 62 | logger.debug "Authenticating '#{login}' against '#{source.name}'" if logger && logger.debug? |
|
59 | 63 | attrs = source.authenticate(login, password) |
|
60 | 64 | rescue => e |
|
61 | 65 | logger.error "Error during authentication: #{e.message}" |
|
62 | 66 | attrs = nil |
|
63 | 67 | end |
|
64 | 68 | return attrs if attrs |
|
65 | 69 | end |
|
66 | 70 | return nil |
|
67 | 71 | end |
|
68 | 72 | end |
@@ -1,131 +1,131 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | require 'iconv' |
|
19 | 19 | require 'net/ldap' |
|
20 | 20 | |
|
21 | 21 | class AuthSourceLdap < AuthSource |
|
22 | 22 | validates_presence_of :host, :port, :attr_login |
|
23 | 23 | validates_length_of :name, :host, :maximum => 60, :allow_nil => true |
|
24 | 24 | validates_length_of :account, :account_password, :base_dn, :maximum => 255, :allow_nil => true |
|
25 | 25 | validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true |
|
26 | 26 | validates_numericality_of :port, :only_integer => true |
|
27 | 27 | |
|
28 | 28 | before_validation :strip_ldap_attributes |
|
29 | 29 | |
|
30 | 30 | def initialize(attributes=nil, *args) |
|
31 | 31 | super |
|
32 | 32 | self.port = 389 if self.port == 0 |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def authenticate(login, password) |
|
36 | 36 | return nil if login.blank? || password.blank? |
|
37 | 37 | attrs = get_user_dn(login) |
|
38 | 38 | |
|
39 | 39 | if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password) |
|
40 | 40 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? |
|
41 | 41 | return attrs.except(:dn) |
|
42 | 42 | end |
|
43 |
rescue Net::LDAP::LdapError => |
|
|
44 | raise "LdapError: " + text | |
|
43 | rescue Net::LDAP::LdapError => e | |
|
44 | raise AuthSourceException.new(e.message) | |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | # test the connection to the LDAP |
|
48 | 48 | def test_connection |
|
49 | 49 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
50 | 50 | ldap_con.open { } |
|
51 | 51 | rescue Net::LDAP::LdapError => text |
|
52 | 52 | raise "LdapError: " + text |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def auth_method_name |
|
56 | 56 | "LDAP" |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | private |
|
60 | 60 | |
|
61 | 61 | def strip_ldap_attributes |
|
62 | 62 | [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| |
|
63 | 63 | write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def initialize_ldap_con(ldap_user, ldap_password) |
|
68 | 68 | options = { :host => self.host, |
|
69 | 69 | :port => self.port, |
|
70 | 70 | :encryption => (self.tls ? :simple_tls : nil) |
|
71 | 71 | } |
|
72 | 72 | options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? |
|
73 | 73 | Net::LDAP.new options |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | def get_user_attributes_from_ldap_entry(entry) |
|
77 | 77 | { |
|
78 | 78 | :dn => entry.dn, |
|
79 | 79 | :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), |
|
80 | 80 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), |
|
81 | 81 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), |
|
82 | 82 | :auth_source_id => self.id |
|
83 | 83 | } |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | # Return the attributes needed for the LDAP search. It will only |
|
87 | 87 | # include the user attributes if on-the-fly registration is enabled |
|
88 | 88 | def search_attributes |
|
89 | 89 | if onthefly_register? |
|
90 | 90 | ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] |
|
91 | 91 | else |
|
92 | 92 | ['dn'] |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | # Check if a DN (user record) authenticates with the password |
|
97 | 97 | def authenticate_dn(dn, password) |
|
98 | 98 | if dn.present? && password.present? |
|
99 | 99 | initialize_ldap_con(dn, password).bind |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | # Get the user's dn and any attributes for them, given their login |
|
104 | 104 | def get_user_dn(login) |
|
105 | 105 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
106 | 106 | login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) |
|
107 | 107 | object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) |
|
108 | 108 | attrs = {} |
|
109 | 109 | |
|
110 | 110 | ldap_con.search( :base => self.base_dn, |
|
111 | 111 | :filter => object_filter & login_filter, |
|
112 | 112 | :attributes=> search_attributes) do |entry| |
|
113 | 113 | |
|
114 | 114 | if onthefly_register? |
|
115 | 115 | attrs = get_user_attributes_from_ldap_entry(entry) |
|
116 | 116 | else |
|
117 | 117 | attrs = {:dn => entry.dn} |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | 120 | logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug? |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | attrs |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def self.get_attr(entry, attr_name) |
|
127 | 127 | if !attr_name.blank? |
|
128 | 128 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] |
|
129 | 129 | end |
|
130 | 130 | end |
|
131 | 131 | end |
@@ -1,222 +1,232 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | require 'account_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class AccountController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class AccountControllerTest < ActionController::TestCase |
|
25 | 25 | fixtures :users, :roles |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = AccountController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | User.current = nil |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_login_should_redirect_to_back_url_param |
|
35 | 35 | # request.uri is "test.host" in test environment |
|
36 | 36 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' |
|
37 | 37 | assert_redirected_to '/issues/show/1' |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def test_login_should_not_redirect_to_another_host |
|
41 | 41 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' |
|
42 | 42 | assert_redirected_to '/my/page' |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def test_login_with_wrong_password |
|
46 | 46 | post :login, :username => 'admin', :password => 'bad' |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_template 'login' |
|
49 | 49 | assert_tag 'div', |
|
50 | 50 | :attributes => { :class => "flash error" }, |
|
51 | 51 | :content => /Invalid user or password/ |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | def test_login_should_rescue_auth_source_exception | |
|
55 | source = AuthSource.create!(:name => 'Test') | |
|
56 | User.find(2).update_attribute :auth_source_id, source.id | |
|
57 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) | |
|
58 | ||
|
59 | post :login, :username => 'jsmith', :password => 'jsmith' | |
|
60 | assert_response 500 | |
|
61 | assert_error_tag :content => /Something wrong/ | |
|
62 | end | |
|
63 | ||
|
54 | 64 | if Object.const_defined?(:OpenID) |
|
55 | 65 | |
|
56 | 66 | def test_login_with_openid_for_existing_user |
|
57 | 67 | Setting.self_registration = '3' |
|
58 | 68 | Setting.openid = '1' |
|
59 | 69 | existing_user = User.new(:firstname => 'Cool', |
|
60 | 70 | :lastname => 'User', |
|
61 | 71 | :mail => 'user@somedomain.com', |
|
62 | 72 | :identity_url => 'http://openid.example.com/good_user') |
|
63 | 73 | existing_user.login = 'cool_user' |
|
64 | 74 | assert existing_user.save! |
|
65 | 75 | |
|
66 | 76 | post :login, :openid_url => existing_user.identity_url |
|
67 | 77 | assert_redirected_to '/my/page' |
|
68 | 78 | end |
|
69 | 79 | |
|
70 | 80 | def test_login_with_invalid_openid_provider |
|
71 | 81 | Setting.self_registration = '0' |
|
72 | 82 | Setting.openid = '1' |
|
73 | 83 | post :login, :openid_url => 'http;//openid.example.com/good_user' |
|
74 | 84 | assert_redirected_to home_url |
|
75 | 85 | end |
|
76 | 86 | |
|
77 | 87 | def test_login_with_openid_for_existing_non_active_user |
|
78 | 88 | Setting.self_registration = '2' |
|
79 | 89 | Setting.openid = '1' |
|
80 | 90 | existing_user = User.new(:firstname => 'Cool', |
|
81 | 91 | :lastname => 'User', |
|
82 | 92 | :mail => 'user@somedomain.com', |
|
83 | 93 | :identity_url => 'http://openid.example.com/good_user', |
|
84 | 94 | :status => User::STATUS_REGISTERED) |
|
85 | 95 | existing_user.login = 'cool_user' |
|
86 | 96 | assert existing_user.save! |
|
87 | 97 | |
|
88 | 98 | post :login, :openid_url => existing_user.identity_url |
|
89 | 99 | assert_redirected_to '/login' |
|
90 | 100 | end |
|
91 | 101 | |
|
92 | 102 | def test_login_with_openid_with_new_user_created |
|
93 | 103 | Setting.self_registration = '3' |
|
94 | 104 | Setting.openid = '1' |
|
95 | 105 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
96 | 106 | assert_redirected_to '/my/account' |
|
97 | 107 | user = User.find_by_login('cool_user') |
|
98 | 108 | assert user |
|
99 | 109 | assert_equal 'Cool', user.firstname |
|
100 | 110 | assert_equal 'User', user.lastname |
|
101 | 111 | end |
|
102 | 112 | |
|
103 | 113 | def test_login_with_openid_with_new_user_and_self_registration_off |
|
104 | 114 | Setting.self_registration = '0' |
|
105 | 115 | Setting.openid = '1' |
|
106 | 116 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
107 | 117 | assert_redirected_to home_url |
|
108 | 118 | user = User.find_by_login('cool_user') |
|
109 | 119 | assert ! user |
|
110 | 120 | end |
|
111 | 121 | |
|
112 | 122 | def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token |
|
113 | 123 | Setting.self_registration = '1' |
|
114 | 124 | Setting.openid = '1' |
|
115 | 125 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
116 | 126 | assert_redirected_to '/login' |
|
117 | 127 | user = User.find_by_login('cool_user') |
|
118 | 128 | assert user |
|
119 | 129 | |
|
120 | 130 | token = Token.find_by_user_id_and_action(user.id, 'register') |
|
121 | 131 | assert token |
|
122 | 132 | end |
|
123 | 133 | |
|
124 | 134 | def test_login_with_openid_with_new_user_created_with_manual_activation |
|
125 | 135 | Setting.self_registration = '2' |
|
126 | 136 | Setting.openid = '1' |
|
127 | 137 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
128 | 138 | assert_redirected_to '/login' |
|
129 | 139 | user = User.find_by_login('cool_user') |
|
130 | 140 | assert user |
|
131 | 141 | assert_equal User::STATUS_REGISTERED, user.status |
|
132 | 142 | end |
|
133 | 143 | |
|
134 | 144 | def test_login_with_openid_with_new_user_with_conflict_should_register |
|
135 | 145 | Setting.self_registration = '3' |
|
136 | 146 | Setting.openid = '1' |
|
137 | 147 | existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com') |
|
138 | 148 | existing_user.login = 'cool_user' |
|
139 | 149 | assert existing_user.save! |
|
140 | 150 | |
|
141 | 151 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
142 | 152 | assert_response :success |
|
143 | 153 | assert_template 'register' |
|
144 | 154 | assert assigns(:user) |
|
145 | 155 | assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url] |
|
146 | 156 | end |
|
147 | 157 | |
|
148 | 158 | def test_setting_openid_should_return_true_when_set_to_true |
|
149 | 159 | Setting.openid = '1' |
|
150 | 160 | assert_equal true, Setting.openid? |
|
151 | 161 | end |
|
152 | 162 | |
|
153 | 163 | else |
|
154 | 164 | puts "Skipping openid tests." |
|
155 | 165 | end |
|
156 | 166 | |
|
157 | 167 | def test_logout |
|
158 | 168 | @request.session[:user_id] = 2 |
|
159 | 169 | get :logout |
|
160 | 170 | assert_redirected_to '/' |
|
161 | 171 | assert_nil @request.session[:user_id] |
|
162 | 172 | end |
|
163 | 173 | |
|
164 | 174 | def test_get_register_with_registration_on |
|
165 | 175 | with_settings :self_registration => '3' do |
|
166 | 176 | get :register |
|
167 | 177 | assert_response :success |
|
168 | 178 | assert_template 'register' |
|
169 | 179 | assert_not_nil assigns(:user) |
|
170 | 180 | |
|
171 | 181 | assert_tag 'input', :attributes => {:name => 'user[password]'} |
|
172 | 182 | assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'} |
|
173 | 183 | end |
|
174 | 184 | end |
|
175 | 185 | |
|
176 | 186 | def test_get_register_with_registration_off_should_redirect |
|
177 | 187 | with_settings :self_registration => '0' do |
|
178 | 188 | get :register |
|
179 | 189 | assert_redirected_to '/' |
|
180 | 190 | end |
|
181 | 191 | end |
|
182 | 192 | |
|
183 | 193 | # See integration/account_test.rb for the full test |
|
184 | 194 | def test_post_register_with_registration_on |
|
185 | 195 | with_settings :self_registration => '3' do |
|
186 | 196 | assert_difference 'User.count' do |
|
187 | 197 | post :register, :user => { |
|
188 | 198 | :login => 'register', |
|
189 | 199 | :password => 'test', |
|
190 | 200 | :password_confirmation => 'test', |
|
191 | 201 | :firstname => 'John', |
|
192 | 202 | :lastname => 'Doe', |
|
193 | 203 | :mail => 'register@example.com' |
|
194 | 204 | } |
|
195 | 205 | assert_redirected_to '/my/account' |
|
196 | 206 | end |
|
197 | 207 | user = User.first(:order => 'id DESC') |
|
198 | 208 | assert_equal 'register', user.login |
|
199 | 209 | assert_equal 'John', user.firstname |
|
200 | 210 | assert_equal 'Doe', user.lastname |
|
201 | 211 | assert_equal 'register@example.com', user.mail |
|
202 | 212 | assert user.check_password?('test') |
|
203 | 213 | assert user.active? |
|
204 | 214 | end |
|
205 | 215 | end |
|
206 | 216 | |
|
207 | 217 | def test_post_register_with_registration_off_should_redirect |
|
208 | 218 | with_settings :self_registration => '0' do |
|
209 | 219 | assert_no_difference 'User.count' do |
|
210 | 220 | post :register, :user => { |
|
211 | 221 | :login => 'register', |
|
212 | 222 | :password => 'test', |
|
213 | 223 | :password_confirmation => 'test', |
|
214 | 224 | :firstname => 'John', |
|
215 | 225 | :lastname => 'Doe', |
|
216 | 226 | :mail => 'register@example.com' |
|
217 | 227 | } |
|
218 | 228 | assert_redirected_to '/' |
|
219 | 229 | end |
|
220 | 230 | end |
|
221 | 231 | end |
|
222 | 232 | end |
General Comments 0
You need to be logged in to leave comments.
Login now