##// END OF EJS Templates
Better error message and AR errors in log for failed LDAP on-the-fly user creation (closes #932, #1042)....
Jean-Philippe Lang -
r1330:a340d8c957bb
parent child
Show More
@@ -1,175 +1,177
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 layout 'base'
20 20 helper :custom_fields
21 21 include CustomFieldsHelper
22 22
23 23 # prevents login action to be filtered by check_if_login_required application scope filter
24 24 skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate]
25 25
26 26 # Show user's account
27 27 def show
28 28 @user = User.find_active(params[:id])
29 29 @custom_values = @user.custom_values.find(:all, :include => :custom_field)
30 30
31 31 # show only public projects and private projects that the logged in user is also a member of
32 32 @memberships = @user.memberships.select do |membership|
33 33 membership.project.is_public? || (User.current.member_of?(membership.project))
34 34 end
35 35 rescue ActiveRecord::RecordNotFound
36 36 render_404
37 37 end
38 38
39 39 # Login request and validation
40 40 def login
41 41 if request.get?
42 42 # Logout user
43 43 self.logged_user = nil
44 44 else
45 45 # Authenticate user
46 46 user = User.try_to_login(params[:username], params[:password])
47 47 if user
48 48 self.logged_user = user
49 49 # generate a key and set cookie if autologin
50 50 if params[:autologin] && Setting.autologin?
51 51 token = Token.create(:user => user, :action => 'autologin')
52 52 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
53 53 end
54 54 redirect_back_or_default :controller => 'my', :action => 'page'
55 55 else
56 56 flash.now[:error] = l(:notice_account_invalid_creditentials)
57 57 end
58 58 end
59 rescue User::OnTheFlyCreationFailure
60 flash.now[:error] = 'Redmine could not retrieve the required information from the LDAP to create your account. Please, contact your Redmine administrator.'
59 61 end
60 62
61 63 # Log out current user and redirect to welcome page
62 64 def logout
63 65 cookies.delete :autologin
64 66 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged?
65 67 self.logged_user = nil
66 68 redirect_to home_url
67 69 end
68 70
69 71 # Enable user to choose a new password
70 72 def lost_password
71 73 redirect_to(home_url) && return unless Setting.lost_password?
72 74 if params[:token]
73 75 @token = Token.find_by_action_and_value("recovery", params[:token])
74 76 redirect_to(home_url) && return unless @token and !@token.expired?
75 77 @user = @token.user
76 78 if request.post?
77 79 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
78 80 if @user.save
79 81 @token.destroy
80 82 flash[:notice] = l(:notice_account_password_updated)
81 83 redirect_to :action => 'login'
82 84 return
83 85 end
84 86 end
85 87 render :template => "account/password_recovery"
86 88 return
87 89 else
88 90 if request.post?
89 91 user = User.find_by_mail(params[:mail])
90 92 # user not found in db
91 93 flash.now[:error] = l(:notice_account_unknown_email) and return unless user
92 94 # user uses an external authentification
93 95 flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id
94 96 # create a new token for password recovery
95 97 token = Token.new(:user => user, :action => "recovery")
96 98 if token.save
97 99 Mailer.deliver_lost_password(token)
98 100 flash[:notice] = l(:notice_account_lost_email_sent)
99 101 redirect_to :action => 'login'
100 102 return
101 103 end
102 104 end
103 105 end
104 106 end
105 107
106 108 # User self-registration
107 109 def register
108 110 redirect_to(home_url) && return unless Setting.self_registration?
109 111 if request.get?
110 112 @user = User.new(:language => Setting.default_language)
111 113 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) }
112 114 else
113 115 @user = User.new(params[:user])
114 116 @user.admin = false
115 117 @user.login = params[:user][:login]
116 118 @user.status = User::STATUS_REGISTERED
117 119 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
118 120 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x,
119 121 :customized => @user,
120 122 :value => (params["custom_fields"] ? params["custom_fields"][x.id.to_s] : nil)) }
121 123 @user.custom_values = @custom_values
122 124 case Setting.self_registration
123 125 when '1'
124 126 # Email activation
125 127 token = Token.new(:user => @user, :action => "register")
126 128 if @user.save and token.save
127 129 Mailer.deliver_register(token)
128 130 flash[:notice] = l(:notice_account_register_done)
129 131 redirect_to :action => 'login'
130 132 end
131 133 when '3'
132 134 # Automatic activation
133 135 @user.status = User::STATUS_ACTIVE
134 136 if @user.save
135 137 flash[:notice] = l(:notice_account_activated)
136 138 redirect_to :action => 'login'
137 139 end
138 140 else
139 141 # Manual activation by the administrator
140 142 if @user.save
141 143 # Sends an email to the administrators
142 144 Mailer.deliver_account_activation_request(@user)
143 145 flash[:notice] = l(:notice_account_pending)
144 146 redirect_to :action => 'login'
145 147 end
146 148 end
147 149 end
148 150 end
149 151
150 152 # Token based account activation
151 153 def activate
152 154 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
153 155 token = Token.find_by_action_and_value('register', params[:token])
154 156 redirect_to(home_url) && return unless token and !token.expired?
155 157 user = token.user
156 158 redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
157 159 user.status = User::STATUS_ACTIVE
158 160 if user.save
159 161 token.destroy
160 162 flash[:notice] = l(:notice_account_activated)
161 163 end
162 164 redirect_to :action => 'login'
163 165 end
164 166
165 167 private
166 168 def logged_user=(user)
167 169 if user && user.is_a?(User)
168 170 User.current = user
169 171 session[:user_id] = user.id
170 172 else
171 173 User.current = User.anonymous
172 174 session[:user_id] = nil
173 175 end
174 176 end
175 177 end
@@ -1,286 +1,291
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 "digest/sha1"
19 19
20 20 class User < ActiveRecord::Base
21
22 class OnTheFlyCreationFailure < Exception; end
23
21 24 # Account statuses
22 25 STATUS_ANONYMOUS = 0
23 26 STATUS_ACTIVE = 1
24 27 STATUS_REGISTERED = 2
25 28 STATUS_LOCKED = 3
26 29
27 30 USER_FORMATS = {
28 31 :firstname_lastname => '#{firstname} #{lastname}',
29 32 :firstname => '#{firstname}',
30 33 :lastname_firstname => '#{lastname} #{firstname}',
31 34 :lastname_coma_firstname => '#{lastname}, #{firstname}',
32 35 :username => '#{login}'
33 36 }
34 37
35 38 has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name", :dependent => :delete_all
36 39 has_many :projects, :through => :memberships
37 40 has_many :custom_values, :dependent => :delete_all, :as => :customized
38 41 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
39 42 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
40 43 has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
41 44 belongs_to :auth_source
42 45
43 46 attr_accessor :password, :password_confirmation
44 47 attr_accessor :last_before_login_on
45 48 # Prevents unauthorized assignments
46 49 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
47 50
48 51 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
49 52 validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }
50 53 validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }
51 54 # Login must contain lettres, numbers, underscores only
52 55 validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
53 56 validates_length_of :login, :maximum => 30
54 57 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
55 58 validates_length_of :firstname, :lastname, :maximum => 30
56 59 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
57 60 validates_length_of :mail, :maximum => 60, :allow_nil => true
58 61 validates_length_of :password, :minimum => 4, :allow_nil => true
59 62 validates_confirmation_of :password, :allow_nil => true
60 63 validates_associated :custom_values, :on => :update
61 64
62 65 def before_create
63 66 self.mail_notification = false
64 67 true
65 68 end
66 69
67 70 def before_save
68 71 # update hashed_password if password was set
69 72 self.hashed_password = User.hash_password(self.password) if self.password
70 73 end
71 74
72 75 def self.active
73 76 with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
74 77 yield
75 78 end
76 79 end
77 80
78 81 def self.find_active(*args)
79 82 active do
80 83 find(*args)
81 84 end
82 85 end
83 86
84 87 # Returns the user that matches provided login and password, or nil
85 88 def self.try_to_login(login, password)
86 89 # Make sure no one can sign in with an empty password
87 90 return nil if password.to_s.empty?
88 91 user = find(:first, :conditions => ["login=?", login])
89 92 if user
90 93 # user is already in local database
91 94 return nil if !user.active?
92 95 if user.auth_source
93 96 # user has an external authentication method
94 97 return nil unless user.auth_source.authenticate(login, password)
95 98 else
96 99 # authentication with local password
97 100 return nil unless User.hash_password(password) == user.hashed_password
98 101 end
99 102 else
100 103 # user is not yet registered, try to authenticate with available sources
101 104 attrs = AuthSource.authenticate(login, password)
102 105 if attrs
103 106 onthefly = new(*attrs)
104 107 onthefly.login = login
105 108 onthefly.language = Setting.default_language
106 109 if onthefly.save
107 110 user = find(:first, :conditions => ["login=?", login])
108 logger.info("User '#{user.login}' created on the fly.") if logger
111 logger.info("User '#{user.login}' created from the LDAP") if logger
112 else
113 logger.error("User '#{onthefly.login}' found in LDAP but could not be created (#{onthefly.errors.full_messages.join(', ')})") if logger
114 raise OnTheFlyCreationFailure.new
109 115 end
110 116 end
111 117 end
112 118 user.update_attribute(:last_login_on, Time.now) if user
113 119 user
114
115 120 rescue => text
116 121 raise text
117 122 end
118 123
119 124 # Return user's full name for display
120 125 def name(formatter = nil)
121 126 f = USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname]
122 127 eval '"' + f + '"'
123 128 end
124 129
125 130 def active?
126 131 self.status == STATUS_ACTIVE
127 132 end
128 133
129 134 def registered?
130 135 self.status == STATUS_REGISTERED
131 136 end
132 137
133 138 def locked?
134 139 self.status == STATUS_LOCKED
135 140 end
136 141
137 142 def check_password?(clear_password)
138 143 User.hash_password(clear_password) == self.hashed_password
139 144 end
140 145
141 146 def pref
142 147 self.preference ||= UserPreference.new(:user => self)
143 148 end
144 149
145 150 def time_zone
146 151 self.pref.time_zone.nil? ? nil : TimeZone[self.pref.time_zone]
147 152 end
148 153
149 154 def wants_comments_in_reverse_order?
150 155 self.pref[:comments_sorting] == 'desc'
151 156 end
152 157
153 158 # Return user's RSS key (a 40 chars long string), used to access feeds
154 159 def rss_key
155 160 token = self.rss_token || Token.create(:user => self, :action => 'feeds')
156 161 token.value
157 162 end
158 163
159 164 # Return an array of project ids for which the user has explicitly turned mail notifications on
160 165 def notified_projects_ids
161 166 @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
162 167 end
163 168
164 169 def notified_project_ids=(ids)
165 170 Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
166 171 Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
167 172 @notified_projects_ids = nil
168 173 notified_projects_ids
169 174 end
170 175
171 176 def self.find_by_rss_key(key)
172 177 token = Token.find_by_value(key)
173 178 token && token.user.active? ? token.user : nil
174 179 end
175 180
176 181 def self.find_by_autologin_key(key)
177 182 token = Token.find_by_action_and_value('autologin', key)
178 183 token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil
179 184 end
180 185
181 186 def <=>(user)
182 187 if user.nil?
183 188 -1
184 189 elsif lastname.to_s.downcase == user.lastname.to_s.downcase
185 190 firstname.to_s.downcase <=> user.firstname.to_s.downcase
186 191 else
187 192 lastname.to_s.downcase <=> user.lastname.to_s.downcase
188 193 end
189 194 end
190 195
191 196 def to_s
192 197 name
193 198 end
194 199
195 200 def logged?
196 201 true
197 202 end
198 203
199 204 # Return user's role for project
200 205 def role_for_project(project)
201 206 # No role on archived projects
202 207 return nil unless project && project.active?
203 208 if logged?
204 209 # Find project membership
205 210 membership = memberships.detect {|m| m.project_id == project.id}
206 211 if membership
207 212 membership.role
208 213 else
209 214 @role_non_member ||= Role.non_member
210 215 end
211 216 else
212 217 @role_anonymous ||= Role.anonymous
213 218 end
214 219 end
215 220
216 221 # Return true if the user is a member of project
217 222 def member_of?(project)
218 223 role_for_project(project).member?
219 224 end
220 225
221 226 # Return true if the user is allowed to do the specified action on project
222 227 # action can be:
223 228 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
224 229 # * a permission Symbol (eg. :edit_project)
225 230 def allowed_to?(action, project, options={})
226 231 if project
227 232 # No action allowed on archived projects
228 233 return false unless project.active?
229 234 # No action allowed on disabled modules
230 235 return false unless project.allows_to?(action)
231 236 # Admin users are authorized for anything else
232 237 return true if admin?
233 238
234 239 role = role_for_project(project)
235 240 return false unless role
236 241 role.allowed_to?(action) && (project.is_public? || role.member?)
237 242
238 243 elsif options[:global]
239 244 # authorize if user has at least one role that has this permission
240 245 roles = memberships.collect {|m| m.role}.uniq
241 246 roles.detect {|r| r.allowed_to?(action)}
242 247 else
243 248 false
244 249 end
245 250 end
246 251
247 252 def self.current=(user)
248 253 @current_user = user
249 254 end
250 255
251 256 def self.current
252 257 @current_user ||= User.anonymous
253 258 end
254 259
255 260 def self.anonymous
256 261 return @anonymous_user if @anonymous_user
257 262 anonymous_user = AnonymousUser.find(:first)
258 263 if anonymous_user.nil?
259 264 anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0)
260 265 raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
261 266 end
262 267 @anonymous_user = anonymous_user
263 268 end
264 269
265 270 private
266 271 # Return password digest
267 272 def self.hash_password(clear_password)
268 273 Digest::SHA1.hexdigest(clear_password || "")
269 274 end
270 275 end
271 276
272 277 class AnonymousUser < User
273 278
274 279 def validate_on_create
275 280 # There should be only one AnonymousUser in the database
276 281 errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first)
277 282 end
278 283
279 284 # Overrides a few properties
280 285 def logged?; false end
281 286 def admin; false end
282 287 def name; 'Anonymous' end
283 288 def mail; nil end
284 289 def time_zone; nil end
285 290 def rss_key; nil end
286 291 end
General Comments 0
You need to be logged in to leave comments. Login now