47 lines
1.2 KiB
Ruby
47 lines
1.2 KiB
Ruby
# -*- encoding : utf-8 -*-
|
|
class Admin < ActiveRecord::Base
|
|
# Include default devise modules. Others available are:
|
|
# :token_authenticatable, :confirmable,
|
|
# :lockable, :timeoutable and :omniauthable
|
|
devise :database_authenticatable, :registerable,
|
|
:recoverable, :rememberable, :trackable, :validatable
|
|
|
|
attr_accessor :login
|
|
|
|
# Setup accessible (or protected) attributes for your model
|
|
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :name, :firstname, :roles_mask, :roles, :super_admin
|
|
# attr_accessible :title, :body
|
|
|
|
|
|
ROLES = %w[SuperAdmin]
|
|
|
|
def super_admin?
|
|
true if self.is? :SuperAdmin
|
|
end
|
|
|
|
|
|
def roles=(roles)
|
|
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
|
|
end
|
|
|
|
def roles
|
|
ROLES.reject do |r|
|
|
((roles_mask || 0) & 2**ROLES.index(r)).zero?
|
|
end
|
|
end
|
|
|
|
def is?(role)
|
|
roles.include?(role.to_s)
|
|
end
|
|
|
|
protected
|
|
|
|
def self.find_for_database_authentication(warden_conditions)
|
|
conditions = warden_conditions.dup
|
|
login = conditions.delete(:login)
|
|
where(conditions).where(["username = :value OR email = :value", { :value => login }]).first
|
|
end
|
|
|
|
|
|
end
|