Guest User

Untitled

a guest
May 17th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. require 'digest/sha1'
  2. class User < ActiveRecord::Base
  3. # Virtual attribute for the unencrypted password
  4. attr_accessor :password
  5.  
  6. validates_presence_of :login, :email
  7. validates_presence_of :password, :if => :password_required?
  8. validates_presence_of :password_confirmation, :if => :password_required?
  9. validates_length_of :password, :within => 4..40, :if => :password_required?
  10. validates_confirmation_of :password, :if => :password_required?
  11. validates_length_of :login, :within => 3..40
  12. validates_length_of :email, :within => 6..100
  13. validates_uniqueness_of :login, :email, :case_sensitive => false
  14. validates_format_of :email, :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/i
  15. validates_presence_of :phone, :first_name, :last_name
  16. validates_length_of :phone, :is => 10, :message => 'must be exactly 10 characters long'
  17. validates_numericality_of :phone
  18.  
  19. has_many :permissions
  20. has_one :role
  21. has_many :licenses
  22.  
  23. before_save :encrypt_password
  24. before_create :make_activation_code
  25.  
  26. # prevents a user from submitting a crafted form that bypasses activation
  27. # anything else you want your user to change should be added here.
  28. attr_accessible :login, :email, :password, :password_confirmation
  29.  
  30. class ActivationCodeNotFound < StandardError; end
  31. class AlreadyActivated < StandardError
  32. attr_reader :user, :message;
  33. def initialize(user, message=nil)
  34. @message, @user = message, user
  35. end
  36. end
  37.  
  38. # Finds the user with the corresponding activation code, activates their account and returns the user.
  39. #
  40. # Raises:
  41. # +User::ActivationCodeNotFound+ if there is no user with the corresponding activation code
  42. # +User::AlreadyActivated+ if the user with the corresponding activation code has already activated their account
  43. def self.find_and_activate!(activation_code)
  44. raise ArgumentError if activation_code.nil?
  45. user = find_by_activation_code(activation_code)
  46. raise ActivationCodeNotFound if !user
  47. raise AlreadyActivated.new(user) if user.active?
  48. user.send(:activate!)
  49. user
  50. end
  51.  
  52. def active?
  53. # the presence of an activation date means they have activated
  54. !activated_at.nil?
  55. end
  56.  
  57. # Returns true if the user has just been activated.
  58. def pending?
  59. @activated
  60. end
  61.  
  62. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  63. # Updated 2/20/08
  64. def self.authenticate(login, password)
  65. u = find :first, :conditions => ['login = ?', login] # need to get the salt
  66. u && u.authenticated?(password) ? u : nil
  67. end
  68.  
  69. # Encrypts some data with the salt.
  70. def self.encrypt(password, salt)
  71. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  72. end
  73.  
  74. # Encrypts the password with the user salt
  75. def encrypt(password)
  76. self.class.encrypt(password, salt)
  77. end
  78.  
  79. def authenticated?(password)
  80. crypted_password == encrypt(password)
  81. end
  82.  
  83. def remember_token?
  84. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  85. end
  86.  
  87. # These create and unset the fields required for remembering users between browser closes
  88. def remember_me
  89. remember_me_for 2.weeks
  90. end
  91.  
  92. def remember_me_for(time)
  93. remember_me_until time.from_now.utc
  94. end
  95.  
  96. def remember_me_until(time)
  97. self.remember_token_expires_at = time
  98. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  99. save(false)
  100. end
  101.  
  102. def forget_me
  103. self.remember_token_expires_at = nil
  104. self.remember_token = nil
  105. save(false)
  106. end
  107.  
  108. def forgot_password
  109. @forgotten_password = true
  110. self.make_password_reset_code
  111. end
  112.  
  113. def reset_password
  114. # First update the password_reset_code before setting the
  115. # reset_password flag to avoid duplicate email notifications.
  116. update_attribute(:password_reset_code, nil)
  117. @reset_password = true
  118. end
  119.  
  120. #used in user_observer
  121. def recently_forgot_password?
  122. @forgotten_password
  123. end
  124.  
  125. def recently_reset_password?
  126. @reset_password
  127. end
  128.  
  129. def self.find_for_forget(email)
  130. find :first, :conditions => ['email = ? and activated_at IS NOT NULL', email]
  131. end
  132.  
  133. def has_role?(rolename)
  134. self.roles.find_by_rolename(rolename) ? true : false
  135. end
  136.  
  137. def role?
  138. self.role_id
  139. end
  140.  
  141. def login?
  142. self.login
  143. end
  144.  
  145.  
  146. protected
  147. # before filter
  148. def encrypt_password
  149. return if password.blank?
  150. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  151. self.crypted_password = encrypt(password)
  152. end
  153.  
  154. def password_required?
  155. crypted_password.blank? || !password.blank?
  156. end
  157.  
  158. def make_activation_code
  159. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  160. end
  161.  
  162. def make_password_reset_code
  163. self.password_reset_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  164. end
  165.  
  166.  
  167. private
  168.  
  169. def activate!
  170. @activated = true
  171. self.update_attribute(:activated_at, Time.now.utc)
  172. end
  173.  
  174. end
Add Comment
Please, Sign In to add comment