Mercurial > hg > soundsoftware-site
comparison app/models/user.rb @ 128:07fa8a8b56a8
Update to Redmine trunk rev 4732
author | Chris Cannam |
---|---|
date | Wed, 19 Jan 2011 15:04:22 +0000 |
parents | 8661b858af72 |
children | 5e974759e8b2 0579821a129a |
comparison
equal
deleted
inserted
replaced
119:8661b858af72 | 128:07fa8a8b56a8 |
---|---|
46 has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, | 46 has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, |
47 :after_remove => Proc.new {|user, group| group.user_removed(user)} | 47 :after_remove => Proc.new {|user, group| group.user_removed(user)} |
48 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify | 48 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
49 has_many :changesets, :dependent => :nullify | 49 has_many :changesets, :dependent => :nullify |
50 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' | 50 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
51 has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'" | 51 has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" |
52 has_one :api_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='api'" | 52 has_one :api_token, :class_name => 'Token', :conditions => "action='api'" |
53 belongs_to :auth_source | 53 belongs_to :auth_source |
54 | 54 |
55 # Active non-anonymous users scope | 55 # Active non-anonymous users scope |
56 named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}" | 56 named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}" |
57 | 57 |
72 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true | 72 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true |
73 validates_length_of :mail, :maximum => 60, :allow_nil => true | 73 validates_length_of :mail, :maximum => 60, :allow_nil => true |
74 validates_confirmation_of :password, :allow_nil => true | 74 validates_confirmation_of :password, :allow_nil => true |
75 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true | 75 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true |
76 | 76 |
77 before_destroy :remove_references_before_destroy | |
78 | |
77 def before_create | 79 def before_create |
78 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? | 80 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
79 true | 81 true |
80 end | 82 end |
81 | 83 |
258 Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? | 260 Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? |
259 @notified_projects_ids = nil | 261 @notified_projects_ids = nil |
260 notified_projects_ids | 262 notified_projects_ids |
261 end | 263 end |
262 | 264 |
265 def valid_notification_options | |
266 self.class.valid_notification_options(self) | |
267 end | |
268 | |
263 # Only users that belong to more than 1 project can select projects for which they are notified | 269 # Only users that belong to more than 1 project can select projects for which they are notified |
264 def valid_notification_options | 270 def self.valid_notification_options(user=nil) |
265 # Note that @user.membership.size would fail since AR ignores | 271 # Note that @user.membership.size would fail since AR ignores |
266 # :include association option when doing a count | 272 # :include association option when doing a count |
267 if memberships.length < 1 | 273 if user.nil? || user.memberships.length < 1 |
268 MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == 'selected'} | 274 MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} |
269 else | 275 else |
270 MAIL_NOTIFICATION_OPTIONS | 276 MAIL_NOTIFICATION_OPTIONS |
271 end | 277 end |
272 end | 278 end |
273 | 279 |
471 errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) | 477 errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) |
472 end | 478 end |
473 end | 479 end |
474 | 480 |
475 private | 481 private |
482 | |
483 # Removes references that are not handled by associations | |
484 # Things that are not deleted are reassociated with the anonymous user | |
485 def remove_references_before_destroy | |
486 return if self.id.nil? | |
487 | |
488 substitute = User.anonymous | |
489 Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
490 Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
491 Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
492 Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] | |
493 Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | |
494 JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] | |
495 JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] | |
496 Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
497 News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
498 # Remove private queries and keep public ones | |
499 Query.delete_all ['user_id = ? AND is_public = ?', id, false] | |
500 Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | |
501 TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | |
502 Token.delete_all ['user_id = ?', id] | |
503 Watcher.delete_all ['user_id = ?', id] | |
504 WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
505 WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | |
506 end | |
476 | 507 |
477 # Return password digest | 508 # Return password digest |
478 def self.hash_password(clear_password) | 509 def self.hash_password(clear_password) |
479 Digest::SHA1.hexdigest(clear_password || "") | 510 Digest::SHA1.hexdigest(clear_password || "") |
480 end | 511 end |
496 def admin; false end | 527 def admin; false end |
497 def name(*args); I18n.t(:label_user_anonymous) end | 528 def name(*args); I18n.t(:label_user_anonymous) end |
498 def mail; nil end | 529 def mail; nil end |
499 def time_zone; nil end | 530 def time_zone; nil end |
500 def rss_key; nil end | 531 def rss_key; nil end |
532 | |
533 # Anonymous user can not be destroyed | |
534 def destroy | |
535 false | |
536 end | |
501 end | 537 end |