Sha256: ca18a683bfbc6e6cd140828674e1386d6d799b730a86d44626b8c5956c373e9f

Contents?: true

Size: 1.48 KB

Versions: 10

Compression:

Stored size: 1.48 KB

Contents

class Account
  include MongoMapper::Document
  attr_accessor :password, :password_confirmation

  # Keys
  key :name,             String
  key :surname,          String
  key :email,            String
  key :crypted_password, String
  key :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, :within => 4..40, :if => :password_required
  validates_confirmation_of :password,                   :if => :password_required
  validates_length_of       :email,    :within => 3..100
  validates_uniqueness_of   :email,    :case_sensitive => false
  validates_format_of       :email,    :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  validates_format_of       :role,     :with => /[A-Za-z]/

  # Callbacks
  before_save :encrypt_password, :if => :password_required

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

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

  private
    def encrypt_password
      self.crypted_password = ::BCrypt::Password.create(password)
    end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
padrino-admin-0.10.5 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.10.4 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.10.3 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.10.2 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.9.27 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.9.26 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.9.24 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.9.25 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.9.23 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt
padrino-admin-0.9.22 lib/padrino-admin/generators/templates/account/mongomapper.rb.tt