This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add scope support * Fix or ignore style issues * Add a bit of documentation * Obey review
- Loading branch information
Showing
7 changed files
with
274 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pundit' | ||
|
||
module GraphQL | ||
module Pundit | ||
module Instrumenters | ||
# Instrumenter that supplies `authorize` | ||
class Authorization | ||
attr_reader :current_user | ||
|
||
def initialize(current_user = :current_user) | ||
@current_user = current_user | ||
end | ||
|
||
def instrument(_type, field) | ||
return field unless field.metadata[:authorize] | ||
old_resolve = field.resolve_proc | ||
resolve_proc = resolve_proc(current_user, | ||
old_resolve, | ||
field.metadata[:authorize]) | ||
field.redefine do | ||
resolve resolve_proc | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize | ||
def resolve_proc(current_user, old_resolve, options) | ||
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize | ||
lambda do |obj, args, ctx| | ||
begin | ||
result = if options[:proc] | ||
options[:proc].call(obj, args, ctx) | ||
else | ||
query = options[:query].to_s + '?' | ||
record = options[:record] || obj | ||
::Pundit.authorize(ctx[current_user], record, query) | ||
end | ||
raise ::Pundit::NotAuthorizedError unless result | ||
old_resolve.call(obj, args, ctx) | ||
rescue ::Pundit::NotAuthorizedError | ||
if options[:raise] | ||
raise GraphQL::ExecutionError, | ||
"You're not authorized to do this" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pundit' | ||
|
||
module GraphQL | ||
module Pundit | ||
module Instrumenters | ||
# Instrumenter that supplies `scope` | ||
class Scope | ||
attr_reader :current_user | ||
|
||
def initialize(current_user = :current_user) | ||
@current_user = current_user | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize | ||
def instrument(_type, field) | ||
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize | ||
scope = field.metadata[:scope] | ||
return field unless scope | ||
unless valid_value?(scope) | ||
raise ArgumentError, 'Invalid value passed to `scope`' | ||
end | ||
|
||
old_resolve = field.resolve_proc | ||
|
||
scope_proc = lambda do |obj, _args, ctx| | ||
::Pundit.policy_scope!(ctx[current_user], obj) | ||
end | ||
scope_proc = scope if proc?(scope) | ||
|
||
field.redefine do | ||
resolve(lambda do |obj, args, ctx| | ||
new_scope = scope_proc.call(obj, args, ctx) | ||
old_resolve.call(new_scope, args, ctx) | ||
end) | ||
end | ||
end | ||
|
||
private | ||
|
||
def valid_value?(value) | ||
inferred?(value) || proc?(value) | ||
end | ||
|
||
def proc?(value) | ||
value.respond_to?(:call) | ||
end | ||
|
||
def inferred?(value) | ||
value == :infer_scope | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
class ScopeTest | ||
def initialize(value) | ||
@value = value | ||
end | ||
|
||
def where(&block) | ||
ScopeTest.new(@value.select(&block)) | ||
end | ||
|
||
def to_a | ||
@value | ||
end | ||
end | ||
|
||
class ScopeTestPolicy | ||
class Scope | ||
attr_reader :scope | ||
|
||
def initialize(_, scope) | ||
@scope = scope | ||
end | ||
|
||
def resolve | ||
scope.where { |e| e.to_i < 20 } | ||
end | ||
end | ||
|
||
def initialize(_, _); end | ||
|
||
def test? | ||
nil | ||
end | ||
end | ||
|
||
RSpec.describe GraphQL::Pundit::Instrumenters::Scope do | ||
let(:instrumenter) { GraphQL::Pundit::Instrumenter.new } | ||
let(:instrumented_field) { instrumenter.instrument(nil, field) } | ||
let(:result) { instrumented_field.resolve(subject, {}, {}) } | ||
|
||
subject { ScopeTest.new([1, 2, 3, 22, 48]) } | ||
|
||
context 'without authorization' do | ||
context 'inferred scope' do | ||
let(:field) do | ||
GraphQL::Field.define(type: 'String') do | ||
name :notTest | ||
scope | ||
resolve ->(obj, _args, _ctx) { obj.to_a } | ||
end | ||
end | ||
|
||
it 'filters the list' do | ||
expect(result).to match_array([1, 2, 3]) | ||
end | ||
end | ||
|
||
context 'explicit scope' do | ||
let(:field) do | ||
GraphQL::Field.define(type: 'String') do | ||
name :notTest | ||
scope ->(scope, _args, _ctx) { scope.where { |e| e > 20 } } | ||
resolve ->(obj, _args, _ctx) { obj.to_a } | ||
end | ||
end | ||
|
||
it 'filters the list' do | ||
expect(result).to match_array([22, 48]) | ||
end | ||
end | ||
end | ||
|
||
context 'with authorization' do | ||
context 'inferred scope' do | ||
let(:field) do | ||
GraphQL::Field.define(type: 'String') do | ||
name :test | ||
authorize | ||
scope | ||
resolve ->(obj, _args, _ctx) { obj.to_a } | ||
end | ||
end | ||
|
||
it 'returns nil' do | ||
expect(result).to eq(nil) | ||
end | ||
end | ||
|
||
context 'explicit scope' do | ||
let(:field) do | ||
GraphQL::Field.define(type: 'String') do | ||
name :test | ||
authorize | ||
scope ->(scope, _args, _ctx) { scope.where { |e| e > 20 } } | ||
resolve ->(obj, _args, _ctx) { obj.to_a } | ||
end | ||
end | ||
|
||
it 'returns nil' do | ||
expect(result).to eq(nil) | ||
end | ||
end | ||
end | ||
|
||
context 'invalid scope argument' do | ||
let(:field) do | ||
GraphQL::Field.define(type: 'String') do | ||
name :test | ||
authorize | ||
scope 'invalid value' | ||
resolve ->(obj, _args, _ctx) { obj.to_a } | ||
end | ||
end | ||
|
||
it 'raises an error' do | ||
expect { result }.to raise_error(ArgumentError) | ||
end | ||
end | ||
end |