nexus_cqrs_auth 0.0.5 → 1.2.0

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: ba5ea106ddd9ac8566584d353c718a1b2a861c1308301c855d758e54a3928bad
4
- data.tar.gz: 461fda9b1ffa501529c8e07d91acface837f7bbbfba1dd1de193eb85b9f4c207
3
+ metadata.gz: 81a584f66bd17223f77be8efc9e55cd688776ef5bbafda83a287dcfdd9d56930
4
+ data.tar.gz: 482568a0f4bcb831714d0a97939d24392b174c0ccd14d12a61322501ed708fe7
5
5
  SHA512:
6
- metadata.gz: 313f9218ba94bee5cd4e62b79eca24774582de4023f072670f6e7f3683c95ab6e113b0199020da152ecebc9b5cb5f06e9d765d471a2708e87a481236610b2489
7
- data.tar.gz: 0b735178b3e5ab4b428a7fd6b29d8451dc95ec4d5f2aaf4d9631ff347adab6f0e9f04e5c76581542303c34eba3d877fbb6f140402a4a2e93e5f3ca5802f66e06
6
+ metadata.gz: bc0c0a9191fa3b5972264806b164fdaa8c67f1c290d7918700de636f6d2d32dfe250392c98bdf06ca5fbee919c87a7b0f31c857534b14934137e0f154d9d4594
7
+ data.tar.gz: 69b83b159ef8202a0fe65b21aeb5848e5aa3e326f6677033c51c72507b97fb2c5bb6d428279dc6f5b4a0330fa6c06395cf669305fa7863e1fdaa3fc486f149ff
data/.gitlab-ci.yml CHANGED
@@ -22,7 +22,7 @@ release:
22
22
  - if: '$CI_COMMIT_TAG'
23
23
  script:
24
24
  - mkdir -p ~/.gem
25
- - cp /builds/pub/nexus_cqrs_auth.tmp/RUBYGEMS_CREDENTIALS ~/.gem/credentials
25
+ - cp $RUBYGEMS_CREDENTIALS ~/.gem/credentials
26
26
  - chmod 0600 ~/.gem/credentials
27
27
  - gem update --system
28
28
  - ruby --version
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  gemspec
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'pundit'
2
3
  require 'strings-case'
3
4
 
@@ -6,13 +7,21 @@ module NexusCqrsAuth
6
7
  def authorize(command, record, query = nil, policy_class: nil)
7
8
  query ||= Strings::Case.snakecase(command.demodularised_class_name) + '?'
8
9
  @command_user = command.metadata[:current_user]
10
+ @global_permissions = command.metadata[:global_permissions]
9
11
  super_ = super(record, query, policy_class: policy_class)
10
12
  @command_user = nil
13
+ @global_permissions = nil
11
14
  super_
12
15
  end
13
16
 
17
+ # Helper method for creating a permissions provider object from a query object. This allows certain permissions
18
+ # to be checked inside the command handler, as opposed to inside the policy
19
+ def permission_provider(query)
20
+ PermissionProvider.new(query.metadata[:current_user], query.metadata[:global_permissions])
21
+ end
22
+
14
23
  def pundit_user
15
- @command_user || super || nil
24
+ UserContext.new(@command_user, @global_permissions)
16
25
  end
17
26
 
18
27
  def current_user
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'nexus_cqrs'
2
3
  require 'pundit'
3
4
 
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ # Concern used to integrate models to the permissions system. Including this module to a model will assume the
3
+ # model can be "owned" by a user. When the model is created, permissions will automatically be assigned to the user
4
+ # and permissions can be validated and "repaired" retroactively.
5
+ module NexusCqrsAuth
6
+ module Ownable
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class_attribute :granted_permissions, default: {}
11
+
12
+ # Default relationship to <class>_permissions
13
+ class_attribute :permissions_relation, default: top_ancestor_class.name.downcase + "_permissions"
14
+ class_attribute :owner_column, default: :user_id
15
+
16
+ before_create :assign_permissions
17
+
18
+ define_model_callbacks :create_permissions
19
+ end
20
+
21
+ module ClassMethods
22
+ # Gets the class that this module is included in
23
+ def top_ancestor_class
24
+ ancestors.first
25
+ end
26
+ end
27
+
28
+ def assign_permissions
29
+ # As we are doing this on before_create, we must "build" this association, as opposed to creating it. This
30
+ # ensures validation is passed before saving this parent Collection.
31
+ granted_permissions.each do |p|
32
+ unless permissions.where(permission: p, entity_id: id, user_id: owner_id).exists?
33
+ permissions.build(user_id: owner_id, permission: p)
34
+ end
35
+ end
36
+ end
37
+
38
+ def validate_owner_permissions
39
+ end
40
+
41
+ def owner_id
42
+ send(owner_column)
43
+ end
44
+
45
+ def permissions
46
+ if respond_to?(permissions_relation)
47
+ return send(permissions_relation)
48
+ end
49
+
50
+ raise OwnableRelationshipNotSet, "Permissions relation not set.
51
+ Set it on your model with `self.permissions_relation = :xxx_permissions`, and ensure the relationship has been created"
52
+ end
53
+ end
54
+
55
+ class OwnableRelationshipNotSet < StandardError
56
+ end
57
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ module NexusCqrsAuth
3
+ class PermissionProvider
4
+ def initialize(user_id, global_permissions)
5
+ @user_id = user_id
6
+ @global_permissions = parse_permissions_array(global_permissions)
7
+ end
8
+
9
+ # Returns true if the current user has the requested permission on the requested entity (if passed), or globally
10
+ #
11
+ # @param [String] permission_key Permission key to check against
12
+ # @param [ApplicationRecord] permission_model Permission model
13
+ # @param [Integer] entity_id ID of the entity
14
+ # @return [Boolean] Returns true if the current user has this permission on this entity
15
+ # @example Check for permission
16
+ # permissions.has_permission?('collection:publish', CollectionPermissions, collection.id) #=> true
17
+ def has_permission?(permission_key, permission_model = nil, entity_id = nil)
18
+ return false if @user_id.nil?
19
+
20
+ return true if @global_permissions.include?(permission_key)
21
+
22
+ # check entity-specific permissions
23
+ unless permission_model.nil?
24
+ return true if permission_model.where(permission: permission_key, entity_id: entity_id,
25
+ user_id: @user_id).exists?
26
+ end
27
+
28
+ false
29
+ end
30
+
31
+ # Retrieves a list of permissions assigned to a user for a specific entity
32
+ #
33
+ # @param [ApplicationRecord] permission_model Permission model
34
+ # @param [Integer] entity_id ID of the entity
35
+ # @return [Array] Returns an array of hashes representing permission keys, along with their global status
36
+ # @example Get a list of permissions
37
+ # permissions.for_user(CollectionPermissions, collection.id) #=>
38
+ # [
39
+ # {:global=>false, :key=>"collection:discard"},
40
+ # {:global=>false, :key=>"collection:publish"},
41
+ # {:global=>false, :key=>"collection:view_under_moderation"},
42
+ # {:global=>false, :key=>"collection:set_status"}
43
+ # ]
44
+ def for_user_on_entity(permission_model, entity_id)
45
+ return [] if @user_id.nil?
46
+
47
+ # retrieve entity-specific permissions from DB and map to hash
48
+ entity_permissions = permission_model.where(user_id: @user_id, entity_id: entity_id)
49
+ .map { |p| { global: false, key: p.permission } }
50
+
51
+ # Map global permissions to hash
52
+ global_permissions = @global_permissions.map { |p| { global: true, key: p } }
53
+
54
+ # Combine hashes and ensure global permissions take priority
55
+ (global_permissions + entity_permissions).uniq { |p| p[:key] }
56
+ end
57
+
58
+ private
59
+
60
+ def parse_permissions_array(permissions_array)
61
+ return [] if permissions_array.nil?
62
+
63
+ permissions = []
64
+
65
+ permissions_array.each do |entity, action_array|
66
+ action_array.each do |action|
67
+ permissions << entity + ":" + action
68
+ end
69
+ end
70
+
71
+ permissions
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ module NexusCqrsAuth
3
+ # Class used to provide additional context into pundit. This enables us to not only pass the user model, but also the
4
+ # global permissions for that user - as those are pulled from the user's request, not the model.
5
+ class UserContext
6
+ attr_reader :user, :global_permissions
7
+
8
+ def initialize(user, global_permissions)
9
+ @user = user
10
+ @global_permissions = global_permissions
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module NexusCqrsAuth
2
- VERSION = '0.0.5'
3
+ VERSION = '1.2.0'
3
4
  end
@@ -1,2 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  require 'nexus_cqrs_auth/helper'
2
3
  require 'nexus_cqrs_auth/middleware'
4
+ require 'nexus_cqrs_auth/permission_provider'
5
+ require 'nexus_cqrs_auth/user_context'
6
+ require 'nexus_cqrs_auth/ownable'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require_relative 'lib/nexus_cqrs_auth/version'
2
3
 
3
4
  Gem::Specification.new do |spec|
@@ -15,7 +16,7 @@ Gem::Specification.new do |spec|
15
16
  %x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
17
  end
17
18
  spec.require_paths = ['lib']
18
- spec.add_dependency('nexus_cqrs', '~>0.1.1')
19
+ spec.add_dependency('nexus_cqrs', '~>0.2')
19
20
  spec.add_dependency('pundit')
20
21
  spec.add_dependency('strings-case')
21
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexus_cqrs_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Harrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-25 00:00:00.000000000 Z
11
+ date: 2021-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nexus_cqrs
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.1
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.1
26
+ version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pundit
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,6 +68,9 @@ files:
68
68
  - lib/nexus_cqrs_auth.rb
69
69
  - lib/nexus_cqrs_auth/helper.rb
70
70
  - lib/nexus_cqrs_auth/middleware.rb
71
+ - lib/nexus_cqrs_auth/ownable.rb
72
+ - lib/nexus_cqrs_auth/permission_provider.rb
73
+ - lib/nexus_cqrs_auth/user_context.rb
71
74
  - lib/nexus_cqrs_auth/version.rb
72
75
  - nexus_cqrs_auth.gemspec
73
76
  homepage:
@@ -88,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
91
  - !ruby/object:Gem::Version
89
92
  version: '0'
90
93
  requirements: []
91
- rubygems_version: 3.2.17
94
+ rubygems_version: 3.2.30
92
95
  signing_key:
93
96
  specification_version: 4
94
97
  summary: Authorisation for the Nexus CQRS pattern