Browse Source

Maintenance: Refactored authorization handling in GraphQL back end.

Martin Gruner 2 years ago
parent
commit
e8172088d4

+ 4 - 0
app/graphql/gql/queries/application_build_checksum.rb

@@ -7,6 +7,10 @@ module Gql::Queries
 
     type String, null: false
 
+    def self.authorize(...)
+      true # This query should be available for all (including unauthenticated) users.
+    end
+
     def resolve(...)
       # Use a stable identifier for the development environment, as we use hot reloading there instead.
       return 'development-auto-build' if Rails.env.development?

+ 4 - 0
app/graphql/gql/queries/application_config.rb

@@ -7,6 +7,10 @@ module Gql::Queries
 
     type [Gql::Types::KeyComplexValueType, { null: false }], null: false
 
+    def self.authorize(...)
+      true # This query should be available for all (including unauthenticated) users.
+    end
+
     # Reimplemented from sessions_controller#config_frontend.
     def resolve(...)
       result = []

+ 5 - 0
app/graphql/gql/queries/base_query.rb

@@ -4,6 +4,11 @@ module Gql::Queries
   class BaseQuery < GraphQL::Schema::Resolver
     include Gql::Concern::HandlesAuthorization
 
+    # Require authentication by default for queries.
+    def self.authorize(_obj, ctx)
+      ctx.current_user
+    end
+
     def self.register_in_schema(schema)
       field_name = name.sub('Gql::Queries::', '').gsub('::', '').camelize(:lower).to_sym
       schema.field field_name, resolver: self

+ 0 - 4
app/graphql/gql/queries/current_user.rb

@@ -7,10 +7,6 @@ module Gql::Queries
 
     type Gql::Types::UserType, null: false
 
-    def self.authorize(_obj, ctx)
-      ctx.current_user
-    end
-
     def resolve(...)
       context.current_user
     end

+ 4 - 0
app/graphql/gql/queries/form_schema.rb

@@ -9,6 +9,10 @@ module Gql::Queries
 
     type GraphQL::Types::JSON, null: false
 
+    def self.authorize(...)
+      true # This query should be available for all (including unauthenticated) users.
+    end
+
     def resolve(form_schema_id: nil)
       form_schema_id.constantize.new(context: context).schema
     end

+ 4 - 0
app/graphql/gql/queries/locales.rb

@@ -9,6 +9,10 @@ module Gql::Queries
 
     type [Gql::Types::LocaleType, { null: false }], null: false
 
+    def self.authorize(...)
+      true # This query should be available for all (including unauthenticated) users.
+    end
+
     def resolve(only_active: false)
       return Locale.where(active: true) if only_active
 

+ 0 - 4
app/graphql/gql/queries/overviews.rb

@@ -5,10 +5,6 @@ module Gql::Queries
 
     description 'Ticket overviews available in the system'
 
-    def self.authorize(_obj, ctx)
-      ctx.current_user
-    end
-
     type Gql::Types::OverviewType.connection_type, null: false
 
     def resolve(...)

+ 0 - 4
app/graphql/gql/queries/session_id.rb

@@ -7,10 +7,6 @@ module Gql::Queries
 
     type String, null: false
 
-    def self.authorize(_obj, ctx)
-      ctx.current_user
-    end
-
     def resolve(...)
       context[:sid]
     end

+ 1 - 5
app/graphql/gql/queries/ticket.rb

@@ -5,11 +5,7 @@ module Gql::Queries
 
     description 'Fetch a ticket by ID'
 
-    def self.authorize(_obj, ctx)
-      # Pundit authorization will be done via TicketType.
-      ctx.current_user
-    end
-
+    # Pundit authorization will be done via TicketType.
     argument :ticket_id, GraphQL::Types::ID, required: false, description: 'Ticket ID'
     argument :ticket_internal_id, Integer, required: false, description: 'Ticket internalId'
     argument :ticket_number, String, required: false, description: 'Ticket number'

+ 1 - 5
app/graphql/gql/queries/ticket/articles.rb

@@ -5,11 +5,7 @@ module Gql::Queries
 
     description 'Fetch a ticket by ID'
 
-    def self.authorize(_obj, ctx)
-      # Pundit authorization will be done via TicketType.
-      ctx.current_user
-    end
-
+    # Pundit authorization will be done via TicketType.
     argument :ticket_id, GraphQL::Types::ID, required: true, description: 'Ticket ID'
 
     type Gql::Types::Ticket::ArticleType.connection_type, null: false

Some files were not shown because too many files changed in this diff