demarches-normaliennes/app/models/administrateur.rb

31 lines
636 B
Ruby
Raw Normal View History

class Administrateur < ActiveRecord::Base
devise :database_authenticatable, :registerable,
2017-06-12 13:49:51 +02:00
:recoverable, :rememberable, :trackable, :validatable
has_and_belongs_to_many :gestionnaires
has_many :procedures
2015-12-14 17:28:36 +01:00
before_save :ensure_api_token
2017-02-07 16:56:21 +01:00
include CredentialsSyncableConcern
2015-12-14 17:28:36 +01:00
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)
2018-01-11 19:04:39 +01:00
break token if !Administrateur.find_by(api_token: token)
2015-12-14 17:28:36 +01:00
end
end
end