ibrain-core 0.5.8 → 0.5.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c74180715e26a0b7365da263cc926c968a89176c3b80a7b70233839c8ac971f3
4
- data.tar.gz: 8f74dd650002174ec4311e83976c98ececf292b2b4e1179fd6c8bc6835ac35f3
3
+ metadata.gz: 330ce67ab6f228e04d3b6ca92feeb6dd7c6d604861abe797c8e582f2b15d431d
4
+ data.tar.gz: d77dc76e8f9665b905dcdc927200db46ce7cc535d730dc54a5841f0cc42fe54b
5
5
  SHA512:
6
- metadata.gz: f614df628d2a7ed54a25074acb7c5268d98c26a62b61045b9a6c2f33e0984318e491a5a4228bff5a65403b35e0a8edb571dbf3d1a665a47cb0f2748b17770bac
7
- data.tar.gz: 37c7d65e40af2ca14a814a9d73e0ac074c78cb6a626ef01e002bb56b34a565802f1cc80d1b6ae23baba67901e2e8e493960877340604b4468fe100db4d171252
6
+ metadata.gz: 69a63bc198060fd596b93fbd164827950261da9bcd8105426a3f5965b2442b0141844725f6906b70c1244b1567396be548535b6916c18a4dc9a01d137edbf63d
7
+ data.tar.gz: 192d6dc9fe457cd124eeb240bd111d44bab696db7e414b1ef16f3ac10b5d979c6f50d08bd010986bcd7b8b86dea51d9eeb9fd2c8a02c8b7671fb0ec39692afbe
@@ -4,7 +4,7 @@ module Ibrain
4
4
  module Extentions
5
5
  class ActiveRequired < GraphQL::Schema::FieldExtension
6
6
  def resolve(object:, arguments:, **rest)
7
- raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.session.is_deactivated') if is_invalid_session(object)
7
+ raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.session.is_deactivated') if is_inactivated(object)
8
8
 
9
9
  # yield the current time as `memo`
10
10
  yield(object, arguments, rest)
@@ -12,9 +12,9 @@ module Ibrain
12
12
 
13
13
  private
14
14
 
15
- def is_activated(object)
15
+ def is_inactivated(object)
16
16
  current_user = object.try(:context).try(:fetch, :current_user, nil)
17
- current_user.try(:is_activated?) && options.try(:fetch, :active_required, false)
17
+ current_user.try(:is_activated?)
18
18
  end
19
19
  end
20
20
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Extentions
5
+ class AuthorizeRequired < GraphQL::Schema::FieldExtension
6
+ def resolve(object:, arguments:, **rest)
7
+ raise IbrainErrors::PermissionError.new("You not have permission to access #{field&.name}") unless is_authorized(object)
8
+
9
+ # yield the current time as `memo`
10
+ yield(object, arguments, rest)
11
+ end
12
+
13
+ private
14
+
15
+ def is_authorized(object)
16
+ required_roles = Ibrain::Config.authorize_resource_enabled_with_roles
17
+ current_user = object.try(:context).try(:fetch, :current_user, nil)
18
+
19
+ role = current_user.try(:role) || current_user.try(:graphql_role)
20
+
21
+ return true unless required_roles.include?(role)
22
+
23
+ current_user.try(:is_authorized?, field.name)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -5,15 +5,20 @@ 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, roles: nil, active_required: true, **kwargs, &block)
8
+ def initialize(*args, session_required: true, roles: nil, active_required: true, authorize_required: true, **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
12
  extension(Ibrain::Extentions::Roles, roles: roles) if roles
13
13
 
14
- if ::Ibrain.config.is_require_activated_account && active_required
14
+ if Ibrain::Config.is_require_activated_account && active_required
15
15
  extension(Ibrain::Extentions::ActiveRequired)
16
16
  end
17
+
18
+ required_roles = Ibrain::Config.authorize_resource_enabled_with_roles || []
19
+ if required_roles.size.positive? && authorize_required
20
+ extension(Ibrain::Extentions::AuthorizeRequired)
21
+ end
17
22
  end
18
23
  end
19
24
  end
@@ -56,6 +56,9 @@ Ibrain.config do |config|
56
56
 
57
57
  # Enable require active
58
58
  config.is_require_activated_account = false
59
+
60
+ # Enabled authorize resource by user
61
+ config.authorize_resource_enabled_with_roles = %w[]
59
62
  end
60
63
 
61
64
  <% if defined?(Ibrain::Api::Engine) -%>
@@ -59,6 +59,9 @@ module Ibrain
59
59
 
60
60
  preference :is_require_activated_account, :boolean, default: false
61
61
 
62
+ # Enabled authorize resource by user
63
+ preference :authorize_resource_enabled_with_roles, :array, default: []
64
+
62
65
  def static_model_preferences
63
66
  @static_model_preferences ||= Ibrain::Preferences::StaticModelPreferences.new
64
67
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ibrain
4
- VERSION = "0.5.8"
4
+ VERSION = "0.5.10"
5
5
 
6
6
  def self.ibrain_version
7
7
  VERSION
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibrain-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.5.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tai Nguyen Van
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-31 00:00:00.000000000 Z
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord-session_store
@@ -166,6 +166,7 @@ files:
166
166
  - app/controllers/ibrain/core/graphql_controller.rb
167
167
  - app/graphql/ibrain/base_schema.rb
168
168
  - app/graphql/ibrain/extentions/active_required.rb
169
+ - app/graphql/ibrain/extentions/authorize_required.rb
169
170
  - app/graphql/ibrain/extentions/default_value.rb
170
171
  - app/graphql/ibrain/extentions/roles.rb
171
172
  - app/graphql/ibrain/extentions/session_required.rb