Merge pull request #9377 from tchak/feat-api-request-touch

ETQ Administrateur, je voudrais que mon compte soit considéré comme actif si je n'utilise que l'API
This commit is contained in:
Paul Chavard 2023-08-02 11:52:18 +00:00 committed by GitHub
commit 166c57b466
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 4 deletions

View file

@ -39,7 +39,9 @@ class API::V2::BaseController < ApplicationController
def api_token
if @api_token.nil?
@api_token = APIToken.find_and_verify(authorization_bearer_token) || false
@api_token = APIToken
.find_and_verify(authorization_bearer_token)
&.tap { _1.touch(:last_v2_authenticated_at) } || false
end
@api_token
end

View file

@ -6,6 +6,7 @@ class APIController < ApplicationController
def find_administrateur_for_token(procedure)
api_token = APIToken.find_and_verify(authorization_bearer_token, procedure.administrateurs)
if api_token.present? && api_token.context.fetch(:procedure_ids).include?(procedure.id)
api_token.touch(:last_v1_authenticated_at)
api_token.administrateur
end
end

View file

@ -80,6 +80,7 @@ class API::V2::Context < GraphQL::Query::Context
elsif self[:token].present?
token = APIToken.find_and_verify(self[:token], demarche.administrateurs)
if token.present?
token.touch(:last_v2_authenticated_at)
Current.user = token.administrateur.user
true
else

View file

@ -20,7 +20,10 @@ class Administrateur < ApplicationRecord
.where.missing(:services)
.left_outer_joins(:administrateurs_procedures) # needed to bypass procedure hidden default scope
.where(administrateurs_procedures: { procedure_id: nil })
.where("users.last_sign_in_at < ? ", UNUSED_ADMIN_THRESHOLD.ago)
.includes(:api_tokens)
.where(users: { last_sign_in_at: ..UNUSED_ADMIN_THRESHOLD.ago })
.merge(APIToken.where(last_v1_authenticated_at: nil).or(APIToken.where(last_v1_authenticated_at: ..UNUSED_ADMIN_THRESHOLD.ago)))
.merge(APIToken.where(last_v2_authenticated_at: nil).or(APIToken.where(last_v2_authenticated_at: ..UNUSED_ADMIN_THRESHOLD.ago)))
end
def self.by_email(email)