Sha256: 5e6f3917dc42e2e52fb65ac50ac59a0492caee1af5fd27ffccd970c472146e10

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

require 'digest/sha1'

class Account
  include Mongoid::Document
  attr_accessor :password

  # Fields
  field :name,             :type => String
  field :surname,          :type => String
  field :email,            :type => String
  field :crypted_password, :type => String
  field :salt,             :type => String
  field :role,             :type => 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
  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 :generate_password

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

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

  ##
  # This method is used to retrieve the original password.
  #
  def password_clean
    crypted_password.decrypt(salt)
  end

  private
    def generate_password
      return if password.blank?
      self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
      self.crypted_password = password.encrypt(self.salt)
    end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
padrino-admin-0.9.18 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.17 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.16 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.15 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.14 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.13 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.12 lib/padrino-admin/generators/templates/account/mongoid.rb.tt
padrino-admin-0.9.11 lib/padrino-admin/generators/templates/account/mongoid.rb.tt