Class: User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- User
- Defined in:
- app/models/user.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#temp_password ⇒ Object
Returns the value of attribute temp_password.
Class Method Summary collapse
Instance Method Summary collapse
- #encrypt_password ⇒ Object
- #generate_password ⇒ Object
- #generate_token ⇒ Object
- #save_token ⇒ Object
- #set_temporary_password ⇒ Object
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
3 4 5 |
# File 'app/models/user.rb', line 3 def password @password end |
#temp_password ⇒ Object
Returns the value of attribute temp_password.
3 4 5 |
# File 'app/models/user.rb', line 3 def temp_password @temp_password end |
Class Method Details
.authenticate(email, password) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'app/models/user.rb', line 11 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 |
Instance Method Details
#encrypt_password ⇒ Object
20 21 22 23 24 25 |
# File 'app/models/user.rb', line 20 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 |
#generate_password ⇒ Object
34 35 36 37 38 39 |
# File 'app/models/user.rb', line 34 def generate_password password_length = 7 lower_alphabets = ("a".."z").to_a return Array.new(password_length){||lower_alphabets[rand(lower_alphabets.size)]}.join end |
#generate_token ⇒ Object
41 42 43 44 45 46 |
# File 'app/models/user.rb', line 41 def generate_token self.authentication_token = loop do random_token = SecureRandom.urlsafe_base64(nil, false) break random_token unless self.class.exists?(authentication_token: random_token) end end |
#save_token ⇒ Object
48 49 50 51 |
# File 'app/models/user.rb', line 48 def save_token self.generate_token self.save end |
#set_temporary_password ⇒ Object
27 28 29 30 31 32 |
# File 'app/models/user.rb', line 27 def set_temporary_password self.temp_password = generate_password self.password_salt = ::BCrypt::Engine.generate_salt self.password_hash = ::BCrypt::Engine.hash_secret(temp_password, password_salt) return temp_password end |