Sha256: 1c15f4843c382c20dc91be676e176188aa8a44f51cbc39ae863ac99b99770f0d

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

class <%= user_class_name %> < ActiveRecord::Base

  # new columns need to be added here to be writable through mass assignment
  attr_accessible :username, :email, :password, :password_confirmation

  attr_accessor :password
  before_save :prepare_password

  validates_presence_of :username
  validates_uniqueness_of :username, :email, :allow_blank => true
  validates_format_of :username, with: /^[-\w\._@]+$/i, :allow_blank => true, message: "should only contain letters, numbers, or .-_@"
  validates_format_of :email, with: /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_presence_of password, on: :create
  validates_confirmation_of :password
  validates_length_of :password, minimum: 4, :allow_blank => true

  # login can be either username or email address
  def self.authenticate(login, pass)
    <%= user_singular_name %> = find_by_username(login) || find_by_email(login)
    return <%= user_singular_name %> if <%= user_singular_name %> && <%= user_singular_name %>.password_hash == <%= user_singular_name %>.encrypt_password(pass)
  end

  def encrypt_password(pass)
    BCrypt::Engine.hash_secret(pass, password_salt)
  end

  private

  def prepare_password
    unless password.blank?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = encrypt_password(password)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zoo-generators-0.1.5 lib/generators/zoo/authentication/templates/user.rb
zoo-generators-0.1.4 lib/generators/zoo/authentication/templates/user.rb
zoo-generators-0.1.3 lib/generators/zoo/authentication/templates/user.rb
zoo-generators-0.1.2 lib/generators/zoo/authentication/templates/user.rb
zoo-generators-0.1.1 lib/generators/zoo/authentication/templates/user.rb