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