Created
August 22, 2012 18:49
-
-
Save tugboat/3428313 to your computer and use it in GitHub Desktop.
Trying to include modules with DataMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Here is the latest way I have this implemented | |
| # I have tried many other things to try and get the magical combo | |
| # I want to be able to include this in my model by doing: | |
| # include Pakyow::Auth::UserMethods | |
| # Or something like that. I'm not having much luck. | |
| module Pakyow | |
| module Auth | |
| module UserMethods | |
| include DataMapper::Resource | |
| class << self | |
| @@login_field = :email | |
| def login_field | |
| @@login_field | |
| end | |
| end | |
| attr_accessor :password, :password_confirmation | |
| storage_names[:default] = "users" | |
| property :id, Serial | |
| property :crypted_password, String | |
| property :salt, String | |
| property :password_reset_token, String | |
| property :password_reset_token_expiration, DateTime | |
| def create_password_reset_token | |
| self.password_reset_token = rand(36**64).to_s(36) | |
| self.password_reset_token_expiration = Date.today + 5 | |
| end | |
| def password=(p) | |
| return if p.nil? || p.empty? | |
| @password = p | |
| self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{User.login_field}--") | |
| self.crypted_password = encrypt(p) | |
| end | |
| # Authenticates a user by their login name and unencrypted password. Returns the user or nil. | |
| def self.authenticate(session) | |
| u = self.first(self.login_field => session.login) # need to get the salt | |
| if u && u.authenticated?(session.password) | |
| return u | |
| else | |
| return false | |
| end | |
| end | |
| def authenticated?(password) | |
| true if self.crypted_password == encrypt(password) | |
| end | |
| private | |
| # Encrypts the password with the user salt | |
| def encrypt(password) | |
| self.encrypt(password, salt) | |
| end | |
| # Encrypts some data with the salt. | |
| def self.encrypt(password, salt) | |
| Digest::SHA1.hexdigest("--#{salt}--#{password}--") | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment