This repository has been archived on 2021-11-24. You can view files and clone it, but cannot push or open issues or pull requests.
phone_app/app/models/admin.rb
2021-08-23 10:26:02 +02:00

101 lines
2.4 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
belongs_to :societe
has_secure_password
attr_accessor :login
validates :password, :presence => true,
:confirmation => true,
:on => :create
#:length => {:within => 6..40},
validates :password, :confirmation => true,
:allow_blank => true,
:on => :update
#:length => {:within => 6..40},
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 societes
if self.super_admin
Societe
else
if self.societe and self.societe.enseigne?
Societe.where(:enseigne => self.societe.enseigne)
elsif self.societe
Societe.where(:id => self.societe_id)
else
Societe.where(:id => 99999999)
end
end
end
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)
if self.super_admin
true
else
permission = AdminPermission.where("code = ? or slug = ?",permission, permission).first
if permission
if self.admin_permissions.include?(permission)
true
else
false
end
else
false
end
end
end
end