Sha256: 6c62438473950b2a7fd94b7aaaa8820b8ba4e2b7c87ec3980216600a70b14d30

Contents?: true

Size: 1.85 KB

Versions: 28

Compression:

Stored size: 1.85 KB

Contents

class <%= @model_name %>
  include DataMapper::Resource
  include DataMapper::Validate
  attr_accessor :password, :password_confirmation

  # Properties
  property :id,               Serial
  property :name,             String
  property :surname,          String
  property :email,            String
  property :crypted_password, String, :length => 70
  property :role,             String

  # Validations
  validates_presence_of      :email, :role
  validates_presence_of      :password,                          :if => :password_required
  validates_presence_of      :password_confirmation,             :if => :password_required
  validates_length_of        :password, :min => 4, :max => 40,   :if => :password_required
  validates_confirmation_of  :password,                          :if => :password_required
  validates_length_of        :email,    :min => 3, :max => 100
  validates_uniqueness_of    :email,    :case_sensitive => false
  validates_format_of        :email,    :with => :email_address
  validates_format_of        :role,     :with => /[A-Za-z]/

  # Callbacks
  before :save, :encrypt_password

  ##
  # This method is for authentication purpose.
  #
  def self.authenticate(email, password)
    account = first(:conditions => ["lower(email) = lower(?)", email]) if email.present?
    account && account.has_password?(password) ? account : nil
  end

  ##
  # This method is used by AuthenticationHelper
  #
  def self.find_by_id(id)
    get(id) rescue nil
  end

  def has_password?(password)
    ::BCrypt::Password.new(crypted_password) == password
  end

  def password=(password)
    self.crypted_password = nil if password.present?
    @password = password
  end

  private

  def password_required
    crypted_password.blank? || password.present?
  end

  def encrypt_password
    self.crypted_password = ::BCrypt::Password.create(password) if password.present?
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
padrino-admin-0.16.0.pre3 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.16.0.pre2 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.15.3 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.15.2 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.15.1 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.15.0 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.4 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.3 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.12.9 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.2 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.13.3.4 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.1.1 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.1 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.0.2 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.0.1 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.0 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.0.rc2 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.14.0.rc1 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.13.3.3 lib/padrino-admin/generators/templates/account/datamapper.rb.tt
padrino-admin-0.13.3.2 lib/padrino-admin/generators/templates/account/datamapper.rb.tt