sadem_app/app/models/admin.rb
Nicolas Bally 8ce9260471 suite
2020-04-01 14:28:19 +02:00

88 lines
2.2 KiB
Ruby

class Admin < ApplicationRecord
#attr_accessible :login, :email, :firstname, :name, :password, :password_confirmation, :avatar, :moderator, :as => :admin
belongs_to :p_commercial
has_many :timer_watchers
has_many :admin_preferences
has_many :admin_p_customers
has_many :p_customers, :through => :admin_p_customers
has_secure_password
attr_accessor :login
validates :password, :presence => true,
:confirmation => true,
:length => {:within => 6..40},
:on => :create
validates :password, :confirmation => true,
:length => {:within => 6..40},
:allow_blank => true,
:on => :update
validates :password_confirmation, :presence => true,
:unless => Proc.new { |a| a.password.blank? }
validates :email, :presence => true, :uniqueness => true
validates :username, :presence => true, :uniqueness => true
# mount_uploader :avatar, AvatarUploader
before_create { generate_token(:remember_token) }
def show_name
self.firstname.to_s + " " + self.name.to_s
end
def send_password_reset
generate_token(:reset_password_token)
self.reset_password_sent_at = Time.now
save!
AdminMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while Admin.exists?(column => self[column])
end
def self.find_by_login(login)
Admin.where("username = ? OR email = ?", login,login).first
end
has_many :admin_admin_roles
has_many :admin_roles, :through => :admin_admin_roles
has_many :admin_permissions, :through => :admin_roles
def has_permission?(permission_slug)
permission = AdminPermission.where(:slug => permission_slug).first
permission = AdminPermission.create(:slug => permission_slug) if !permission
if self.super_admin
true
else
if permission
if self.admin_permissions.include?(permission)
true
else
false
end
else
false
end
end
end
end