/vendor/plugins/restful_authentication/generators/authenticated/templates/model.rb

https://github.com/globetrotter/derdiedas · Ruby · 142 lines · 112 code · 19 blank · 11 comment · 14 complexity · c95f03e3e5bb3890c74f390e39b7bd95 MD5 · raw file

  1. require 'digest/sha1'
  2. class <%= class_name %> < ActiveRecord::Base
  3. # Virtual attribute for the unencrypted password
  4. attr_accessor :password
  5. validates_presence_of :login, :email
  6. validates_presence_of :password, :if => :password_required?
  7. validates_presence_of :password_confirmation, :if => :password_required?
  8. validates_length_of :password, :within => 4..40, :if => :password_required?
  9. validates_confirmation_of :password, :if => :password_required?
  10. validates_length_of :login, :within => 3..40
  11. validates_length_of :email, :within => 3..100
  12. validates_uniqueness_of :login, :email, :case_sensitive => false
  13. before_save :encrypt_password
  14. <% if options[:include_activation] && !options[:stateful] %>before_create :make_activation_code <% end %>
  15. # prevents a user from submitting a crafted form that bypasses activation
  16. # anything else you want your user to change should be added here.
  17. attr_accessible :login, :email, :password, :password_confirmation
  18. <% if options[:stateful] %>
  19. acts_as_state_machine :initial => :pending
  20. state :passive
  21. state :pending, :enter => :make_activation_code
  22. state :active, :enter => :do_activate
  23. state :suspended
  24. state :deleted, :enter => :do_delete
  25. event :register do
  26. transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
  27. end
  28. event :activate do
  29. transitions :from => :pending, :to => :active
  30. end
  31. event :suspend do
  32. transitions :from => [:passive, :pending, :active], :to => :suspended
  33. end
  34. event :delete do
  35. transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
  36. end
  37. event :unsuspend do
  38. transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? }
  39. transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? }
  40. transitions :from => :suspended, :to => :passive
  41. end
  42. <% elsif options[:include_activation] %>
  43. # Activates the user in the database.
  44. def activate
  45. @activated = true
  46. self.activated_at = Time.now.utc
  47. self.activation_code = nil
  48. save(false)
  49. end
  50. def active?
  51. # the existence of an activation code means they have not activated yet
  52. activation_code.nil?
  53. end
  54. <% end %>
  55. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  56. def self.authenticate(login, password)
  57. u = <%
  58. if options[:stateful] %>find_in_state :first, :active, :conditions => {:login => login}<%
  59. elsif options[:include_activation] %>find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login]<%
  60. else %>find_by_login(login)<%
  61. end %> # need to get the salt
  62. u && u.authenticated?(password) ? u : nil
  63. end
  64. # Encrypts some data with the salt.
  65. def self.encrypt(password, salt)
  66. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  67. end
  68. # Encrypts the password with the user salt
  69. def encrypt(password)
  70. self.class.encrypt(password, salt)
  71. end
  72. def authenticated?(password)
  73. crypted_password == encrypt(password)
  74. end
  75. def remember_token?
  76. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  77. end
  78. # These create and unset the fields required for remembering users between browser closes
  79. def remember_me
  80. remember_me_for 2.weeks
  81. end
  82. def remember_me_for(time)
  83. remember_me_until time.from_now.utc
  84. end
  85. def remember_me_until(time)
  86. self.remember_token_expires_at = time
  87. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  88. save(false)
  89. end
  90. def forget_me
  91. self.remember_token_expires_at = nil
  92. self.remember_token = nil
  93. save(false)
  94. end
  95. # Returns true if the user has just been activated.
  96. def recently_activated?
  97. @activated
  98. end
  99. protected
  100. # before filter
  101. def encrypt_password
  102. return if password.blank?
  103. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  104. self.crypted_password = encrypt(password)
  105. end
  106. def password_required?
  107. crypted_password.blank? || !password.blank?
  108. end
  109. <% if options[:include_activation] %>
  110. def make_activation_code
  111. <% if options[:stateful] %> self.deleted_at = nil<% end %>
  112. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  113. end<% end %>
  114. <% if options[:stateful] %>
  115. def do_delete
  116. self.deleted_at = Time.now.utc
  117. end
  118. def do_activate
  119. @activated = true
  120. self.activated_at = Time.now.utc
  121. self.deleted_at = self.activation_code = nil
  122. end<% end %>
  123. end