1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| # == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# password :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
module Usermod
attr_accessor :name, :password, :password_confirmation, :id, :email
end
has_many :incidents
email_regexp = /\A[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$\z/
name_regexp = /\A([a-zA-Z0-9-_]{2,36})\z/
validates :name, :presence => true,
:format => {:with => name_regexp},
:uniqueness => {:case_sensitive => false}
validates :email, :presence => true,
:format => {:with => email_regexp},
:uniqueness => {:case_sensitive => false}
validates :password, :presence => true,
:confirmation => true,
:length => {:within => 6..40}
before_save :encrypt_password
def has_password?(password_soumis)
encrypted_password == encrypt(password_soumis)
end
private
def encrypt_password
self.encrypted_password = encrypt(password)
end
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end |
Partager