Sha256: 3b412264a95e4ba4c2b79102a9ea36c919ae7c5491a39d6a1c87bafca290e1ee

Contents?: true

Size: 1.46 KB

Versions: 14

Compression:

Stored size: 1.46 KB

Contents

class <%= user_class_name %> < ActiveRecord::Base
<%- if options[:authlogic] -%>
  acts_as_authentic
<%- else -%>
  # 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 %>.matching_password?(pass)
  end

  def matching_password?(pass)
    self.password_hash == encrypt_password(pass)
  end

  private

  def prepare_password
    unless password.blank?
      self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
      self.password_hash = encrypt_password(password)
    end
  end

  def encrypt_password(pass)
    Digest::SHA1.hexdigest([pass, password_salt].join)
  end
<%- end -%>
end

Version data entries

14 entries across 13 versions & 2 rubygems

Version Path
flockonus-nifty-generators-0.0.12 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.4.6 rails_generators/nifty_authentication/templates/user.rb
flockonus-nifty-generators-0.0.11 rails_generators/nifty_authentication/templates/user.rb
flockonus-nifty-generators-0.0.10 rails_generators/nifty_authentication/templates/user.rb
flockonus-nifty-generators-0.0.9 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.4.5 rails_generators/nifty_authentication/templates/user.rb
flockonus-nifty-generators-0.0.8 rails_generators/nifty_authentication/templates/user.rb
flockonus-nifty-generators-0.0.7 rails_generators/nifty_authentication/templates/user.rb
flockonus-nifty-generators-0.0.6 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.4.4 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.4.3 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.4.2 rails_generators/nifty_authentication/templates/user.rb
nifty-generators-0.4.1 lib/generators/nifty/authentication/templates/user.rb
nifty-generators-0.4.1 rails_generators/nifty_authentication/templates/user.rb