Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## user excerpt
- require 'digest/sha1'
- class User < ActiveRecord::Base
- # Virtual attribute for the unencrypted password
- attr_accessor :password
- validates_presence_of :firstname, :lastname
- validates_presence_of :login, :email
- validates_presence_of :password, :if => :password_required?
- validates_presence_of :password_confirmation, :if => :password_required?
- validates_length_of :password, :within => 4..40, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_length_of :login, :within => 2..40
- validates_length_of :email, :within => 3..100
- validates_uniqueness_of :login, :email, :case_sensitive => false
- validates_email_format_of :email
- validates_presence_of :locale
- before_save :encrypt_password
- before_create :make_activation_code
- attr_accessible :title, :firstname, :lastname, :login, :email, :password, :password_confirmation, :locale
- # Activates the user in the database.
- def activate
- @activated = true
- self.activated_at = Time.now.utc
- self.activation_code = nil
- save(false)
- end
- # Locks this user by setting its deleted-attribute to the current time.
- def lock
- self.deleted = Time.now
- save(false)
- end
- # Unlocks this user.
- def unlock
- self.deleted = nil
- save(false)
- end
- # Checks if this user has been activated yet.
- def active?
- # the existence of an activation code means they have not activated yet
- activation_code.nil?
- end
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- def self.authenticate(login, password)
- u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL and deleted IS NULL', login] # need to get the salt
- u && u.authenticated?(password) ? u : nil
- end
- # Encrypts some data with the salt.
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- # Encrypts the password with the user salt
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- # Checks if the given password matches either the normal password or the
- # temporary password and returns true, otherwise false.
- def authenticated?(password)
- epw = encrypt(password)
- case epw
- when temp_crypted_password
- self.crypted_password = temp_crypted_password
- self.temp_crypted_password = nil
- save(false)
- true
- when crypted_password
- unless temp_crypted_password.nil?
- self.temp_crypted_password = nil
- save(false)
- end
- true
- else
- false
- end
- end
- # Generates a password with eight characters from the current time, the user's
- # login and some random values.
- def gen_temp_password
- temp_password = random_password
- self.temp_crypted_password = encrypt(temp_password)
- save(false)
- temp_password
- end
- def random_password
- Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{rand.to_s}--#{login}--")[0...8]
- end
- ....
- end
- ## controller
- # Renders newpw.html.erb
- def newpw
- end
- # Generates a temporary password and sends it to the user's email-address.
- def createnewpw
- @user = User.find_by_login(params[:login])
- if @user
- temp_password = @user.gen_temp_password
- UserMailer.deliver_temp_password(@user, temp_password)
- flash[:notice] = 'A new password was mailed to your mail address.'.t
- else
- flash[:notice] = 'No such user!'.t
- end
- redirect_to(login_url)
- end
Add Comment
Please, Sign In to add comment