Guest User

Untitled

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