Sha256: 501cc7a1633c94e5c3690a3d027311efe0839cab8f7bd3ac93648eb9dedbbd0b

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 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 :email, 
            :format => {:with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i,
                        :allow_blank => true, 
                        :allow_nil => true },
            :presence => { :if => lambda {self.username.nil?} },
            :uniqueness => true
                        
  validates :username,
            :format => {:with => /[A-Za-z0-9]+/, :allow_blank => true, :allow_nil => true},
            :presence => { :if => lambda {self.email.nil?} },
            :length => {:minimum => 3, :allow_blank => true},
            :uniqueness => true
            
  validates :role, 
            :inclusion => { :in => %w(admin moderator writer user guest) }

  validates :password,
            :length => {:minimum => 3},
            :presence => true




  # login can be either username or email address
  def self.authenticate(params)
    login = params[:login]
    conditions = ['username = ? or email = ?', login, login]
    <%= user_singular_name %> = <%= user_class_name %>.where(conditions).first
    return <%= user_singular_name %> if <%= user_singular_name %> && <%= user_singular_name %>.matching_password?(params[:password])
  end

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

  private

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

  def encrypt_password(pass)
    BCrypt::Engine.hash_secret(pass, password_salt)
  end
<%- end -%>
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
seivan-generators-0.5 lib/generators/seivan/authentication/templates/user.rb
seivan-generators-0.4 lib/generators/seivan/authentication/templates/user.rb