comparison app/models/.svn/text-base/user.rb.svn-base @ 117:af80e5618e9b redmine-1.1

* Update to Redmine 1.1-stable branch (Redmine SVN rev 4707)
author Chris Cannam
date Thu, 13 Jan 2011 12:53:21 +0000
parents 94944d00e43c
children cd2282d2aa55
comparison
equal deleted inserted replaced
39:150ceac17a8d 117:af80e5618e9b
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require "digest/sha1" 18 require "digest/sha1"
19 19
20 class User < Principal 20 class User < Principal
21 21 include Redmine::SafeAttributes
22
22 # Account statuses 23 # Account statuses
23 STATUS_ANONYMOUS = 0 24 STATUS_ANONYMOUS = 0
24 STATUS_ACTIVE = 1 25 STATUS_ACTIVE = 1
25 STATUS_REGISTERED = 2 26 STATUS_REGISTERED = 2
26 STATUS_LOCKED = 3 27 STATUS_LOCKED = 3
32 :lastname_coma_firstname => '#{lastname}, #{firstname}', 33 :lastname_coma_firstname => '#{lastname}, #{firstname}',
33 :username => '#{login}' 34 :username => '#{login}'
34 } 35 }
35 36
36 MAIL_NOTIFICATION_OPTIONS = [ 37 MAIL_NOTIFICATION_OPTIONS = [
37 [:all, :label_user_mail_option_all], 38 ['all', :label_user_mail_option_all],
38 [:selected, :label_user_mail_option_selected], 39 ['selected', :label_user_mail_option_selected],
39 [:none, :label_user_mail_option_none], 40 ['only_my_events', :label_user_mail_option_only_my_events],
40 [:only_my_events, :label_user_mail_option_only_my_events], 41 ['only_assigned', :label_user_mail_option_only_assigned],
41 [:only_assigned, :label_user_mail_option_only_assigned], 42 ['only_owner', :label_user_mail_option_only_owner],
42 [:only_owner, :label_user_mail_option_only_owner] 43 ['none', :label_user_mail_option_none]
43 ] 44 ]
44 45
45 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)},
46 :after_remove => Proc.new {|user, group| group.user_removed(user)} 47 :after_remove => Proc.new {|user, group| group.user_removed(user)}
47 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify 48 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
48 has_many :changesets, :dependent => :nullify 49 has_many :changesets, :dependent => :nullify
57 acts_as_customizable 58 acts_as_customizable
58 59
59 attr_accessor :password, :password_confirmation 60 attr_accessor :password, :password_confirmation
60 attr_accessor :last_before_login_on 61 attr_accessor :last_before_login_on
61 # Prevents unauthorized assignments 62 # Prevents unauthorized assignments
62 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password, :group_ids 63 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
63 64
64 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } 65 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
65 validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false 66 validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false
66 validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false 67 validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false
67 # Login must contain lettres, numbers, underscores only 68 # Login must contain lettres, numbers, underscores only
70 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-\.]*$/i 71 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-\.]*$/i
71 validates_length_of :firstname, :lastname, :maximum => 30 72 validates_length_of :firstname, :lastname, :maximum => 30
72 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true 73 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 74 validates_length_of :mail, :maximum => 60, :allow_nil => true
74 validates_confirmation_of :password, :allow_nil => true 75 validates_confirmation_of :password, :allow_nil => true
76 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
75 77
76 def before_create 78 def before_create
77 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? 79 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
78 true 80 true
79 end 81 end
262 # Only users that belong to more than 1 project can select projects for which they are notified 264 # Only users that belong to more than 1 project can select projects for which they are notified
263 def valid_notification_options 265 def valid_notification_options
264 # Note that @user.membership.size would fail since AR ignores 266 # Note that @user.membership.size would fail since AR ignores
265 # :include association option when doing a count 267 # :include association option when doing a count
266 if memberships.length < 1 268 if memberships.length < 1
267 MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == :selected} 269 MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == 'selected'}
268 else 270 else
269 MAIL_NOTIFICATION_OPTIONS 271 MAIL_NOTIFICATION_OPTIONS
270 end 272 end
271 end 273 end
272 274
388 # Is the user allowed to do the specified action on any project? 390 # Is the user allowed to do the specified action on any project?
389 # See allowed_to? for the actions and valid options. 391 # See allowed_to? for the actions and valid options.
390 def allowed_to_globally?(action, options) 392 def allowed_to_globally?(action, options)
391 allowed_to?(action, nil, options.reverse_merge(:global => true)) 393 allowed_to?(action, nil, options.reverse_merge(:global => true))
392 end 394 end
395
396 safe_attributes 'login',
397 'firstname',
398 'lastname',
399 'mail',
400 'mail_notification',
401 'language',
402 'custom_field_values',
403 'custom_fields',
404 'identity_url'
405
406 safe_attributes 'status',
407 'auth_source_id',
408 :if => lambda {|user, current_user| current_user.admin?}
409
410 safe_attributes 'group_ids',
411 :if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
393 412
394 # Utility method to help check if a user should be notified about an 413 # Utility method to help check if a user should be notified about an
395 # event. 414 # event.
396 # 415 #
397 # TODO: only supports Issue events currently 416 # TODO: only supports Issue events currently
398 def notify_about?(object) 417 def notify_about?(object)
399 case mail_notification.to_sym 418 case mail_notification
400 when :all 419 when 'all'
401 true 420 true
402 when :selected 421 when 'selected'
403 # Handled by the Project 422 # Handled by the Project
404 when :none 423 when 'none'
405 false 424 false
406 when :only_my_events 425 when 'only_my_events'
407 if object.is_a?(Issue) && (object.author == self || object.assigned_to == self) 426 if object.is_a?(Issue) && (object.author == self || object.assigned_to == self)
408 true 427 true
409 else 428 else
410 false 429 false
411 end 430 end
412 when :only_assigned 431 when 'only_assigned'
413 if object.is_a?(Issue) && object.assigned_to == self 432 if object.is_a?(Issue) && object.assigned_to == self
414 true 433 true
415 else 434 else
416 false 435 false
417 end 436 end
418 when :only_owner 437 when 'only_owner'
419 if object.is_a?(Issue) && object.author == self 438 if object.is_a?(Issue) && object.author == self
420 true 439 true
421 else 440 else
422 false 441 false
423 end 442 end