Guest User

Untitled

a guest
Apr 30th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. include Utils::Encryption
  2.  
  3. class User < ActiveRecord::Base
  4.  
  5. has_and_belongs_to_many :roles
  6. has_one :activation_key, :dependent => :destroy
  7.  
  8. #-------------------------------------------------------------------------------
  9. # Custom Exceptions
  10. #-------------------------------------------------------------------------------
  11.  
  12. class PasswordInvalid < Exception; end
  13.  
  14. #-------------------------------------------------------------------------------
  15. # Validations
  16. #-------------------------------------------------------------------------------
  17.  
  18. attr_accessible :first_name, :last_name, :email, :email_confirmation, :password, :password_confirmation
  19.  
  20. validates_presence_of :first_name, :last_name, :on => :create
  21. validates_length_of :first_name, :last_name, :on => :create, :maximum => 32
  22. validates_format_of :first_name, :last_name, :on => :create, :with => /^[A-Z][a-z]*[-]?[a-z]*$/
  23.  
  24. validates_presence_of :email, :if => :validate_email?
  25. validates_as_email :email, :if => :validate_email?
  26.  
  27. validates_presence_of :password, :if => :validate_password?
  28. validates_length_of :password, :if => :validate_password?, :minimum => 8
  29. validates_as_password :password, :if => :validate_password?
  30.  
  31. #-------------------------------------------------------------------------------
  32. # Validation Helpers
  33. #-------------------------------------------------------------------------------
  34.  
  35. protected
  36.  
  37. def validate_email?
  38. return new_record? || !email.nil?
  39. end
  40.  
  41. def validate_password?
  42. return new_record? || !password.nil?
  43. end
  44.  
  45. #-------------------------------------------------------------------------------
  46. # Authentication
  47. #-------------------------------------------------------------------------------
  48.  
  49. public
  50.  
  51. def self.authenticate(email, password)
  52. user = find_by_email_and_enabled(email, true) or raise ActiveRecord::RecordNotFound
  53. user.valid_password?(password) or raise User::PasswordInvalid
  54. user.update_timestamp_for(:authenticated_at)
  55. return user
  56. end
  57.  
  58. def valid_password?(password)
  59. return encrypted_password == encrypt_password(password)
  60. end
  61.  
  62. def has_role?(role)
  63. return roles.find_by_name(role) ? true : false
  64. end
  65.  
  66. protected
  67.  
  68. def password
  69. return @password
  70. end
  71.  
  72. def password=(password)
  73. @password = password
  74.  
  75. self.salt = create_salt() if new_record?
  76. self.encrypted_password = encrypt_password(password) unless password.blank?
  77. end
  78.  
  79. def encrypt_password(password)
  80. return Digest::SHA512.hexdigest("#{salt}#{password}")
  81. end
  82.  
  83. def flush_password
  84. @password = nil
  85. @password_confirmation = nil
  86. end
  87.  
  88. #-------------------------------------------------------------------------------
  89. # Activation
  90. #-------------------------------------------------------------------------------
  91.  
  92. public
  93.  
  94. def self.activate(key)
  95. user = key.user
  96. user.enabled = true
  97. #user.activation_key.destroy()
  98. user.update_timestamp_for(:activated_at)
  99. return user
  100. end
  101.  
  102. def public_activation_key
  103. return activation_key.public_key unless enabled? or activation_key.nil?
  104. end
  105.  
  106. def enable
  107. return update_attribute(:enabled, true) unless user.enabled?
  108. end
  109.  
  110. def disable
  111. return update_attribute(:enabled, false) if user.enabled?
  112. end
  113.  
  114. #-------------------------------------------------------------------------------
  115. # Password Reset
  116. #-------------------------------------------------------------------------------
  117.  
  118. def self.forgot_password(email, remote_ip)
  119. user = find_by_email_and_enabled(email, true) or raise ActiveRecord::RecordNotFound
  120. user.enabled = false
  121. user.create_activation_key()
  122. return user
  123. end
  124.  
  125. def self.reset_password(key, params)
  126. user = key.user
  127. user.update_attributes(params.slice(:password, :password_confirmation))
  128. user.enabled = true
  129. #user.activation_key.destroy()
  130. #user.update_timestamp_for(:reset_at)
  131. #return user
  132. end
  133.  
  134. #-------------------------------------------------------------------------------
  135. # Miscellaneous Helpers
  136. #-------------------------------------------------------------------------------
  137.  
  138. public
  139.  
  140. def update_timestamp_for(attr)
  141. self.class.record_timestamps = false
  142. update_attribute(attr, Time.now)
  143. self.class.record_timestamps = true
  144. end
  145.  
  146. def full_name
  147. return "#{first_name} #{last_name}"
  148. end
  149.  
  150. #-------------------------------------------------------------------------------
  151. # Lifecycle Callbacks
  152. #-------------------------------------------------------------------------------
  153.  
  154. protected
  155.  
  156. def before_create
  157. self.roles << Role.find_by_name("User")
  158. end
  159.  
  160. def after_create
  161. self.create_activation_key()
  162. end
  163.  
  164. def after_save
  165. flush_password()
  166. end
  167.  
  168. def before_destroy
  169. if system?
  170. errors.add_to_base("Woops! Can't destroy a System Account")
  171. return false
  172. end
  173. end
  174.  
  175. end
Add Comment
Please, Sign In to add comment