Sha256: 43b1698f6986f025ddba7fce901adca0f00f08aaef457b816669c90c7f47bc11

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 KB

Contents

require 'digest/sha1'
class User < ActiveRecord::Base
  include Lockdown::Helper
  has_and_belongs_to_many :user_groups
  belongs_to :profile
  
  # Virtual attributes
  attr_accessor :password

  validates_presence_of     :login
  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 => 3..40
  validates_uniqueness_of   :login, :case_sensitive => false
  
  validates_presence_of :profile
	validates_associated	:profile
  
	before_save :prepare_for_save

  attr_accessible :login, :password, :password_confirmation
  
  # 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 = ?', 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

  def authenticated?(password)
    crypted_password == encrypt(password)
  end
  
   def email
    self.profile.email
  end
  
  def full_name
    self.profile.first_name + " " + self.profile.last_name
  end
  
  protected

	def prepare_for_save
		encrypt_password
		self.profile.save
	end
      
	def encrypt_password
		return if password.blank?
		if new_record?
			self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") 
		end
		self.crypted_password = encrypt(password)
	end
    
	def password_required?
		(crypted_password.blank? || !password.blank?)
	end
    
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
lockdown-0.3.11 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.12 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.14 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.13 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.1 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.10 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.5 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.6 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.7 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.8 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.9 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.15 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.4.0 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.2 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.3 rails_generators/lockdown_all/templates/app/models/user.rb
lockdown-0.3.4 rails_generators/lockdown_all/templates/app/models/user.rb