Merge pull request #8766 from demarches-simplifiees/better_api_log

Better api log
This commit is contained in:
LeSim 2023-03-16 10:19:10 +00:00 committed by GitHub
commit bb442ccfe3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 22 deletions

View file

@ -46,18 +46,20 @@ class API::V1::DossiersController < APIController
@procedure = Procedure.for_api.find(params[:procedure_id]) @procedure = Procedure.for_api.find(params[:procedure_id])
administrateur = find_administrateur_for_token(@procedure) administrateur = find_administrateur_for_token(@procedure)
if administrateur if administrateur.nil?
Current.administrateur = administrateur
else
render json: {}, status: :unauthorized render json: {}, status: :unauthorized
else
# allow BaseController append_info_to_payload
# to log info on current_user
@current_user = administrateur.user
order = ORDER_DIRECTIONS.fetch(params[:order], :asc)
@dossiers = @procedure
.dossiers
.visible_by_administration
.order_by_created_at(order)
end end
order = ORDER_DIRECTIONS.fetch(params[:order], :asc)
@dossiers = @procedure
.dossiers
.visible_by_administration
.order_by_created_at(order)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render json: {}, status: :not_found render json: {}, status: :not_found
end end

View file

@ -11,12 +11,13 @@ class API::V1::ProceduresController < APIController
@procedure = Procedure.for_api.find(params[:id]) @procedure = Procedure.for_api.find(params[:id])
administrateur = find_administrateur_for_token(@procedure) administrateur = find_administrateur_for_token(@procedure)
if administrateur if administrateur.nil?
Current.administrateur = administrateur
else
render json: {}, status: :unauthorized render json: {}, status: :unauthorized
else
# allow BaseController append_info_to_payload
# to log info on current_user
@current_user = administrateur.user
end end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render json: {}, status: :not_found render json: {}, status: :not_found
end end

View file

@ -19,6 +19,12 @@ class API::V2::GraphqlController < API::V2::BaseController
private private
def append_info_to_payload(payload) def append_info_to_payload(payload)
# if on the graphql playground, authenticate via devise
# if authenticate by a v2 or v3 token
# @current_user is set by `api_v2_base_controller.authenticate_administrateur_from_token`
# else it is set on `context.authorized_demarche`
@current_user ||= Current.user
super super
payload.merge!({ payload.merge!({

View file

@ -7,7 +7,6 @@ class ApplicationController < ActionController::Base
MAINTENANCE_MESSAGE = 'Le site est actuellement en maintenance. Il sera à nouveau disponible dans un court instant.' MAINTENANCE_MESSAGE = 'Le site est actuellement en maintenance. Il sera à nouveau disponible dans un court instant.'
before_action :set_current_roles
before_action :set_sentry_user before_action :set_sentry_user
before_action :redirect_if_untrusted before_action :redirect_if_untrusted
before_action :reject, if: -> { ENV.fetch("MAINTENANCE_MODE", 'false') == 'true' } before_action :reject, if: -> { ENV.fetch("MAINTENANCE_MODE", 'false') == 'true' }
@ -150,11 +149,6 @@ class ApplicationController < ActionController::Base
private private
def set_current_roles
Current.administrateur = current_administrateur
Current.instructeur = current_instructeur
end
def set_active_storage_host def set_active_storage_host
ActiveStorage::Current.host = request.base_url ActiveStorage::Current.host = request.base_url
end end

View file

@ -62,7 +62,13 @@ class API::V2::Context < GraphQL::Query::Context
if self[:procedure_ids].present? if self[:procedure_ids].present?
self[:procedure_ids].include?(demarche.id) self[:procedure_ids].include?(demarche.id)
elsif self[:token].present? elsif self[:token].present?
APIToken.find_and_verify(self[:token], demarche.administrateurs).present? token = APIToken.find_and_verify(self[:token], demarche.administrateurs)
if token.present?
Current.user = token.administrateur.user
true
else
false
end
else else
false false
end end

View file

@ -1,3 +1,3 @@
class Current < ActiveSupport::CurrentAttributes class Current < ActiveSupport::CurrentAttributes
attribute :instructeur, :administrateur, :request_id attribute :user, :request_id
end end