Class: GraphQL::Authorization::Ability
- Inherits:
-
Object
- Object
- GraphQL::Authorization::Ability
- Defined in:
- lib/graphql/authorization/ability.rb
Instance Method Summary collapse
- #ability(user) ⇒ Object
- #allowed(type) ⇒ Object
-
#callSetArgs(object, *args) ⇒ Object
calls a proc-like object with args comensorate with it’s arity.
-
#canAccess(type, field, object = nil, args = {}) ⇒ Object
returns true if the user can access “field” on “type”.
-
#canExecute(type, args = {}) ⇒ Object
returns true if the user can execute queries of type, “type”.
-
#initialize(user) ⇒ Ability
constructor
A new instance of Ability.
-
#permit(type, options = {}) ⇒ Object
permits execution, all access by default.
Constructor Details
#initialize(user) ⇒ Ability
Returns a new instance of Ability.
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/graphql/authorization/ability.rb', line 2 def initialize(user) @user = user @ability = {} #default white list builtin scalars permit GraphQL::STRING_TYPE, execute: true, only: [] permit GraphQL::INT_TYPE, execute: true, only: [] permit GraphQL::FLOAT_TYPE, execute: true, only: [] permit GraphQL::ID_TYPE, execute: true, only: [] permit GraphQL::BOOLEAN_TYPE, execute: true, only: [] ability(user) end |
Instance Method Details
#ability(user) ⇒ Object
72 73 74 |
# File 'lib/graphql/authorization/ability.rb', line 72 def ability(user) raise NotImplementedError.new("must implmenet ability funciton") end |
#allowed(type) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/graphql/authorization/ability.rb', line 64 def allowed type if type.class == GraphQL::UnionType permit type, execute: true else permit type, execute: true, only: GraphQL::Authorization::All end end |
#callSetArgs(object, *args) ⇒ Object
calls a proc-like object with args comensorate with it’s arity
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/graphql/authorization/ability.rb', line 37 def callSetArgs(object,*args) arity = object&.arity || object.method(:call).arity if arity > 0 object.call(*args[0..arity-1]) elsif arity == 0 object.call() else object.call(*args) end end |
#canAccess(type, field, object = nil, args = {}) ⇒ Object
returns true if the user can access “field” on “type”
57 58 59 60 61 62 |
# File 'lib/graphql/authorization/ability.rb', line 57 def canAccess(type,field,object=nil,args={}) return false unless @ability[type] access = @ability[type].[field] return callSetArgs(access,object,args) if access.respond_to? :call access end |
#canExecute(type, args = {}) ⇒ Object
returns true if the user can execute queries of type, “type”
49 50 51 52 53 54 |
# File 'lib/graphql/authorization/ability.rb', line 49 def canExecute(type,args={}) return false unless @ability[type] execute = @ability[type]. return callSetArgs(execute,args) if execute.respond_to? :call execute end |
#permit(type, options = {}) ⇒ Object
permits execution, all access by default
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/graphql/authorization/ability.rb', line 17 def permit(type,={}) raise NameError.new("duplicate ability definition") if @ability.key? type ability_object = GraphQL::Authorization::AbilityType.new(type,nil,{}) if .key?(:except) && .key?(:only) raise ArgumentError.new("you cannot specify white list and black list") end if [:except] ability_object.access(type.fields.keys.map(&:to_sym) - [:except]) elsif [:only] ability_object.access([:only]) end ability_object.execute [:execute] if block_given? #note Proc.new creates a proc with the block given to the method ability_object.instance_eval(&Proc.new) end @ability[type] = ability_object end |