Sha256: f237538389dd799f990af26152aab41ebe2df7ee7f12215cb01b9966c9a8dc5b

Contents?: true

Size: 814 Bytes

Versions: 4

Compression:

Stored size: 814 Bytes

Contents

# frozen_string_literal: true

module Ibrain
  module Extentions
    class AuthorizeRequired < GraphQL::Schema::FieldExtension
      def resolve(object:, arguments:, **rest)
        raise IbrainErrors::PermissionError.new("You not have permission to access #{field&.name}") unless is_authorized(object)

        # yield the current time as `memo`
        yield(object, arguments, rest)
      end

      private

      def is_authorized(object)
        required_roles = Ibrain::Config.authorize_resource_enabled_with_roles
        current_user = object.try(:context).try(:fetch, :current_user, nil)

        role = current_user.try(:role) || current_user.try(:graphql_role)

        return true unless required_roles.include?(role)

        current_user.try(:is_authorized?, field.name)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ibrain-core-0.5.15 app/graphql/ibrain/extentions/authorize_required.rb
ibrain-core-0.5.13 app/graphql/ibrain/extentions/authorize_required.rb
ibrain-core-0.5.12 app/graphql/ibrain/extentions/authorize_required.rb
ibrain-core-0.5.10 app/graphql/ibrain/extentions/authorize_required.rb