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