Chris@0
|
1 # Redmine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require "digest/sha1"
|
Chris@0
|
19
|
Chris@0
|
20 class User < Principal
|
Chris@119
|
21 include Redmine::SafeAttributes
|
Chris@119
|
22
|
Chris@0
|
23 # Account statuses
|
Chris@0
|
24 STATUS_ANONYMOUS = 0
|
Chris@0
|
25 STATUS_ACTIVE = 1
|
Chris@0
|
26 STATUS_REGISTERED = 2
|
Chris@0
|
27 STATUS_LOCKED = 3
|
Chris@0
|
28
|
Chris@0
|
29 USER_FORMATS = {
|
Chris@0
|
30 :firstname_lastname => '#{firstname} #{lastname}',
|
Chris@0
|
31 :firstname => '#{firstname}',
|
Chris@0
|
32 :lastname_firstname => '#{lastname} #{firstname}',
|
Chris@0
|
33 :lastname_coma_firstname => '#{lastname}, #{firstname}',
|
Chris@0
|
34 :username => '#{login}'
|
Chris@0
|
35 }
|
Chris@0
|
36
|
chris@37
|
37 MAIL_NOTIFICATION_OPTIONS = [
|
Chris@119
|
38 ['all', :label_user_mail_option_all],
|
Chris@119
|
39 ['selected', :label_user_mail_option_selected],
|
Chris@119
|
40 ['only_my_events', :label_user_mail_option_only_my_events],
|
Chris@119
|
41 ['only_assigned', :label_user_mail_option_only_assigned],
|
Chris@119
|
42 ['only_owner', :label_user_mail_option_only_owner],
|
Chris@119
|
43 ['none', :label_user_mail_option_none]
|
Chris@119
|
44 ]
|
chris@37
|
45
|
Chris@0
|
46 has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)},
|
Chris@0
|
47 :after_remove => Proc.new {|user, group| group.user_removed(user)}
|
Chris@0
|
48 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
|
Chris@0
|
49 has_many :changesets, :dependent => :nullify
|
Chris@0
|
50 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
|
Chris@128
|
51 has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'"
|
Chris@128
|
52 has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
|
Chris@0
|
53 belongs_to :auth_source
|
Chris@0
|
54
|
Chris@0
|
55 # Active non-anonymous users scope
|
Chris@0
|
56 named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
|
Chris@0
|
57
|
Chris@0
|
58 acts_as_customizable
|
Chris@0
|
59
|
Chris@0
|
60 attr_accessor :password, :password_confirmation
|
Chris@0
|
61 attr_accessor :last_before_login_on
|
Chris@0
|
62 # Prevents unauthorized assignments
|
Chris@119
|
63 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
|
Chris@0
|
64
|
Chris@0
|
65 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
|
Chris@0
|
66 validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false
|
Chris@0
|
67 validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false
|
Chris@0
|
68 # Login must contain lettres, numbers, underscores only
|
Chris@0
|
69 validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
|
Chris@0
|
70 validates_length_of :login, :maximum => 30
|
Chris@0
|
71 validates_length_of :firstname, :lastname, :maximum => 30
|
Chris@0
|
72 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
|
Chris@0
|
73 validates_length_of :mail, :maximum => 60, :allow_nil => true
|
Chris@0
|
74 validates_confirmation_of :password, :allow_nil => true
|
Chris@119
|
75 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
|
Chris@0
|
76
|
Chris@128
|
77 before_destroy :remove_references_before_destroy
|
Chris@128
|
78
|
Chris@0
|
79 def before_create
|
chris@37
|
80 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
|
Chris@0
|
81 true
|
Chris@0
|
82 end
|
Chris@0
|
83
|
Chris@0
|
84 def before_save
|
Chris@0
|
85 # update hashed_password if password was set
|
Chris@245
|
86 if self.password && self.auth_source_id.blank?
|
Chris@245
|
87 salt_password(password)
|
Chris@245
|
88 end
|
Chris@0
|
89 end
|
Chris@0
|
90
|
Chris@0
|
91 def reload(*args)
|
Chris@0
|
92 @name = nil
|
Chris@0
|
93 super
|
Chris@0
|
94 end
|
Chris@0
|
95
|
Chris@1
|
96 def mail=(arg)
|
Chris@1
|
97 write_attribute(:mail, arg.to_s.strip)
|
Chris@1
|
98 end
|
Chris@1
|
99
|
Chris@0
|
100 def identity_url=(url)
|
Chris@0
|
101 if url.blank?
|
Chris@0
|
102 write_attribute(:identity_url, '')
|
Chris@0
|
103 else
|
Chris@0
|
104 begin
|
Chris@0
|
105 write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url))
|
Chris@0
|
106 rescue OpenIdAuthentication::InvalidOpenId
|
Chris@0
|
107 # Invlaid url, don't save
|
Chris@0
|
108 end
|
Chris@0
|
109 end
|
Chris@0
|
110 self.read_attribute(:identity_url)
|
Chris@0
|
111 end
|
Chris@0
|
112
|
Chris@0
|
113 # Returns the user that matches provided login and password, or nil
|
Chris@0
|
114 def self.try_to_login(login, password)
|
Chris@0
|
115 # Make sure no one can sign in with an empty password
|
Chris@0
|
116 return nil if password.to_s.empty?
|
Chris@0
|
117 user = find_by_login(login)
|
Chris@0
|
118 if user
|
Chris@0
|
119 # user is already in local database
|
Chris@0
|
120 return nil if !user.active?
|
Chris@0
|
121 if user.auth_source
|
Chris@0
|
122 # user has an external authentication method
|
Chris@0
|
123 return nil unless user.auth_source.authenticate(login, password)
|
Chris@0
|
124 else
|
Chris@0
|
125 # authentication with local password
|
Chris@245
|
126 return nil unless user.check_password?(password)
|
Chris@0
|
127 end
|
Chris@0
|
128 else
|
Chris@0
|
129 # user is not yet registered, try to authenticate with available sources
|
Chris@0
|
130 attrs = AuthSource.authenticate(login, password)
|
Chris@0
|
131 if attrs
|
Chris@0
|
132 user = new(attrs)
|
Chris@0
|
133 user.login = login
|
Chris@0
|
134 user.language = Setting.default_language
|
Chris@0
|
135 if user.save
|
Chris@0
|
136 user.reload
|
Chris@0
|
137 logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
|
Chris@0
|
138 end
|
Chris@0
|
139 end
|
Chris@0
|
140 end
|
Chris@0
|
141 user.update_attribute(:last_login_on, Time.now) if user && !user.new_record?
|
Chris@0
|
142 user
|
Chris@0
|
143 rescue => text
|
Chris@0
|
144 raise text
|
Chris@0
|
145 end
|
Chris@0
|
146
|
Chris@0
|
147 # Returns the user who matches the given autologin +key+ or nil
|
Chris@0
|
148 def self.try_to_autologin(key)
|
Chris@0
|
149 tokens = Token.find_all_by_action_and_value('autologin', key)
|
Chris@0
|
150 # Make sure there's only 1 token that matches the key
|
Chris@0
|
151 if tokens.size == 1
|
Chris@0
|
152 token = tokens.first
|
Chris@0
|
153 if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active?
|
Chris@0
|
154 token.user.update_attribute(:last_login_on, Time.now)
|
Chris@0
|
155 token.user
|
Chris@0
|
156 end
|
Chris@0
|
157 end
|
Chris@0
|
158 end
|
Chris@0
|
159
|
Chris@0
|
160 # Return user's full name for display
|
Chris@0
|
161 def name(formatter = nil)
|
Chris@0
|
162 if formatter
|
Chris@0
|
163 eval('"' + (USER_FORMATS[formatter] || USER_FORMATS[:firstname_lastname]) + '"')
|
Chris@0
|
164 else
|
Chris@0
|
165 @name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"')
|
Chris@0
|
166 end
|
Chris@0
|
167 end
|
Chris@0
|
168
|
Chris@0
|
169 def active?
|
Chris@0
|
170 self.status == STATUS_ACTIVE
|
Chris@0
|
171 end
|
Chris@0
|
172
|
Chris@0
|
173 def registered?
|
Chris@0
|
174 self.status == STATUS_REGISTERED
|
Chris@0
|
175 end
|
Chris@0
|
176
|
Chris@0
|
177 def locked?
|
Chris@0
|
178 self.status == STATUS_LOCKED
|
Chris@0
|
179 end
|
Chris@0
|
180
|
Chris@14
|
181 def activate
|
Chris@14
|
182 self.status = STATUS_ACTIVE
|
Chris@14
|
183 end
|
Chris@14
|
184
|
Chris@14
|
185 def register
|
Chris@14
|
186 self.status = STATUS_REGISTERED
|
Chris@14
|
187 end
|
Chris@14
|
188
|
Chris@14
|
189 def lock
|
Chris@14
|
190 self.status = STATUS_LOCKED
|
Chris@14
|
191 end
|
Chris@14
|
192
|
Chris@14
|
193 def activate!
|
Chris@14
|
194 update_attribute(:status, STATUS_ACTIVE)
|
Chris@14
|
195 end
|
Chris@14
|
196
|
Chris@14
|
197 def register!
|
Chris@14
|
198 update_attribute(:status, STATUS_REGISTERED)
|
Chris@14
|
199 end
|
Chris@14
|
200
|
Chris@14
|
201 def lock!
|
Chris@14
|
202 update_attribute(:status, STATUS_LOCKED)
|
Chris@14
|
203 end
|
Chris@14
|
204
|
Chris@245
|
205 # Returns true if +clear_password+ is the correct user's password, otherwise false
|
Chris@0
|
206 def check_password?(clear_password)
|
Chris@0
|
207 if auth_source_id.present?
|
Chris@0
|
208 auth_source.authenticate(self.login, clear_password)
|
Chris@0
|
209 else
|
Chris@245
|
210 User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password
|
Chris@0
|
211 end
|
Chris@0
|
212 end
|
Chris@245
|
213
|
Chris@245
|
214 # Generates a random salt and computes hashed_password for +clear_password+
|
Chris@245
|
215 # The hashed password is stored in the following form: SHA1(salt + SHA1(password))
|
Chris@245
|
216 def salt_password(clear_password)
|
Chris@245
|
217 self.salt = User.generate_salt
|
Chris@245
|
218 self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}")
|
Chris@245
|
219 end
|
Chris@0
|
220
|
Chris@0
|
221 # Does the backend storage allow this user to change their password?
|
Chris@0
|
222 def change_password_allowed?
|
Chris@0
|
223 return true if auth_source_id.blank?
|
Chris@0
|
224 return auth_source.allow_password_changes?
|
Chris@0
|
225 end
|
Chris@0
|
226
|
Chris@0
|
227 # Generate and set a random password. Useful for automated user creation
|
Chris@0
|
228 # Based on Token#generate_token_value
|
Chris@0
|
229 #
|
Chris@0
|
230 def random_password
|
Chris@0
|
231 chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
|
Chris@0
|
232 password = ''
|
Chris@0
|
233 40.times { |i| password << chars[rand(chars.size-1)] }
|
Chris@0
|
234 self.password = password
|
Chris@0
|
235 self.password_confirmation = password
|
Chris@0
|
236 self
|
Chris@0
|
237 end
|
Chris@0
|
238
|
Chris@0
|
239 def pref
|
Chris@0
|
240 self.preference ||= UserPreference.new(:user => self)
|
Chris@0
|
241 end
|
Chris@0
|
242
|
Chris@0
|
243 def time_zone
|
Chris@0
|
244 @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone])
|
Chris@0
|
245 end
|
Chris@0
|
246
|
Chris@0
|
247 def wants_comments_in_reverse_order?
|
Chris@0
|
248 self.pref[:comments_sorting] == 'desc'
|
Chris@0
|
249 end
|
Chris@0
|
250
|
Chris@0
|
251 # Return user's RSS key (a 40 chars long string), used to access feeds
|
Chris@0
|
252 def rss_key
|
Chris@0
|
253 token = self.rss_token || Token.create(:user => self, :action => 'feeds')
|
Chris@0
|
254 token.value
|
Chris@0
|
255 end
|
Chris@0
|
256
|
Chris@0
|
257 # Return user's API key (a 40 chars long string), used to access the API
|
Chris@0
|
258 def api_key
|
Chris@0
|
259 token = self.api_token || self.create_api_token(:action => 'api')
|
Chris@0
|
260 token.value
|
Chris@0
|
261 end
|
Chris@0
|
262
|
Chris@0
|
263 # Return an array of project ids for which the user has explicitly turned mail notifications on
|
Chris@0
|
264 def notified_projects_ids
|
Chris@0
|
265 @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
|
Chris@0
|
266 end
|
Chris@0
|
267
|
Chris@0
|
268 def notified_project_ids=(ids)
|
Chris@0
|
269 Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
|
Chris@0
|
270 Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
|
Chris@0
|
271 @notified_projects_ids = nil
|
Chris@0
|
272 notified_projects_ids
|
Chris@0
|
273 end
|
Chris@0
|
274
|
Chris@128
|
275 def valid_notification_options
|
Chris@128
|
276 self.class.valid_notification_options(self)
|
Chris@128
|
277 end
|
Chris@128
|
278
|
chris@37
|
279 # Only users that belong to more than 1 project can select projects for which they are notified
|
Chris@128
|
280 def self.valid_notification_options(user=nil)
|
chris@37
|
281 # Note that @user.membership.size would fail since AR ignores
|
chris@37
|
282 # :include association option when doing a count
|
Chris@128
|
283 if user.nil? || user.memberships.length < 1
|
Chris@128
|
284 MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'}
|
chris@37
|
285 else
|
chris@37
|
286 MAIL_NOTIFICATION_OPTIONS
|
chris@37
|
287 end
|
chris@37
|
288 end
|
chris@37
|
289
|
Chris@0
|
290 # Find a user account by matching the exact login and then a case-insensitive
|
Chris@0
|
291 # version. Exact matches will be given priority.
|
Chris@0
|
292 def self.find_by_login(login)
|
Chris@0
|
293 # force string comparison to be case sensitive on MySQL
|
Chris@0
|
294 type_cast = (ActiveRecord::Base.connection.adapter_name == 'MySQL') ? 'BINARY' : ''
|
Chris@0
|
295
|
Chris@0
|
296 # First look for an exact match
|
Chris@0
|
297 user = first(:conditions => ["#{type_cast} login = ?", login])
|
Chris@0
|
298 # Fail over to case-insensitive if none was found
|
Chris@0
|
299 user ||= first(:conditions => ["#{type_cast} LOWER(login) = ?", login.to_s.downcase])
|
Chris@0
|
300 end
|
Chris@0
|
301
|
Chris@0
|
302 def self.find_by_rss_key(key)
|
Chris@0
|
303 token = Token.find_by_value(key)
|
Chris@0
|
304 token && token.user.active? ? token.user : nil
|
Chris@0
|
305 end
|
Chris@0
|
306
|
Chris@0
|
307 def self.find_by_api_key(key)
|
Chris@0
|
308 token = Token.find_by_action_and_value('api', key)
|
Chris@0
|
309 token && token.user.active? ? token.user : nil
|
Chris@0
|
310 end
|
Chris@0
|
311
|
Chris@0
|
312 # Makes find_by_mail case-insensitive
|
Chris@0
|
313 def self.find_by_mail(mail)
|
Chris@0
|
314 find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase])
|
Chris@0
|
315 end
|
Chris@0
|
316
|
Chris@0
|
317 def to_s
|
Chris@0
|
318 name
|
Chris@0
|
319 end
|
Chris@0
|
320
|
Chris@0
|
321 # Returns the current day according to user's time zone
|
Chris@0
|
322 def today
|
Chris@0
|
323 if time_zone.nil?
|
Chris@0
|
324 Date.today
|
Chris@0
|
325 else
|
Chris@0
|
326 Time.now.in_time_zone(time_zone).to_date
|
Chris@0
|
327 end
|
Chris@0
|
328 end
|
Chris@0
|
329
|
Chris@0
|
330 def logged?
|
Chris@0
|
331 true
|
Chris@0
|
332 end
|
Chris@0
|
333
|
Chris@0
|
334 def anonymous?
|
Chris@0
|
335 !logged?
|
Chris@0
|
336 end
|
Chris@0
|
337
|
Chris@0
|
338 # Return user's roles for project
|
Chris@0
|
339 def roles_for_project(project)
|
Chris@0
|
340 roles = []
|
Chris@0
|
341 # No role on archived projects
|
Chris@0
|
342 return roles unless project && project.active?
|
Chris@0
|
343 if logged?
|
Chris@0
|
344 # Find project membership
|
Chris@0
|
345 membership = memberships.detect {|m| m.project_id == project.id}
|
Chris@0
|
346 if membership
|
Chris@0
|
347 roles = membership.roles
|
Chris@0
|
348 else
|
Chris@0
|
349 @role_non_member ||= Role.non_member
|
Chris@0
|
350 roles << @role_non_member
|
Chris@0
|
351 end
|
Chris@0
|
352 else
|
Chris@0
|
353 @role_anonymous ||= Role.anonymous
|
Chris@0
|
354 roles << @role_anonymous
|
Chris@0
|
355 end
|
Chris@0
|
356 roles
|
Chris@0
|
357 end
|
Chris@0
|
358
|
Chris@0
|
359 # Return true if the user is a member of project
|
Chris@0
|
360 def member_of?(project)
|
Chris@0
|
361 !roles_for_project(project).detect {|role| role.member?}.nil?
|
Chris@0
|
362 end
|
Chris@0
|
363
|
chris@37
|
364 # Return true if the user is allowed to do the specified action on a specific context
|
chris@37
|
365 # Action can be:
|
Chris@0
|
366 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
|
Chris@0
|
367 # * a permission Symbol (eg. :edit_project)
|
chris@37
|
368 # Context can be:
|
chris@37
|
369 # * a project : returns true if user is allowed to do the specified action on this project
|
chris@37
|
370 # * a group of projects : returns true if user is allowed on every project
|
chris@37
|
371 # * nil with options[:global] set : check if user has at least one role allowed for this action,
|
chris@37
|
372 # or falls back to Non Member / Anonymous permissions depending if the user is logged
|
chris@37
|
373 def allowed_to?(action, context, options={})
|
chris@37
|
374 if context && context.is_a?(Project)
|
Chris@0
|
375 # No action allowed on archived projects
|
chris@37
|
376 return false unless context.active?
|
Chris@0
|
377 # No action allowed on disabled modules
|
chris@37
|
378 return false unless context.allows_to?(action)
|
Chris@0
|
379 # Admin users are authorized for anything else
|
Chris@0
|
380 return true if admin?
|
Chris@0
|
381
|
chris@37
|
382 roles = roles_for_project(context)
|
Chris@0
|
383 return false unless roles
|
chris@37
|
384 roles.detect {|role| (context.is_public? || role.member?) && role.allowed_to?(action)}
|
Chris@0
|
385
|
chris@37
|
386 elsif context && context.is_a?(Array)
|
chris@37
|
387 # Authorize if user is authorized on every element of the array
|
chris@37
|
388 context.map do |project|
|
chris@37
|
389 allowed_to?(action,project,options)
|
chris@37
|
390 end.inject do |memo,allowed|
|
chris@37
|
391 memo && allowed
|
chris@37
|
392 end
|
Chris@0
|
393 elsif options[:global]
|
Chris@0
|
394 # Admin users are always authorized
|
Chris@0
|
395 return true if admin?
|
Chris@0
|
396
|
Chris@0
|
397 # authorize if user has at least one role that has this permission
|
Chris@0
|
398 roles = memberships.collect {|m| m.roles}.flatten.uniq
|
Chris@0
|
399 roles.detect {|r| r.allowed_to?(action)} || (self.logged? ? Role.non_member.allowed_to?(action) : Role.anonymous.allowed_to?(action))
|
Chris@0
|
400 else
|
Chris@0
|
401 false
|
Chris@0
|
402 end
|
Chris@0
|
403 end
|
chris@22
|
404
|
chris@22
|
405 # Is the user allowed to do the specified action on any project?
|
chris@22
|
406 # See allowed_to? for the actions and valid options.
|
chris@22
|
407 def allowed_to_globally?(action, options)
|
chris@22
|
408 allowed_to?(action, nil, options.reverse_merge(:global => true))
|
chris@22
|
409 end
|
Chris@119
|
410
|
Chris@119
|
411 safe_attributes 'login',
|
Chris@119
|
412 'firstname',
|
Chris@119
|
413 'lastname',
|
Chris@119
|
414 'mail',
|
Chris@119
|
415 'mail_notification',
|
Chris@119
|
416 'language',
|
Chris@119
|
417 'custom_field_values',
|
Chris@119
|
418 'custom_fields',
|
Chris@119
|
419 'identity_url'
|
Chris@119
|
420
|
Chris@119
|
421 safe_attributes 'status',
|
Chris@119
|
422 'auth_source_id',
|
Chris@119
|
423 :if => lambda {|user, current_user| current_user.admin?}
|
Chris@119
|
424
|
Chris@119
|
425 safe_attributes 'group_ids',
|
Chris@119
|
426 :if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
|
Chris@0
|
427
|
chris@37
|
428 # Utility method to help check if a user should be notified about an
|
chris@37
|
429 # event.
|
chris@37
|
430 #
|
chris@37
|
431 # TODO: only supports Issue events currently
|
chris@37
|
432 def notify_about?(object)
|
Chris@119
|
433 case mail_notification
|
Chris@119
|
434 when 'all'
|
chris@37
|
435 true
|
Chris@119
|
436 when 'selected'
|
Chris@210
|
437 # user receives notifications for created/assigned issues on unselected projects
|
Chris@210
|
438 if object.is_a?(Issue) && (object.author == self || object.assigned_to == self)
|
Chris@210
|
439 true
|
Chris@210
|
440 else
|
Chris@210
|
441 false
|
Chris@210
|
442 end
|
Chris@119
|
443 when 'none'
|
chris@37
|
444 false
|
Chris@119
|
445 when 'only_my_events'
|
chris@37
|
446 if object.is_a?(Issue) && (object.author == self || object.assigned_to == self)
|
chris@37
|
447 true
|
chris@37
|
448 else
|
chris@37
|
449 false
|
chris@37
|
450 end
|
Chris@119
|
451 when 'only_assigned'
|
chris@37
|
452 if object.is_a?(Issue) && object.assigned_to == self
|
chris@37
|
453 true
|
chris@37
|
454 else
|
chris@37
|
455 false
|
chris@37
|
456 end
|
Chris@119
|
457 when 'only_owner'
|
chris@37
|
458 if object.is_a?(Issue) && object.author == self
|
chris@37
|
459 true
|
chris@37
|
460 else
|
chris@37
|
461 false
|
chris@37
|
462 end
|
chris@37
|
463 else
|
chris@37
|
464 false
|
chris@37
|
465 end
|
chris@37
|
466 end
|
chris@37
|
467
|
Chris@0
|
468 def self.current=(user)
|
Chris@0
|
469 @current_user = user
|
Chris@0
|
470 end
|
Chris@0
|
471
|
Chris@0
|
472 def self.current
|
Chris@0
|
473 @current_user ||= User.anonymous
|
Chris@0
|
474 end
|
Chris@0
|
475
|
Chris@0
|
476 # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
|
Chris@0
|
477 # one anonymous user per database.
|
Chris@0
|
478 def self.anonymous
|
Chris@0
|
479 anonymous_user = AnonymousUser.find(:first)
|
Chris@0
|
480 if anonymous_user.nil?
|
Chris@0
|
481 anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0)
|
Chris@0
|
482 raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
|
Chris@0
|
483 end
|
Chris@0
|
484 anonymous_user
|
Chris@0
|
485 end
|
Chris@245
|
486
|
Chris@245
|
487 # Salts all existing unsalted passwords
|
Chris@245
|
488 # It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password))
|
Chris@245
|
489 # This method is used in the SaltPasswords migration and is to be kept as is
|
Chris@245
|
490 def self.salt_unsalted_passwords!
|
Chris@245
|
491 transaction do
|
Chris@245
|
492 User.find_each(:conditions => "salt IS NULL OR salt = ''") do |user|
|
Chris@245
|
493 next if user.hashed_password.blank?
|
Chris@245
|
494 salt = User.generate_salt
|
Chris@245
|
495 hashed_password = User.hash_password("#{salt}#{user.hashed_password}")
|
Chris@245
|
496 User.update_all("salt = '#{salt}', hashed_password = '#{hashed_password}'", ["id = ?", user.id] )
|
Chris@245
|
497 end
|
Chris@245
|
498 end
|
Chris@245
|
499 end
|
Chris@0
|
500
|
Chris@0
|
501 protected
|
Chris@0
|
502
|
Chris@0
|
503 def validate
|
Chris@0
|
504 # Password length validation based on setting
|
Chris@0
|
505 if !password.nil? && password.size < Setting.password_min_length.to_i
|
Chris@0
|
506 errors.add(:password, :too_short, :count => Setting.password_min_length.to_i)
|
Chris@0
|
507 end
|
Chris@0
|
508 end
|
Chris@0
|
509
|
Chris@0
|
510 private
|
Chris@128
|
511
|
Chris@128
|
512 # Removes references that are not handled by associations
|
Chris@128
|
513 # Things that are not deleted are reassociated with the anonymous user
|
Chris@128
|
514 def remove_references_before_destroy
|
Chris@128
|
515 return if self.id.nil?
|
Chris@128
|
516
|
Chris@128
|
517 substitute = User.anonymous
|
Chris@128
|
518 Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
519 Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
520 Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
521 Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id]
|
Chris@128
|
522 Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id]
|
Chris@128
|
523 JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s]
|
Chris@128
|
524 JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s]
|
Chris@128
|
525 Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
526 News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
527 # Remove private queries and keep public ones
|
Chris@128
|
528 Query.delete_all ['user_id = ? AND is_public = ?', id, false]
|
Chris@128
|
529 Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id]
|
Chris@128
|
530 TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id]
|
Chris@128
|
531 Token.delete_all ['user_id = ?', id]
|
Chris@128
|
532 Watcher.delete_all ['user_id = ?', id]
|
Chris@128
|
533 WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
534 WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
|
Chris@128
|
535 end
|
Chris@0
|
536
|
Chris@0
|
537 # Return password digest
|
Chris@0
|
538 def self.hash_password(clear_password)
|
Chris@0
|
539 Digest::SHA1.hexdigest(clear_password || "")
|
Chris@0
|
540 end
|
Chris@245
|
541
|
Chris@245
|
542 # Returns a 128bits random salt as a hex string (32 chars long)
|
Chris@245
|
543 def self.generate_salt
|
Chris@245
|
544 ActiveSupport::SecureRandom.hex(16)
|
Chris@245
|
545 end
|
Chris@245
|
546
|
Chris@0
|
547 end
|
Chris@0
|
548
|
Chris@0
|
549 class AnonymousUser < User
|
Chris@0
|
550
|
Chris@0
|
551 def validate_on_create
|
Chris@0
|
552 # There should be only one AnonymousUser in the database
|
Chris@0
|
553 errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first)
|
Chris@0
|
554 end
|
Chris@0
|
555
|
Chris@0
|
556 def available_custom_fields
|
Chris@0
|
557 []
|
Chris@0
|
558 end
|
Chris@0
|
559
|
Chris@0
|
560 # Overrides a few properties
|
Chris@0
|
561 def logged?; false end
|
Chris@0
|
562 def admin; false end
|
Chris@0
|
563 def name(*args); I18n.t(:label_user_anonymous) end
|
Chris@0
|
564 def mail; nil end
|
Chris@0
|
565 def time_zone; nil end
|
Chris@0
|
566 def rss_key; nil end
|
Chris@128
|
567
|
Chris@128
|
568 # Anonymous user can not be destroyed
|
Chris@128
|
569 def destroy
|
Chris@128
|
570 false
|
Chris@128
|
571 end
|
Chris@0
|
572 end
|