Guest User

Untitled

a guest
Jul 18th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. class Goal < ActiveRecord::Base
  2. has_many :links
  3. belongs_to :user
  4. end
  5.  
  6. require 'digest/sha1'
  7.  
  8. class User < ActiveRecord::Base
  9. has_many :goals
  10.  
  11. include Authentication
  12. include Authentication::ByPassword
  13. include Authentication::ByCookieToken
  14.  
  15. validates_presence_of :login
  16. validates_length_of :login, :within => 3..40
  17. validates_uniqueness_of :login
  18. validates_format_of :login, :with => Authentication.login_regex, :message => Authentication.bad_login_message
  19.  
  20. validates_presence_of :email
  21. validates_length_of :email, :within => 6..100 #r@a.wk
  22. validates_uniqueness_of :email
  23. validates_format_of :email, :with => Authentication.email_regex, :message => Authentication.bad_email_message
  24.  
  25.  
  26.  
  27. # HACK HACK HACK -- how to do attr_accessible from here?
  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
  31.  
  32.  
  33.  
  34. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  35. #
  36. # uff. this is really an authorization, not authentication routine.
  37. # We really need a Dispatch Chain here or something.
  38. # This will also let us return a human error message.
  39. #
  40. def self.authenticate(login, password)
  41. return nil if login.blank? || password.blank?
  42. u = find_by_login(login.downcase) # need to get the salt
  43. u && u.authenticated?(password) ? u : nil
  44. end
  45.  
  46. def login=(value)
  47. write_attribute :login, (value ? value.downcase : nil)
  48. end
  49.  
  50. def email=(value)
  51. write_attribute :email, (value ? value.downcase : nil)
  52. end
  53.  
  54. protected
  55.  
  56.  
  57.  
  58. end
Add Comment
Please, Sign In to add comment