Sha256: 579519e6d5db36816f9a1d0e363b1c98f296c74202731af8d4d4b7dd5d85bdd9

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :email
  field :name
  field :password_hash
  field :password_salt
  field :admin, type: Boolean, default: false

  attr_accessible :email, :name, :password

  attr_accessor :password
  before_save :encrypt_password

  validates :password, presence: true, on: :create
  # TODO: add password length
  #validates :password, length: {min: 6}, empty: true
  validates :email, presence: true
  validates :email, uniqueness: true
  validates :email, email: true

  def self.authenticate(email, password)
    user = where(email: email).first
    user.verify(password) if user
  end

  def verify(password)
    if password_hash == BCrypt::Engine.hash_secret(password, password_salt)
      self
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end

  def admin?
    self.admin
  end

  def self.find_by_id(id)
    self.where(:_id => id).first
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
oauth2_provider_engine-0.0.2 test/dummy/app/models/user.rb
oauth2_provider_engine-0.0.1 test/dummy/app/models/user.rb