2015-10-23 16:19:55 +02:00
|
|
|
class Administrateur < ActiveRecord::Base
|
|
|
|
devise :database_authenticatable, :registerable,
|
|
|
|
:recoverable, :rememberable, :trackable, :validatable
|
2015-11-10 10:23:15 +01:00
|
|
|
|
2016-05-20 15:39:17 +02:00
|
|
|
has_and_belongs_to_many :gestionnaires
|
2015-11-10 10:23:15 +01:00
|
|
|
has_many :procedures
|
2015-12-14 17:28:36 +01:00
|
|
|
|
|
|
|
before_save :ensure_api_token
|
|
|
|
|
|
|
|
def ensure_api_token
|
|
|
|
if api_token.nil?
|
|
|
|
self.api_token = generate_api_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew_api_token
|
|
|
|
update_attributes(api_token: generate_api_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_api_token
|
|
|
|
loop do
|
|
|
|
token = SecureRandom.hex(20)
|
|
|
|
break token unless Administrateur.find_by(api_token: token)
|
|
|
|
end
|
|
|
|
end
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|