summaryrefslogtreecommitdiffstats
path: root/redmine/app/models/user.rb
diff options
context:
space:
mode:
Diffstat (limited to 'redmine/app/models/user.rb')
-rw-r--r--redmine/app/models/user.rb88
1 files changed, 38 insertions, 50 deletions
diff --git a/redmine/app/models/user.rb b/redmine/app/models/user.rb
index 1bc1b5802..e0adbb0df 100644
--- a/redmine/app/models/user.rb
+++ b/redmine/app/models/user.rb
@@ -18,58 +18,46 @@
require "digest/sha1"
class User < ActiveRecord::Base
- has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :dependent => true
+ has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :dependent => true
- attr_accessor :password
- attr_accessor :last_before_login_on
- # Prevents unauthorized assignments
- attr_protected :admin
+ attr_accessor :password, :password_confirmation
+ attr_accessor :last_before_login_on
+ # Prevents unauthorized assignments
+ attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
- validates_presence_of :login, :firstname, :lastname, :mail
- validates_uniqueness_of :login, :mail
-
- # Login must contain lettres, numbers, underscores only
- validates_format_of :login, :with => /^[a-z0-9_]+$/i
- validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
+ validates_presence_of :login, :firstname, :lastname, :mail
+ validates_uniqueness_of :login, :mail
+ # Login must contain lettres, numbers, underscores only
+ validates_format_of :login, :with => /^[a-z0-9_]+$/i
+ validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
+ # Password length between 4 and 12
+ validates_length_of :password, :in => 4..12, :allow_nil => true
+ validates_confirmation_of :password, :allow_nil => true
+
+ def before_save
+ # update hashed_password if password was set
+ self.hashed_password = User.hash_password(self.password) if self.password
+ end
- def before_create
- self.hashed_password = User.hash_password(self.password)
- end
+ # Returns the user that matches provided login and password, or nil
+ def self.try_to_login(login, password)
+ user = find(:first, :conditions => ["login=? and hashed_password=? and locked=?", login, User.hash_password(password), false])
+ if user
+ user.last_before_login_on = user.last_login_on
+ user.update_attribute(:last_login_on, Time.now)
+ end
+ user
+ end
- def after_create
- @password = nil
- end
+ # Return user's full name for display
+ def display_name
+ firstname + " " + lastname
+ end
- # Returns the user that matches user's login and password
- def try_to_login
- @user = User.login(self.login, self.password)
- unless @user.nil?
- @user.last_before_login_on = @user.last_login_on
- @user.update_attribute(:last_login_on, DateTime.now)
- end
- @user
- end
-
- # Return user's full name for display
- def display_name
- firstname + " " + lastname #+ (self.admin ? " (Admin)" : "" )
- end
+ def check_password?(clear_password)
+ User.hash_password(clear_password) == self.hashed_password
+ end
- # Returns the user that matches the given login and password
- def self.login(login, password)
- hashed_password = hash_password(password || "")
- find(:first,
- :conditions => ["login = ? and hashed_password = ? and locked = ?", login, hashed_password, false])
- end
-
- def check_password?(clear_password)
- User.hash_password(clear_password) == self.hashed_password
- end
-
- def change_password(current_password, new_password)
- self.hashed_password = User.hash_password(new_password)
- save
- end
def role_for_project(project_id)
@role_for_projects ||=
@@ -82,8 +70,8 @@ class User < ActiveRecord::Base
end
private
- # Return password digest
- def self.hash_password(clear_password)
- Digest::SHA1.hexdigest(clear_password)
- end
+ # Return password digest
+ def self.hash_password(clear_password)
+ Digest::SHA1.hexdigest(clear_password || "")
+ end
end