Guest User

Untitled

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