Guest User

Untitled

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