Guest User

Untitled

a guest
Jul 22nd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. require 'openssl'
  2. require 'digest/sha2'
  3. require 'base64'
  4.  
  5. class User < ActiveRecord::Base
  6.  
  7. attr_accessor :password, :password_confirmation
  8. attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :birthday, :weight, :bodyfat, :gender
  9.  
  10. validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  11. validates_uniqueness_of :email, :case_sensitive => false
  12. validates_length_of :first_name, :in => 1..30, :too_short => "can't be blank"
  13. validates_length_of :last_name, :in => 1..30, :too_short => "can't be blank"
  14. validates_length_of :password, :minimum => 6, :if => :password_needs_validating?
  15. validates_confirmation_of :password, :if => :password_needs_validating?
  16. validates_inclusion_of :gender, :in => %w( M F )
  17.  
  18. has_many :weight_entries
  19. has_many :bodyfat_entries
  20. has_many :workouts
  21.  
  22. acts_as_tagger
  23.  
  24. # returns the users full name
  25. def full_name
  26. first_name + " " + last_name
  27. end
  28.  
  29. # sets the password
  30. def password=(password)
  31. if !password.blank?
  32. @password_changed = true
  33. @password = password
  34.  
  35. # generate a new salt and hash the password
  36. self.salt = User.random_salt
  37. self.hashed_password = encrypt_password(password)
  38. end
  39. end
  40.  
  41. # encrypts a password based on the user's salt and the current encryption
  42. # technique
  43. def encrypt_password(password)
  44. Digest::SHA256.hexdigest(password + self.salt)
  45. end
  46.  
  47. # verifies a password against the existing password
  48. def verify_password(password)
  49. return hashed_password == encrypt_password(password)
  50. end
  51.  
  52. # returns a salt for a password
  53. def self.random_salt
  54. return Salt.random_salt
  55. end
  56.  
  57. # sets this object's password reset hash and expiration date
  58. def set_password_reset_hash
  59. self.password_reset_expires = 3.days.from_now
  60. self.password_reset_hash = Digest::SHA256.hexdigest(rand().to_s + Time.now.to_s)
  61. save!
  62. end
  63.  
  64. # finds a user by a valid password reset hash
  65. def self.find_by_valid_password_reset_hash(hash)
  66. return nil if hash.blank?
  67. User.find(:first,
  68. :conditions => ["password_reset_hash = ? AND password_reset_expires > ?",
  69. hash, Time.now])
  70. end
  71.  
  72. # builds a remember_token and saves it to the user
  73. def remember_login
  74. token = RememberToken.new
  75. token.user = self
  76. token.expires = 1.month.from_now
  77. token.token = Digest::SHA2.hexdigest("#{rand.to_s}--#{Time.now.to_i}")
  78. token.save!
  79.  
  80. return token
  81. end
  82.  
  83. # forgets the user's login
  84. def forget_login(token)
  85. token = RememberToken.find_by_token(token)
  86. token.destroy if token
  87. end
  88.  
  89. private
  90.  
  91. # lets us know that we need to validate the password
  92. def password_needs_validating?
  93. self.new_record? || @password_changed
  94. end
  95. end
Add Comment
Please, Sign In to add comment