Add authorizations to root queries

This commit is contained in:
Paul Chavard 2018-11-22 18:43:53 +01:00
parent a7fc4df09b
commit ba683a107c
3 changed files with 29 additions and 0 deletions

View file

@ -1,4 +1,25 @@
module Types
class BaseObject < GraphQL::Schema::Object
def self.authorized_demarche?(demarche, context)
# We are caching authorization logic because it is called for each node
# of the requested graph and can be expensive. Context is reset per request so it is safe.
context[:authorized] ||= {}
if context[:authorized][demarche.id]
return true
end
administrateur = demarche.administrateurs.find do |administrateur|
if context[:token]
administrateur.valid_api_token?(context[:token])
else
administrateur.id == context[:administrateur_id]
end
end
if administrateur
context[:authorized][demarche.id] = true
true
end
end
end
end

View file

@ -46,5 +46,9 @@ module Types
dossiers
end
def self.authorized?(object, context)
authorized_demarche?(object, context)
end
end
end

View file

@ -38,5 +38,9 @@ module Types
def instructeurs
Loaders::Association.for(object.class, :followers_instructeurs).load(object)
end
def self.authorized?(object, context)
authorized_demarche?(object.procedure, context)
end
end
end