ibrain-core 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0accd0585dbb8b751342cfbeae014e22ef41b75b56bb3aba37f2071813307344
4
- data.tar.gz: b5e922dea8ec2515d5ee390271d04e9ba879d88d0b629f54868420d46771386e
3
+ metadata.gz: 9c01fb318f3aa91812d1bfa1e71f956260a51929f05bfe5a6ae642149f8219ba
4
+ data.tar.gz: d15d09f9e9b6761d2ab5424a04b1c12185898493034683a5394a482041e59f73
5
5
  SHA512:
6
- metadata.gz: be6f2b3e72bdba22100743dfa74b99b001ab17f5c4ebf3cc4218ab776667fc3761ad7db01326de8ed4c0d9cf5362f35cf66bbde2260fc943e72327e81a8557b0
7
- data.tar.gz: 47dd41b984584ca4e3bc820e9a07b18eafcbcd8312f5ccad9c03c322ca49ff56cc2bf43ac1d1360db61f87dca904cfb61edac157b6527ae94ce746e1224a2abf
6
+ metadata.gz: 4fd557d32f5ccdedcf916bccc2b3fd9d9af87a344276b9142946b9008b5b9681343801e80579a3564055b78c784cadf864a02faf014f1adddb423ad8d0c9b4da
7
+ data.tar.gz: b2c18c0af1ab23dab608e93e8a6634d23a5c90d21eb734320aabf9a6b3e4f99afc01557b5c7633b5cf10eecb1ec0a827e133bdd65e3124544bd6cd83566ae5a7
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Extentions
5
+ class Roles < GraphQL::Schema::FieldExtension
6
+ def after_resolve(object:, arguments:, **rest)
7
+ raise IbrainErrors::PermissionError.new("You not have permission to access #{field&.name}") if is_invalid_role(object)
8
+
9
+ # yield the current time as `memo`
10
+ yield(object, arguments, rest)
11
+ end
12
+
13
+ private
14
+
15
+ def is_invalid_role(object)
16
+ roles = options.try(:fetch, :roles, [])
17
+ current_user = object.try(:context).try(:fetch, :current_user, nil)
18
+ role = current_user.try(:role) || current_user.try(:graphql_role)
19
+
20
+ return if roles.blank?
21
+ return true if current_user.blank?
22
+ return false if roles.include?(role)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -13,7 +13,7 @@ module Ibrain
13
13
  private
14
14
 
15
15
  def is_invalid_session(object)
16
- object.try(:contect).try(:fetch, :current_user, nil).blank? && options.try(:fetch, :session_required, false)
16
+ object.try(:context).try(:fetch, :current_user, nil).blank? && options.try(:fetch, :session_required, false)
17
17
  end
18
18
  end
19
19
  end
@@ -5,10 +5,11 @@ module Ibrain
5
5
  class BaseApiField < GraphQL::Schema::Field
6
6
  argument_class ::Ibrain::Types::BaseArgument
7
7
 
8
- def initialize(*args, session_required: true, **kwargs, &block)
8
+ def initialize(*args, session_required: true, roles: nil, **kwargs, &block)
9
9
  super(*args, camelize: false, **kwargs, &block)
10
10
 
11
11
  extension(Ibrain::Extentions::SessionRequired, session_required: session_required) if session_required
12
+ extension(Ibrain::Extentions::Roles, roles: roles) if roles
12
13
  end
13
14
  end
14
15
  end
@@ -5,10 +5,11 @@ module Ibrain
5
5
  class BaseField < GraphQL::Schema::Field
6
6
  argument_class ::Ibrain::Types::BaseArgument
7
7
 
8
- def initialize(*args, default_value: nil, **kwargs, &block)
8
+ def initialize(*args, default_value: nil, roles: nil, **kwargs, &block)
9
9
  super(*args, camelize: false, **kwargs, &block)
10
10
 
11
11
  extension(::Ibrain::Extentions::DefaultValue, default_value: default_value) unless default_value.nil?
12
+ extension(Ibrain::Extentions::Roles, roles: roles) if roles
12
13
  end
13
14
  end
14
15
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ibrain
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
 
6
6
  def self.ibrain_version
7
7
  VERSION
8
8
  end
9
9
 
10
10
  def self.previous_ibrain_minor_version
11
- '0.2.1'
11
+ '0.2.2'
12
12
  end
13
13
 
14
14
  def self.ibrain_gem_version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibrain-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tai Nguyen Van
@@ -186,6 +186,7 @@ files:
186
186
  - app/controllers/ibrain/core/graphql_controller.rb
187
187
  - app/graphql/ibrain/base_schema.rb
188
188
  - app/graphql/ibrain/extentions/default_value.rb
189
+ - app/graphql/ibrain/extentions/roles.rb
189
190
  - app/graphql/ibrain/extentions/session_required.rb
190
191
  - app/graphql/ibrain/interfaces/base_interface.rb
191
192
  - app/graphql/ibrain/interfaces/person_interface.rb