Guest User

Untitled

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