git-svn-id: http://svn.redmine.org/redmine/trunk@15320 e93f8b46-1217-0410-a6f0-8f06a7374b81tags/3.3.0
@@ -30,6 +30,8 @@ class Group < Principal | |||
validates_length_of :lastname, :maximum => 255 | |||
attr_protected :id | |||
self.valid_statuses = [STATUS_ACTIVE] | |||
before_destroy :remove_references_before_destroy | |||
scope :sorted, lambda { order(:type => :asc, :lastname => :asc) } |
@@ -24,6 +24,8 @@ class Principal < ActiveRecord::Base | |||
STATUS_REGISTERED = 2 | |||
STATUS_LOCKED = 3 | |||
class_attribute :valid_statuses | |||
has_many :members, :foreign_key => 'user_id', :dependent => :destroy | |||
has_many :memberships, | |||
lambda {preload(:project, :roles). | |||
@@ -34,6 +36,8 @@ class Principal < ActiveRecord::Base | |||
has_many :projects, :through => :memberships | |||
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify | |||
validate :validate_status | |||
# Groups and active users | |||
scope :active, lambda { where(:status => STATUS_ACTIVE) } | |||
@@ -178,6 +182,14 @@ class Principal < ActiveRecord::Base | |||
self.lastname ||= '' | |||
true | |||
end | |||
def validate_status | |||
if status_changed? && self.class.valid_statuses.present? | |||
unless self.class.valid_statuses.include?(status) | |||
errors.add :status, :invalid | |||
end | |||
end | |||
end | |||
end | |||
require_dependency "user" |
@@ -119,6 +119,8 @@ class User < Principal | |||
end | |||
end | |||
self.valid_statuses = [STATUS_ACTIVE, STATUS_REGISTERED, STATUS_LOCKED] | |||
before_validation :instantiate_email_address | |||
before_create :set_mail_notification | |||
before_save :generate_password_if_needed, :update_hashed_password | |||
@@ -872,6 +874,8 @@ end | |||
class AnonymousUser < User | |||
validate :validate_anonymous_uniqueness, :on => :create | |||
self.valid_statuses = [STATUS_ANONYMOUS] | |||
def validate_anonymous_uniqueness | |||
# There should be only one AnonymousUser in the database | |||
errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? |
@@ -52,6 +52,14 @@ class UserTest < ActiveSupport::TestCase | |||
assert_kind_of User, @jsmith | |||
end | |||
def test_should_validate_status | |||
user = User.new | |||
user.status = 0 | |||
assert !user.save | |||
assert_include I18n.translate('activerecord.errors.messages.invalid'), user.errors[:status] | |||
end | |||
def test_mail_should_be_stripped | |||
u = User.new | |||
u.mail = " foo@bar.com " |