Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.72 KB | None | 0 0
  1. # == Schema Information
  2. # Schema version: 20110127044339
  3. #
  4. # Table name: users
  5. #
  6. #  id                 :integer         not null, primary key
  7. #  name               :string(255)
  8. #  email              :string(255)
  9. #  created_at         :datetime
  10. #  updated_at         :datetime
  11. #  encrypted_password :string(255)
  12. #  salt               :string(255)
  13. #  gender             :string
  14. #  birthday           :date
  15. #  city               :string(255)
  16. #
  17.  
  18. require 'digest'
  19. class User < ActiveRecord::Base
  20.   attr_accessor :password
  21.   attr_accessible :name, :email, :password, :password_confirmation,
  22.                   :gender, :birthday, :city, :educations_attributes
  23.  
  24.   email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  25.   gender_regex = /[MFU]/i
  26.  
  27.   validates :name,      :presence     => true,
  28.                         :length       => { :maximum => 50 }
  29.   validates :email,     :presence     => true,
  30.                         :format       => { :with => email_regex },
  31.                         :uniqueness   => { :case_sensitive => false }
  32.   validates :password,  :presence     => true,
  33.                         :confirmation => true,
  34.                         :length       => { :within => 6..40 }
  35.   validates :gender,    :format       => { :with => gender_regex }
  36.   #validates :birthday,  :presence     => false
  37.   validates :city,      :length       => { :minimum => 1 }, :allow_nil => true
  38.  
  39.   before_save :encrypt_password
  40.  
  41.   has_many :friendships
  42.   has_many :friends, :through => :friendships
  43.   has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
  44.   has_many :inverse_friends, :through => :inverse_friendships, :source => :user
  45.   has_many :educations
  46.   accepts_nested_attributes_for :educations,
  47.                                 :allow_destroy => true,
  48.                                 :reject_if => proc {
  49.                                   |attrs| attrs[:title].blank?
  50.                                 }
  51.  
  52.   def self.authenticate(email, submitted_password)
  53.     user = find_by_email(email)
  54.     return nil if user.nil?
  55.     return user if user.has_password?(submitted_password)
  56.   end
  57.  
  58.   def self.authenticate_with_salt(id, cookie_salt)
  59.     user = find_by_id(id)
  60.     (user && user.salt == cookie_salt) ? user : nil
  61.   end
  62.  
  63.   def has_password?(submitted_password)
  64.     encrypted_password == encrypt(submitted_password)
  65.   end
  66.  
  67.   private
  68.  
  69.     def encrypt_password
  70.       self.salt = make_salt if new_record?
  71.       self.encrypted_password = encrypt(password)
  72.     end
  73.  
  74.     def encrypt(string)
  75.       secure_hash("#{salt}--#{string}")
  76.     end
  77.  
  78.     def make_salt
  79.       secure_hash("#{Time.now.utc}--#{password}")
  80.     end
  81.  
  82.     def secure_hash(string)
  83.       Digest::SHA2.hexdigest(string)
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement