Guest User

Untitled

a guest
Nov 19th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. attr_accessor :password
  3. attr_accessible :email, :name, :password, :password_confirmation, :userpicurl, :info, :userpic, :remote_userpic_url
  4.  
  5. has_many :collections, :dependent => :destroy
  6. has_many :achives, :dependent => :destroy
  7.  
  8. mount_uploader :userpic, ImageUploader
  9.  
  10. email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  11.  
  12. validates :name, :presence => true,
  13. :length => { :maximum => 50 }
  14. validates :email, :presence => true,
  15. :format => { :with => email_regex },
  16. :uniqueness => { :case_sensitive => false }
  17. validates :password, :presence => true,
  18. :confirmation => true,
  19. :length => { :within => 6..40 }
  20. validates :info, :length => { :maximum => 512 }
  21.  
  22. before_save :encrypt_password
  23.  
  24. # Return true if the user's password matches the submitted password.
  25. def has_password?(submitted_password)
  26. encrypted_password == encrypt(submitted_password)
  27. end
  28.  
  29. def self.authenticate(email, submitted_password)
  30. user = find_by_email(email)
  31. return nil if user.nil?
  32. return user if user.has_password?(submitted_password)
  33. end
  34.  
  35. def self.authenticate_with_salt(id, cookie_salt)
  36. user = find_by_id(id)
  37. (user && user.salt == cookie_salt) ? user : nil
  38. end
  39.  
  40. def getuserpic(id)
  41. user = User.find(id)
  42. user.userpicurl = user.userpic_url
  43. return "userpicurl/defaultuserpic.jpg" if user.userpicurl.nil?
  44. return user.userpic_url(:thumb)
  45.  
  46. end
  47.  
  48. def custom_update_attributes(params)
  49. if params[:password].blank?
  50. params.delete :password
  51. params.delete :password_confirmation
  52. end
  53. update_attributes params
  54. end
  55.  
  56. private
  57.  
  58. def encrypt_password
  59. self.salt = make_salt if new_record?
  60. self.encrypted_password = encrypt(password)
  61. end
  62.  
  63. def encrypt(string)
  64. secure_hash("#{salt}--#{string}")
  65. end
  66.  
  67. def make_salt
  68. secure_hash("#{Time.now.utc}--#{password}")
  69. end
  70.  
  71. def secure_hash(string)
  72. Digest::SHA2.hexdigest(string)
  73. end
  74. end
Add Comment
Please, Sign In to add comment