@@ -30,6 +30,8 class Group < Principal | |||||
30 | validates_length_of :lastname, :maximum => 255 |
|
30 | validates_length_of :lastname, :maximum => 255 | |
31 | attr_protected :id |
|
31 | attr_protected :id | |
32 |
|
32 | |||
|
33 | self.valid_statuses = [STATUS_ACTIVE] | |||
|
34 | ||||
33 | before_destroy :remove_references_before_destroy |
|
35 | before_destroy :remove_references_before_destroy | |
34 |
|
36 | |||
35 | scope :sorted, lambda { order(:type => :asc, :lastname => :asc) } |
|
37 | scope :sorted, lambda { order(:type => :asc, :lastname => :asc) } |
@@ -24,6 +24,8 class Principal < ActiveRecord::Base | |||||
24 | STATUS_REGISTERED = 2 |
|
24 | STATUS_REGISTERED = 2 | |
25 | STATUS_LOCKED = 3 |
|
25 | STATUS_LOCKED = 3 | |
26 |
|
26 | |||
|
27 | class_attribute :valid_statuses | |||
|
28 | ||||
27 | has_many :members, :foreign_key => 'user_id', :dependent => :destroy |
|
29 | has_many :members, :foreign_key => 'user_id', :dependent => :destroy | |
28 | has_many :memberships, |
|
30 | has_many :memberships, | |
29 | lambda {preload(:project, :roles). |
|
31 | lambda {preload(:project, :roles). | |
@@ -34,6 +36,8 class Principal < ActiveRecord::Base | |||||
34 | has_many :projects, :through => :memberships |
|
36 | has_many :projects, :through => :memberships | |
35 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
|
37 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify | |
36 |
|
38 | |||
|
39 | validate :validate_status | |||
|
40 | ||||
37 | # Groups and active users |
|
41 | # Groups and active users | |
38 | scope :active, lambda { where(:status => STATUS_ACTIVE) } |
|
42 | scope :active, lambda { where(:status => STATUS_ACTIVE) } | |
39 |
|
43 | |||
@@ -178,6 +182,14 class Principal < ActiveRecord::Base | |||||
178 | self.lastname ||= '' |
|
182 | self.lastname ||= '' | |
179 | true |
|
183 | true | |
180 | end |
|
184 | end | |
|
185 | ||||
|
186 | def validate_status | |||
|
187 | if status_changed? && self.class.valid_statuses.present? | |||
|
188 | unless self.class.valid_statuses.include?(status) | |||
|
189 | errors.add :status, :invalid | |||
|
190 | end | |||
|
191 | end | |||
|
192 | end | |||
181 | end |
|
193 | end | |
182 |
|
194 | |||
183 | require_dependency "user" |
|
195 | require_dependency "user" |
@@ -119,6 +119,8 class User < Principal | |||||
119 | end |
|
119 | end | |
120 | end |
|
120 | end | |
121 |
|
121 | |||
|
122 | self.valid_statuses = [STATUS_ACTIVE, STATUS_REGISTERED, STATUS_LOCKED] | |||
|
123 | ||||
122 | before_validation :instantiate_email_address |
|
124 | before_validation :instantiate_email_address | |
123 | before_create :set_mail_notification |
|
125 | before_create :set_mail_notification | |
124 | before_save :generate_password_if_needed, :update_hashed_password |
|
126 | before_save :generate_password_if_needed, :update_hashed_password | |
@@ -872,6 +874,8 end | |||||
872 | class AnonymousUser < User |
|
874 | class AnonymousUser < User | |
873 | validate :validate_anonymous_uniqueness, :on => :create |
|
875 | validate :validate_anonymous_uniqueness, :on => :create | |
874 |
|
876 | |||
|
877 | self.valid_statuses = [STATUS_ANONYMOUS] | |||
|
878 | ||||
875 | def validate_anonymous_uniqueness |
|
879 | def validate_anonymous_uniqueness | |
876 | # There should be only one AnonymousUser in the database |
|
880 | # There should be only one AnonymousUser in the database | |
877 | errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? |
|
881 | errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? |
@@ -52,6 +52,14 class UserTest < ActiveSupport::TestCase | |||||
52 | assert_kind_of User, @jsmith |
|
52 | assert_kind_of User, @jsmith | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
|
55 | def test_should_validate_status | |||
|
56 | user = User.new | |||
|
57 | user.status = 0 | |||
|
58 | ||||
|
59 | assert !user.save | |||
|
60 | assert_include I18n.translate('activerecord.errors.messages.invalid'), user.errors[:status] | |||
|
61 | end | |||
|
62 | ||||
55 | def test_mail_should_be_stripped |
|
63 | def test_mail_should_be_stripped | |
56 | u = User.new |
|
64 | u = User.new | |
57 | u.mail = " foo@bar.com " |
|
65 | u.mail = " foo@bar.com " |
General Comments 0
You need to be logged in to leave comments.
Login now