Created
April 26, 2013 02:20
-
-
Save markprovan/5464708 to your computer and use it in GitHub Desktop.
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
,lass User < ActiveRecord::Bas, | |
attr_accessible :email, :invite_accepted, :password_hash, :password_salt, :invite_token, :role, :account_id, :created_at, :forename, :surname, :job_title, :department, :standard_rate, :invite_status | |
attr_accessor :password | |
before_save :encrypt_password, :assign_invite_token | |
validates_confirmation_of :password | |
validates_presence_of :password, :on => :create | |
validates_presence_of :email | |
validates_presence_of :account | |
validates_uniqueness_of :email | |
belongs_to :account | |
has_many :project_memberships | |
has_many :projects, :through => :project_memberships | |
def self.authenticate(email, password) | |
user = find_by_email(email) | |
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) | |
user | |
else | |
nil | |
end | |
end | |
def is_admin? | |
self.role == "admin" | |
end | |
private | |
def encrypt_password | |
if password.present? | |
self.password_salt = BCrypt::Engine.generate_salt | |
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) | |
end | |
end | |
def assign_invite_token | |
self.invite_token = generate_invite_token | |
end | |
def generate_invite_token | |
loop do | |
token = SecureRandom.hex(32) | |
break token unless User.where(:invite_token => token).exists? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment