Sha256: 46b29df4e10b0cebac2e1583660bc2c43cb86ebc94e7a7975dff5ef0b7e6bbbe

Contents?: true

Size: 1.37 KB

Versions: 17

Compression:

Stored size: 1.37 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_create :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 %>.matching_password?(pass)
  end
  
  def matching_password?(pass)
    self.password_hash == encrypt_password(pass)
  end
  
  private
  
  def prepare_password
    self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
    self.password_hash = encrypt_password(password)
  end
  
  def encrypt_password(pass)
    Digest::SHA1.hexdigest([pass, password_salt].join)
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
polly-nifty-generators-0.2.3 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.2.5 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.2.6 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.2.7 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.2.8 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.2.9 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.0 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.1 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.2 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.3 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.4 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.5 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.6 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.7 rails_generators/nifty_authentication/templates/user.rb
twilson63-nifty-generators-0.3.8 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.2.3 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.2.2 rails_generators/nifty_authentication/templates/user.rb