##// END OF EJS Templates
Prevent LDAP authentication with empty password related problems....
Jean-Philippe Lang -
r1217:3a75b6771fa1
parent child
Show More
@@ -1,275 +1,277
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 21 # Account statuses
22 22 STATUS_ANONYMOUS = 0
23 23 STATUS_ACTIVE = 1
24 24 STATUS_REGISTERED = 2
25 25 STATUS_LOCKED = 3
26 26
27 27 USER_FORMATS = {
28 28 :firstname_lastname => '#{firstname} #{lastname}',
29 29 :firstname => '#{firstname}',
30 30 :lastname_firstname => '#{lastname} #{firstname}',
31 31 :lastname_coma_firstname => '#{lastname}, #{firstname}',
32 32 :username => '#{login}'
33 33 }
34 34
35 35 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 36 has_many :projects, :through => :memberships
37 37 has_many :custom_values, :dependent => :delete_all, :as => :customized
38 38 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
39 39 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
40 40 has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
41 41 belongs_to :auth_source
42 42
43 43 attr_accessor :password, :password_confirmation
44 44 attr_accessor :last_before_login_on
45 45 # Prevents unauthorized assignments
46 46 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
47 47
48 48 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
49 49 validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }
50 50 validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }
51 51 # Login must contain lettres, numbers, underscores only
52 52 validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
53 53 validates_length_of :login, :maximum => 30
54 54 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
55 55 validates_length_of :firstname, :lastname, :maximum => 30
56 56 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
57 57 validates_length_of :mail, :maximum => 60, :allow_nil => true
58 58 validates_length_of :password, :minimum => 4, :allow_nil => true
59 59 validates_confirmation_of :password, :allow_nil => true
60 60 validates_associated :custom_values, :on => :update
61 61
62 62 def before_create
63 63 self.mail_notification = false
64 64 true
65 65 end
66 66
67 67 def before_save
68 68 # update hashed_password if password was set
69 69 self.hashed_password = User.hash_password(self.password) if self.password
70 70 end
71 71
72 72 def self.active
73 73 with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
74 74 yield
75 75 end
76 76 end
77 77
78 78 def self.find_active(*args)
79 79 active do
80 80 find(*args)
81 81 end
82 82 end
83 83
84 84 # Returns the user that matches provided login and password, or nil
85 85 def self.try_to_login(login, password)
86 # Make sure no one can sign in with an empty password
87 return nil if password.to_s.empty?
86 88 user = find(:first, :conditions => ["login=?", login])
87 89 if user
88 90 # user is already in local database
89 91 return nil if !user.active?
90 92 if user.auth_source
91 93 # user has an external authentication method
92 94 return nil unless user.auth_source.authenticate(login, password)
93 95 else
94 96 # authentication with local password
95 97 return nil unless User.hash_password(password) == user.hashed_password
96 98 end
97 99 else
98 100 # user is not yet registered, try to authenticate with available sources
99 101 attrs = AuthSource.authenticate(login, password)
100 102 if attrs
101 103 onthefly = new(*attrs)
102 104 onthefly.login = login
103 105 onthefly.language = Setting.default_language
104 106 if onthefly.save
105 107 user = find(:first, :conditions => ["login=?", login])
106 108 logger.info("User '#{user.login}' created on the fly.") if logger
107 109 end
108 110 end
109 111 end
110 112 user.update_attribute(:last_login_on, Time.now) if user
111 113 user
112 114
113 115 rescue => text
114 116 raise text
115 117 end
116 118
117 119 # Return user's full name for display
118 120 def name(formatter = nil)
119 121 f = USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname]
120 122 eval '"' + f + '"'
121 123 end
122 124
123 125 def active?
124 126 self.status == STATUS_ACTIVE
125 127 end
126 128
127 129 def registered?
128 130 self.status == STATUS_REGISTERED
129 131 end
130 132
131 133 def locked?
132 134 self.status == STATUS_LOCKED
133 135 end
134 136
135 137 def check_password?(clear_password)
136 138 User.hash_password(clear_password) == self.hashed_password
137 139 end
138 140
139 141 def pref
140 142 self.preference ||= UserPreference.new(:user => self)
141 143 end
142 144
143 145 def time_zone
144 146 self.pref.time_zone.nil? ? nil : TimeZone[self.pref.time_zone]
145 147 end
146 148
147 149 def wants_comments_in_reverse_order?
148 150 self.pref[:comments_sorting] == 'desc'
149 151 end
150 152
151 153 # Return user's RSS key (a 40 chars long string), used to access feeds
152 154 def rss_key
153 155 token = self.rss_token || Token.create(:user => self, :action => 'feeds')
154 156 token.value
155 157 end
156 158
157 159 # Return an array of project ids for which the user has explicitly turned mail notifications on
158 160 def notified_projects_ids
159 161 @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
160 162 end
161 163
162 164 def notified_project_ids=(ids)
163 165 Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
164 166 Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
165 167 @notified_projects_ids = nil
166 168 notified_projects_ids
167 169 end
168 170
169 171 def self.find_by_rss_key(key)
170 172 token = Token.find_by_value(key)
171 173 token && token.user.active? ? token.user : nil
172 174 end
173 175
174 176 def self.find_by_autologin_key(key)
175 177 token = Token.find_by_action_and_value('autologin', key)
176 178 token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil
177 179 end
178 180
179 181 def <=>(user)
180 182 if user.nil?
181 183 -1
182 184 elsif lastname.to_s.downcase == user.lastname.to_s.downcase
183 185 firstname.to_s.downcase <=> user.firstname.to_s.downcase
184 186 else
185 187 lastname.to_s.downcase <=> user.lastname.to_s.downcase
186 188 end
187 189 end
188 190
189 191 def to_s
190 192 name
191 193 end
192 194
193 195 def logged?
194 196 true
195 197 end
196 198
197 199 # Return user's role for project
198 200 def role_for_project(project)
199 201 # No role on archived projects
200 202 return nil unless project && project.active?
201 203 if logged?
202 204 # Find project membership
203 205 membership = memberships.detect {|m| m.project_id == project.id}
204 206 if membership
205 207 membership.role
206 208 else
207 209 @role_non_member ||= Role.non_member
208 210 end
209 211 else
210 212 @role_anonymous ||= Role.anonymous
211 213 end
212 214 end
213 215
214 216 # Return true if the user is a member of project
215 217 def member_of?(project)
216 218 role_for_project(project).member?
217 219 end
218 220
219 221 # Return true if the user is allowed to do the specified action on project
220 222 # action can be:
221 223 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
222 224 # * a permission Symbol (eg. :edit_project)
223 225 def allowed_to?(action, project)
224 226 # No action allowed on archived projects
225 227 return false unless project.active?
226 228 # No action allowed on disabled modules
227 229 return false unless project.allows_to?(action)
228 230 # Admin users are authorized for anything else
229 231 return true if admin?
230 232
231 233 role = role_for_project(project)
232 234 return false unless role
233 235 role.allowed_to?(action) && (project.is_public? || role.member?)
234 236 end
235 237
236 238 def self.current=(user)
237 239 @current_user = user
238 240 end
239 241
240 242 def self.current
241 243 @current_user ||= User.anonymous
242 244 end
243 245
244 246 def self.anonymous
245 247 return @anonymous_user if @anonymous_user
246 248 anonymous_user = AnonymousUser.find(:first)
247 249 if anonymous_user.nil?
248 250 anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0)
249 251 raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
250 252 end
251 253 @anonymous_user = anonymous_user
252 254 end
253 255
254 256 private
255 257 # Return password digest
256 258 def self.hash_password(clear_password)
257 259 Digest::SHA1.hexdigest(clear_password || "")
258 260 end
259 261 end
260 262
261 263 class AnonymousUser < User
262 264
263 265 def validate_on_create
264 266 # There should be only one AnonymousUser in the database
265 267 errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first)
266 268 end
267 269
268 270 # Overrides a few properties
269 271 def logged?; false end
270 272 def admin; false end
271 273 def name; 'Anonymous' end
272 274 def mail; nil end
273 275 def time_zone; nil end
274 276 def rss_key; nil end
275 277 end
General Comments 0
You need to be logged in to leave comments. Login now