Mercurial > hg > soundsoftware-site
comparison app/models/user.rb @ 1464:261b3d9a4903 redmine-2.4
Update to Redmine 2.4 branch rev 12663
author | Chris Cannam |
---|---|
date | Tue, 14 Jan 2014 14:37:42 +0000 |
parents | 433d4f72a19b |
children | 51364c0cd58f e248c7af89ec |
comparison
equal
deleted
inserted
replaced
1296:038ba2d95de8 | 1464:261b3d9a4903 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 Jean-Philippe Lang | 2 # Copyright (C) 2006-2013 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
17 | 17 |
18 require "digest/sha1" | 18 require "digest/sha1" |
19 | 19 |
20 class User < Principal | 20 class User < Principal |
21 include Redmine::SafeAttributes | 21 include Redmine::SafeAttributes |
22 | |
23 # Account statuses | |
24 STATUS_ANONYMOUS = 0 | |
25 STATUS_ACTIVE = 1 | |
26 STATUS_REGISTERED = 2 | |
27 STATUS_LOCKED = 3 | |
28 | 22 |
29 # Different ways of displaying/sorting users | 23 # Different ways of displaying/sorting users |
30 USER_FORMATS = { | 24 USER_FORMATS = { |
31 :firstname_lastname => { | 25 :firstname_lastname => { |
32 :string => '#{firstname} #{lastname}', | 26 :string => '#{firstname} #{lastname}', |
80 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' | 74 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
81 has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" | 75 has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" |
82 has_one :api_token, :class_name => 'Token', :conditions => "action='api'" | 76 has_one :api_token, :class_name => 'Token', :conditions => "action='api'" |
83 belongs_to :auth_source | 77 belongs_to :auth_source |
84 | 78 |
85 scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" | 79 scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") } |
86 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} } | 80 scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } |
87 | 81 |
88 acts_as_customizable | 82 acts_as_customizable |
89 | 83 |
90 attr_accessor :password, :password_confirmation | 84 attr_accessor :password, :password_confirmation, :generate_password |
91 attr_accessor :last_before_login_on | 85 attr_accessor :last_before_login_on |
92 # Prevents unauthorized assignments | 86 # Prevents unauthorized assignments |
93 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password | 87 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password |
94 | 88 |
95 LOGIN_LENGTH_LIMIT = 60 | 89 LOGIN_LENGTH_LIMIT = 60 |
96 MAIL_LENGTH_LIMIT = 60 | 90 MAIL_LENGTH_LIMIT = 60 |
97 | 91 |
98 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } | 92 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } |
99 validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false | 93 validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false |
100 validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false | 94 validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false |
101 # Login must contain lettres, numbers, underscores only | 95 # Login must contain letters, numbers, underscores only |
102 validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i | 96 validates_format_of :login, :with => /\A[a-z0-9_\-@\.]*\z/i |
103 validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT | 97 validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT |
104 validates_length_of :firstname, :lastname, :maximum => 30 | 98 validates_length_of :firstname, :lastname, :maximum => 30 |
105 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_blank => true | 99 validates_format_of :mail, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true |
106 validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true | 100 validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true |
107 validates_confirmation_of :password, :allow_nil => true | 101 validates_confirmation_of :password, :allow_nil => true |
108 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true | 102 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true |
109 validate :validate_password_length | 103 validate :validate_password_length |
110 | 104 |
111 before_create :set_mail_notification | 105 before_create :set_mail_notification |
112 before_save :update_hashed_password | 106 before_save :generate_password_if_needed, :update_hashed_password |
113 before_destroy :remove_references_before_destroy | 107 before_destroy :remove_references_before_destroy |
108 after_save :update_notified_project_ids | |
114 | 109 |
115 scope :in_group, lambda {|group| | 110 scope :in_group, lambda {|group| |
116 group_id = group.is_a?(Group) ? group.id : group.to_i | 111 group_id = group.is_a?(Group) ? group.id : group.to_i |
117 where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) | 112 where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) |
118 } | 113 } |
119 scope :not_in_group, lambda {|group| | 114 scope :not_in_group, lambda {|group| |
120 group_id = group.is_a?(Group) ? group.id : group.to_i | 115 group_id = group.is_a?(Group) ? group.id : group.to_i |
121 where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) | 116 where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) |
122 } | 117 } |
118 scope :sorted, lambda { order(*User.fields_for_order_statement)} | |
123 | 119 |
124 def set_mail_notification | 120 def set_mail_notification |
125 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? | 121 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
126 true | 122 true |
127 end | 123 end |
131 if self.password && self.auth_source_id.blank? | 127 if self.password && self.auth_source_id.blank? |
132 salt_password(password) | 128 salt_password(password) |
133 end | 129 end |
134 end | 130 end |
135 | 131 |
132 alias :base_reload :reload | |
136 def reload(*args) | 133 def reload(*args) |
137 @name = nil | 134 @name = nil |
138 @projects_by_role = nil | 135 @projects_by_role = nil |
139 super | 136 @membership_by_project_id = nil |
137 @notified_projects_ids = nil | |
138 @notified_projects_ids_changed = false | |
139 @builtin_role = nil | |
140 base_reload(*args) | |
140 end | 141 end |
141 | 142 |
142 def mail=(arg) | 143 def mail=(arg) |
143 write_attribute(:mail, arg.to_s.strip) | 144 write_attribute(:mail, arg.to_s.strip) |
144 end | 145 end |
148 write_attribute(:identity_url, '') | 149 write_attribute(:identity_url, '') |
149 else | 150 else |
150 begin | 151 begin |
151 write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) | 152 write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) |
152 rescue OpenIdAuthentication::InvalidOpenId | 153 rescue OpenIdAuthentication::InvalidOpenId |
153 # Invlaid url, don't save | 154 # Invalid url, don't save |
154 end | 155 end |
155 end | 156 end |
156 self.read_attribute(:identity_url) | 157 self.read_attribute(:identity_url) |
157 end | 158 end |
158 | 159 |
159 # Returns the user that matches provided login and password, or nil | 160 # Returns the user that matches provided login and password, or nil |
160 def self.try_to_login(login, password) | 161 def self.try_to_login(login, password, active_only=true) |
161 login = login.to_s | 162 login = login.to_s |
162 password = password.to_s | 163 password = password.to_s |
163 | 164 |
164 # Make sure no one can sign in with an empty password | 165 # Make sure no one can sign in with an empty login or password |
165 return nil if password.empty? | 166 return nil if login.empty? || password.empty? |
166 user = find_by_login(login) | 167 user = find_by_login(login) |
167 if user | 168 if user |
168 # user is already in local database | 169 # user is already in local database |
169 return nil if !user.active? | 170 return nil unless user.check_password?(password) |
170 if user.auth_source | 171 return nil if !user.active? && active_only |
171 # user has an external authentication method | |
172 return nil unless user.auth_source.authenticate(login, password) | |
173 else | |
174 # authentication with local password | |
175 return nil unless user.check_password?(password) | |
176 end | |
177 else | 172 else |
178 # user is not yet registered, try to authenticate with available sources | 173 # user is not yet registered, try to authenticate with available sources |
179 attrs = AuthSource.authenticate(login, password) | 174 attrs = AuthSource.authenticate(login, password) |
180 if attrs | 175 if attrs |
181 user = new(attrs) | 176 user = new(attrs) |
185 user.reload | 180 user.reload |
186 logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source | 181 logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source |
187 end | 182 end |
188 end | 183 end |
189 end | 184 end |
190 user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? | 185 user.update_column(:last_login_on, Time.now) if user && !user.new_record? && user.active? |
191 user | 186 user |
192 rescue => text | 187 rescue => text |
193 raise text | 188 raise text |
194 end | 189 end |
195 | 190 |
196 # Returns the user who matches the given autologin +key+ or nil | 191 # Returns the user who matches the given autologin +key+ or nil |
197 def self.try_to_autologin(key) | 192 def self.try_to_autologin(key) |
198 tokens = Token.find_all_by_action_and_value('autologin', key.to_s) | 193 user = Token.find_active_user('autologin', key, Setting.autologin.to_i) |
199 # Make sure there's only 1 token that matches the key | 194 if user |
200 if tokens.size == 1 | 195 user.update_column(:last_login_on, Time.now) |
201 token = tokens.first | 196 user |
202 if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active? | |
203 token.user.update_attribute(:last_login_on, Time.now) | |
204 token.user | |
205 end | |
206 end | 197 end |
207 end | 198 end |
208 | 199 |
209 def self.name_formatter(formatter = nil) | 200 def self.name_formatter(formatter = nil) |
210 USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] | 201 USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] |
287 def change_password_allowed? | 278 def change_password_allowed? |
288 return true if auth_source.nil? | 279 return true if auth_source.nil? |
289 return auth_source.allow_password_changes? | 280 return auth_source.allow_password_changes? |
290 end | 281 end |
291 | 282 |
292 # Generate and set a random password. Useful for automated user creation | 283 def must_change_password? |
293 # Based on Token#generate_token_value | 284 must_change_passwd? && change_password_allowed? |
294 # | 285 end |
295 def random_password | 286 |
287 def generate_password? | |
288 generate_password == '1' || generate_password == true | |
289 end | |
290 | |
291 # Generate and set a random password on given length | |
292 def random_password(length=40) | |
296 chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a | 293 chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a |
294 chars -= %w(0 O 1 l) | |
297 password = '' | 295 password = '' |
298 40.times { |i| password << chars[rand(chars.size-1)] } | 296 length.times {|i| password << chars[SecureRandom.random_number(chars.size)] } |
299 self.password = password | 297 self.password = password |
300 self.password_confirmation = password | 298 self.password_confirmation = password |
301 self | 299 self |
302 end | 300 end |
303 | 301 |
333 def notified_projects_ids | 331 def notified_projects_ids |
334 @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) | 332 @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) |
335 end | 333 end |
336 | 334 |
337 def notified_project_ids=(ids) | 335 def notified_project_ids=(ids) |
338 Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) | 336 @notified_projects_ids_changed = true |
339 Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? | 337 @notified_projects_ids = ids |
340 @notified_projects_ids = nil | 338 end |
341 notified_projects_ids | 339 |
342 end | 340 # Updates per project notifications (after_save callback) |
341 def update_notified_project_ids | |
342 if @notified_projects_ids_changed | |
343 ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : []) | |
344 members.update_all(:mail_notification => false) | |
345 members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any? | |
346 end | |
347 end | |
348 private :update_notified_project_ids | |
343 | 349 |
344 def valid_notification_options | 350 def valid_notification_options |
345 self.class.valid_notification_options(self) | 351 self.class.valid_notification_options(self) |
346 end | 352 end |
347 | 353 |
357 end | 363 end |
358 | 364 |
359 # Find a user account by matching the exact login and then a case-insensitive | 365 # Find a user account by matching the exact login and then a case-insensitive |
360 # version. Exact matches will be given priority. | 366 # version. Exact matches will be given priority. |
361 def self.find_by_login(login) | 367 def self.find_by_login(login) |
362 # First look for an exact match | 368 if login.present? |
363 user = where(:login => login).all.detect {|u| u.login == login} | 369 login = login.to_s |
364 unless user | 370 # First look for an exact match |
365 # Fail over to case-insensitive if none was found | 371 user = where(:login => login).all.detect {|u| u.login == login} |
366 user = where("LOWER(login) = ?", login.to_s.downcase).first | 372 unless user |
367 end | 373 # Fail over to case-insensitive if none was found |
368 user | 374 user = where("LOWER(login) = ?", login.downcase).first |
375 end | |
376 user | |
377 end | |
369 end | 378 end |
370 | 379 |
371 def self.find_by_rss_key(key) | 380 def self.find_by_rss_key(key) |
372 token = Token.find_by_action_and_value('feeds', key.to_s) | 381 Token.find_active_user('feeds', key) |
373 token && token.user.active? ? token.user : nil | |
374 end | 382 end |
375 | 383 |
376 def self.find_by_api_key(key) | 384 def self.find_by_api_key(key) |
377 token = Token.find_by_action_and_value('api', key.to_s) | 385 Token.find_active_user('api', key) |
378 token && token.user.active? ? token.user : nil | |
379 end | 386 end |
380 | 387 |
381 # Makes find_by_mail case-insensitive | 388 # Makes find_by_mail case-insensitive |
382 def self.find_by_mail(mail) | 389 def self.find_by_mail(mail) |
383 where("LOWER(mail) = ?", mail.to_s.downcase).first | 390 where("LOWER(mail) = ?", mail.to_s.downcase).first |
427 | 434 |
428 def anonymous? | 435 def anonymous? |
429 !logged? | 436 !logged? |
430 end | 437 end |
431 | 438 |
439 # Returns user's membership for the given project | |
440 # or nil if the user is not a member of project | |
441 def membership(project) | |
442 project_id = project.is_a?(Project) ? project.id : project | |
443 | |
444 @membership_by_project_id ||= Hash.new {|h, project_id| | |
445 h[project_id] = memberships.where(:project_id => project_id).first | |
446 } | |
447 @membership_by_project_id[project_id] | |
448 end | |
449 | |
450 # Returns the user's bult-in role | |
451 def builtin_role | |
452 @builtin_role ||= Role.non_member | |
453 end | |
454 | |
432 # Return user's roles for project | 455 # Return user's roles for project |
433 def roles_for_project(project) | 456 def roles_for_project(project) |
434 roles = [] | 457 roles = [] |
435 # No role on archived projects | 458 # No role on archived projects |
436 return roles if project.nil? || project.archived? | 459 return roles if project.nil? || project.archived? |
437 if logged? | 460 if membership = membership(project) |
438 # Find project membership | 461 roles = membership.roles |
439 membership = memberships.detect {|m| m.project_id == project.id} | 462 else |
440 if membership | 463 roles << builtin_role |
441 roles = membership.roles | |
442 else | |
443 @role_non_member ||= Role.non_member | |
444 roles << @role_non_member | |
445 end | |
446 else | |
447 @role_anonymous ||= Role.anonymous | |
448 roles << @role_anonymous | |
449 end | 464 end |
450 roles | 465 roles |
451 end | 466 end |
452 | 467 |
453 # Return true if the user is a member of project | 468 # Return true if the user is a member of project |
454 def member_of?(project) | 469 def member_of?(project) |
455 !roles_for_project(project).detect {|role| role.member?}.nil? | 470 projects.to_a.include?(project) |
456 end | 471 end |
457 | 472 |
458 # Returns a hash of user's projects grouped by roles | 473 # Returns a hash of user's projects grouped by roles |
459 def projects_by_role | 474 def projects_by_role |
460 return @projects_by_role if @projects_by_role | 475 return @projects_by_role if @projects_by_role |
535 # See allowed_to? for the actions and valid options. | 550 # See allowed_to? for the actions and valid options. |
536 def allowed_to_globally?(action, options, &block) | 551 def allowed_to_globally?(action, options, &block) |
537 allowed_to?(action, nil, options.reverse_merge(:global => true), &block) | 552 allowed_to?(action, nil, options.reverse_merge(:global => true), &block) |
538 end | 553 end |
539 | 554 |
540 # Returns true if the user is allowed to delete his own account | 555 # Returns true if the user is allowed to delete the user's own account |
541 def own_account_deletable? | 556 def own_account_deletable? |
542 Setting.unsubscribe? && | 557 Setting.unsubscribe? && |
543 (!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?) | 558 (!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?) |
544 end | 559 end |
545 | 560 |
546 safe_attributes 'login', | 561 safe_attributes 'login', |
547 'firstname', | 562 'firstname', |
548 'lastname', | 563 'lastname', |
549 'mail', | 564 'mail', |
550 'mail_notification', | 565 'mail_notification', |
566 'notified_project_ids', | |
551 'language', | 567 'language', |
552 'custom_field_values', | 568 'custom_field_values', |
553 'custom_fields', | 569 'custom_fields', |
554 'identity_url' | 570 'identity_url' |
555 | 571 |
556 safe_attributes 'status', | 572 safe_attributes 'status', |
557 'auth_source_id', | 573 'auth_source_id', |
574 'generate_password', | |
575 'must_change_passwd', | |
558 :if => lambda {|user, current_user| current_user.admin?} | 576 :if => lambda {|user, current_user| current_user.admin?} |
559 | 577 |
560 safe_attributes 'group_ids', | 578 safe_attributes 'group_ids', |
561 :if => lambda {|user, current_user| current_user.admin? && !user.new_record?} | 579 :if => lambda {|user, current_user| current_user.admin? && !user.new_record?} |
562 | 580 |
563 # Utility method to help check if a user should be notified about an | 581 # Utility method to help check if a user should be notified about an |
564 # event. | 582 # event. |
565 # | 583 # |
566 # TODO: only supports Issue events currently | 584 # TODO: only supports Issue events currently |
567 def notify_about?(object) | 585 def notify_about?(object) |
568 case mail_notification | 586 if mail_notification == 'all' |
569 when 'all' | |
570 true | 587 true |
571 when 'selected' | 588 elsif mail_notification.blank? || mail_notification == 'none' |
572 # user receives notifications for created/assigned issues on unselected projects | 589 false |
573 if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was)) | 590 else |
591 case object | |
592 when Issue | |
593 case mail_notification | |
594 when 'selected', 'only_my_events' | |
595 # user receives notifications for created/assigned issues on unselected projects | |
596 object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) | |
597 when 'only_assigned' | |
598 is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) | |
599 when 'only_owner' | |
600 object.author == self | |
601 end | |
602 when News | |
603 # always send to project members except when mail_notification is set to 'none' | |
574 true | 604 true |
575 else | |
576 false | |
577 end | 605 end |
578 when 'none' | |
579 false | |
580 when 'only_my_events' | |
581 if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was)) | |
582 true | |
583 else | |
584 false | |
585 end | |
586 when 'only_assigned' | |
587 if object.is_a?(Issue) && (is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was)) | |
588 true | |
589 else | |
590 false | |
591 end | |
592 when 'only_owner' | |
593 if object.is_a?(Issue) && object.author == self | |
594 true | |
595 else | |
596 false | |
597 end | |
598 else | |
599 false | |
600 end | 606 end |
601 end | 607 end |
602 | 608 |
603 def self.current=(user) | 609 def self.current=(user) |
604 @current_user = user | 610 Thread.current[:current_user] = user |
605 end | 611 end |
606 | 612 |
607 def self.current | 613 def self.current |
608 @current_user ||= User.anonymous | 614 Thread.current[:current_user] ||= User.anonymous |
609 end | 615 end |
610 | 616 |
611 # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only | 617 # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only |
612 # one anonymous user per database. | 618 # one anonymous user per database. |
613 def self.anonymous | 619 def self.anonymous |
634 end | 640 end |
635 | 641 |
636 protected | 642 protected |
637 | 643 |
638 def validate_password_length | 644 def validate_password_length |
645 return if password.blank? && generate_password? | |
639 # Password length validation based on setting | 646 # Password length validation based on setting |
640 if !password.nil? && password.size < Setting.password_min_length.to_i | 647 if !password.nil? && password.size < Setting.password_min_length.to_i |
641 errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) | 648 errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) |
642 end | 649 end |
643 end | 650 end |
644 | 651 |
645 private | 652 private |
653 | |
654 def generate_password_if_needed | |
655 if generate_password? && auth_source.nil? | |
656 length = [Setting.password_min_length.to_i + 2, 10].max | |
657 random_password(length) | |
658 end | |
659 end | |
646 | 660 |
647 # Removes references that are not handled by associations | 661 # Removes references that are not handled by associations |
648 # Things that are not deleted are reassociated with the anonymous user | 662 # Things that are not deleted are reassociated with the anonymous user |
649 def remove_references_before_destroy | 663 def remove_references_before_destroy |
650 return if self.id.nil? | 664 return if self.id.nil? |
658 JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] | 672 JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] |
659 JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] | 673 JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] |
660 Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | 674 Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
661 News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | 675 News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
662 # Remove private queries and keep public ones | 676 # Remove private queries and keep public ones |
663 ::Query.delete_all ['user_id = ? AND is_public = ?', id, false] | 677 ::Query.delete_all ['user_id = ? AND visibility = ?', id, ::Query::VISIBILITY_PRIVATE] |
664 ::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | 678 ::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
665 TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | 679 TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
666 Token.delete_all ['user_id = ?', id] | 680 Token.delete_all ['user_id = ?', id] |
667 Watcher.delete_all ['user_id = ?', id] | 681 Watcher.delete_all ['user_id = ?', id] |
668 WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | 682 WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
684 class AnonymousUser < User | 698 class AnonymousUser < User |
685 validate :validate_anonymous_uniqueness, :on => :create | 699 validate :validate_anonymous_uniqueness, :on => :create |
686 | 700 |
687 def validate_anonymous_uniqueness | 701 def validate_anonymous_uniqueness |
688 # There should be only one AnonymousUser in the database | 702 # There should be only one AnonymousUser in the database |
689 errors.add :base, 'An anonymous user already exists.' if AnonymousUser.find(:first) | 703 errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? |
690 end | 704 end |
691 | 705 |
692 def available_custom_fields | 706 def available_custom_fields |
693 [] | 707 [] |
694 end | 708 end |
703 | 717 |
704 def pref | 718 def pref |
705 UserPreference.new(:user => self) | 719 UserPreference.new(:user => self) |
706 end | 720 end |
707 | 721 |
722 # Returns the user's bult-in role | |
723 def builtin_role | |
724 @builtin_role ||= Role.anonymous | |
725 end | |
726 | |
727 def membership(*args) | |
728 nil | |
729 end | |
730 | |
731 def member_of?(*args) | |
732 false | |
733 end | |
734 | |
708 # Anonymous user can not be destroyed | 735 # Anonymous user can not be destroyed |
709 def destroy | 736 def destroy |
710 false | 737 false |
711 end | 738 end |
712 end | 739 end |