Guest User

Untitled

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