Guest User

Untitled

a guest
Jul 11th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. module Clearance
  4. module User
  5.  
  6. def self.included(model)
  7. model.extend(ClassMethods)
  8.  
  9. model.send(:include, InstanceMethods)
  10. model.send(:include, AttrAccessible)
  11. model.send(:include, AttrAccessor)
  12. model.send(:include, Validations)
  13. model.send(:include, Callbacks)
  14. end
  15.  
  16. module AttrAccessible
  17. def self.included(model)
  18. model.class_eval do
  19. attr_accessible :email, :password, :password_confirmation
  20. end
  21. end
  22. end
  23.  
  24. module AttrAccessor
  25. def self.included(model)
  26. model.class_eval do
  27. attr_accessor :password, :password_confirmation
  28. end
  29. end
  30. end
  31.  
  32. module Validations
  33. def self.included(model)
  34. model.class_eval do
  35. validates_presence_of :email
  36. validates_uniqueness_of :email, :case_sensitive => false
  37. validates_format_of :email, :with => %r{.+@.+\..+}
  38.  
  39. validates_presence_of :password, :if => :password_required?
  40. validates_confirmation_of :password, :if => :password_required?
  41. end
  42. end
  43. end
  44.  
  45. module Callbacks
  46. def self.included(model)
  47. model.class_eval do
  48. before_save :initialize_salt, :encrypt_password, :initialize_token
  49. end
  50. end
  51. end
  52.  
  53. module InstanceMethods
  54. def authenticated?(password)
  55. encrypted_password == encrypt(password)
  56. end
  57.  
  58. def encrypt(string)
  59. generate_hash("--#{salt}--#{string}--")
  60. end
  61.  
  62. def remember?
  63. token_expires_at && Time.now.utc < token_expires_at
  64. end
  65.  
  66. def remember_me!
  67. remember_me_until! 2.weeks.from_now.utc
  68. end
  69.  
  70. def forget_me!
  71. clear_token
  72. save(false)
  73. end
  74.  
  75. def confirm_email!
  76. self.email_confirmed = true
  77. self.token = nil
  78. save(false)
  79. end
  80.  
  81. def forgot_password!
  82. generate_token
  83. save(false)
  84. end
  85.  
  86. def update_password(new_password, new_password_confirmation)
  87. self.password = new_password
  88. self.password_confirmation = new_password_confirmation
  89. clear_token if valid?
  90. save
  91. end
  92.  
  93. protected
  94.  
  95. def generate_hash(string)
  96. Digest::SHA1.hexdigest(string)
  97. end
  98.  
  99. def initialize_salt
  100. if new_record?
  101. self.salt = generate_hash("--#{Time.now.utc.to_s}--#{password}--")
  102. end
  103. end
  104.  
  105. def encrypt_password
  106. return if password.blank?
  107. self.encrypted_password = encrypt(password)
  108. end
  109.  
  110. def generate_token
  111. self.token = encrypt("--#{Time.now.utc.to_s}--#{password}--")
  112. self.token_expires_at = nil
  113. end
  114.  
  115. def clear_token
  116. self.token = nil
  117. self.token_expires_at = nil
  118. end
  119.  
  120. def initialize_token
  121. generate_token if new_record?
  122. end
  123.  
  124. def password_required?
  125. encrypted_password.blank? || !password.blank?
  126. end
  127.  
  128. def remember_me_until!(time)
  129. self.token_expires_at = time
  130. self.token = encrypt("--#{token_expires_at}--#{password}--")
  131. save(false)
  132. end
  133. end
  134.  
  135. module ClassMethods
  136. def authenticate(email, password)
  137. return nil unless user = find_by_email(email)
  138. return user if user.authenticated?(password)
  139. end
  140. end
  141.  
  142. end
  143. end
Add Comment
Please, Sign In to add comment