Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##
- # @class User
- #
- class User
- include DataMapper::Resource
- property :id, Serial
- property :salt, String
- property :crypted_password, String
- property :first, String
- property :last, String
- property :email, String
- property :role, String
- # virtual-properties used by validators below.
- attr_accessor :password
- attr_accessor :password_confirmation
- 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_presence_of :email
- validates_uniqueness_of :email
- before :save, :generate_password
- def self.authenticate(login, password)
- #@u = first(Merb::Authentication::Strategies::Basic::Base.login_param => login)
- #@u && @u.authenticated?(password) ? @u : nil
- # TODO Implement above code to search "person.email". Couldn't figure how to query non-embedded documents.
- # First query the Person for email then find User with that id. match the password if person found.
- # Steve?
- if user = User.first("email" => login)
- user && user.authenticated?(password) ? user : nil
- else
- nil
- end
- end
- def authenticated?(password)
- crypted_password == encrypt(password)
- end
- private
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- def generate_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new?
- self.crypted_password = encrypt(password)
- end
- def password_required
- crypted_password.blank? || !password.blank?
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement