nexus_cqrs_auth 0.0.2 → 0.1.0

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: 12f88e69474120c6c77e39eb87c83087352ab327bbf3ccb31a25ea7a71ddd5af
4
- data.tar.gz: 9a87ded659ce541ebd65d0089a01d739fa562c2ab3ff7836d555e62a4db6e90e
3
+ metadata.gz: 9e062f14cca2a6a41d143bf648edbe21a6e661056fa5f8a408aeaea5c75d9b69
4
+ data.tar.gz: 686b79c46f969565dc23ba210bc48ff4a42f9c2f309b2592f82e0174e9e652b9
5
5
  SHA512:
6
- metadata.gz: c932a6ecd9cb000b4f1d70a3fff680a6abcbe4c86af7b1277faf04e3d44f50d453dbeacff310654055a026eecee01b2d0a68feef6225149258d00dd92f394404
7
- data.tar.gz: 86134c0025b0134282495d4ea9823e3fb3b28914e35b5937d93ff1a5ab63a4a23f1a677bae3bdb9d4ddac4485066267f868478c94164a2870dab663cb17b7c9f
6
+ metadata.gz: 3168a7a3ae6bb2070a2b3790043c5630560502515d7305d87a64d36cdfd0ed88f969c036d292452a6be6758f5f045f147a0a6c25ff46463ad3b697b2d418b935
7
+ data.tar.gz: 7f64fc4f86f877b19621728360d1f9cbd1ed1afedbff3c8808dc250e9b729af99477c0148af58b64042fa75c88d785296c71d7c55119285b06b05b0bec3db63b
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/README.md CHANGED
@@ -1 +1,149 @@
1
- nexus_cqrs_auth
1
+ # nexus_cqrs_auth
2
+
3
+ Authorisation for the Nexus CQRS pattern.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'nexus_cqrs_auth'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install nexus_cqrs_auth
20
+
21
+ ## Usage
22
+
23
+ When setting up the message bus, attach the authorisation middleware to it:
24
+
25
+ ```ruby
26
+ middleware_stack = Middleware::Builder.new do |b|
27
+ b.use NexusCqrsAuth::AuthMiddleware
28
+ end
29
+
30
+ bus = Bus.new(middleware: middleware_stack)
31
+ ```
32
+
33
+ You will also need to set metadata on each message (command/query) before dispatching it to the bus:
34
+
35
+ ```ruby
36
+ command.set_metadata(:current_user, user)
37
+ execute(command)
38
+ ```
39
+
40
+ How you set this data, and where you get the current user from is application specific.
41
+
42
+ For example, a helper included in all GraphQL types could look like this:
43
+
44
+ ```ruby
45
+ module GraphQlCqrsHelpers
46
+ def execute(command)
47
+ command_executor.execute(enrich_message(command))
48
+ end
49
+
50
+ def query(query)
51
+ query_executor.execute(enrich_message(query))
52
+ end
53
+
54
+ def command_executor
55
+ @command_executor ||= $COMMAND_EXECUTOR
56
+ end
57
+
58
+ def query_executor
59
+ @query_executor ||= $QUERY_EXECUTOR
60
+ end
61
+
62
+ private
63
+
64
+ def enrich_message(message)
65
+ message.set_metadata(:current_user, @context[:current_user])
66
+ message
67
+ end
68
+ end
69
+ ```
70
+
71
+ You can then write various policies to setup authorisation in CQRS flows.
72
+
73
+ More information about policies can be found in the [Pundit documentation](https://github.com/varvet/pundit).
74
+
75
+ Remember to create a base policy at: `app/policies/application_policy.rb`
76
+
77
+ ### Bus level policy
78
+
79
+ Create a policy class in `app/policies/my_message_policy.rb`
80
+
81
+ ```ruby
82
+ class MyMessagePolicy < ApplicationPolicy
83
+ def initialize(user, message)
84
+ @user = user
85
+ @query = message
86
+ end
87
+
88
+ def authorise?
89
+ true
90
+ end
91
+ end
92
+ ```
93
+
94
+ The `authorise?` method will be called before the message handler. If `authorise?` returns false, execution of the bus
95
+ will halt and a `Pundit::NotAuthorizedError` will be raised.
96
+
97
+ ### Record level policy
98
+
99
+ You can write policies for records:
100
+
101
+ ```ruby
102
+ class PostPolicy < ApplicationPolicy
103
+ def initialize(user, post)
104
+ @user = user
105
+ @post = post
106
+ end
107
+
108
+ def publish_post?
109
+ true
110
+ end
111
+ end
112
+ ```
113
+
114
+ You can then authorise a particular `Post`s by calling the policy from a command handler:
115
+
116
+ ```ruby
117
+ class PublishPostHandler < NexusCqrs::BaseCommandHandler
118
+ include NexusCqrsAuth
119
+
120
+ # @param [Commands::PublishPost] command
121
+ def call(command)
122
+ post = Post.find(command.post_id)
123
+ authorize(command, post)
124
+ post.is_published = true
125
+ post.save
126
+ end
127
+ end
128
+ ```
129
+
130
+ The `NexusCqrsAuth` module must be included in the handler.
131
+
132
+ `authorize` should be called with the domain message (e.g. command) and the record. The policy for that record type
133
+ (e.g. `PostPolicy`) will be called and the scope with the same name as the command (`PublishPost` -> `publish_post?`)
134
+ will be called.
135
+
136
+ If the scope returns false, then a `Pundit::NotAuthorizedError` will be raised.
137
+
138
+ ## Development
139
+
140
+ To contribute to this gem, simple clone the repository, run `bundle install` and run tests:
141
+
142
+ ```shell script
143
+ bundle exec rspec
144
+ bundle exec rubocop
145
+ ```
146
+
147
+ ## Releasing
148
+
149
+ The release process is tied to the git tags. Simply creating a new tag and pushing will trigger a new release to rubygems.
@@ -12,6 +12,12 @@ module NexusCqrsAuth
12
12
  end
13
13
 
14
14
  def pundit_user
15
- @command_user || super
15
+ @command_user || super || nil
16
+ end
17
+
18
+ def current_user
19
+ return super if defined?(super)
20
+
21
+ nil
16
22
  end
17
23
  end
@@ -7,7 +7,9 @@ module NexusCqrsAuth
7
7
  include NexusCqrsAuth
8
8
 
9
9
  def call(message)
10
- authorize(message, message, :authorise?)
10
+ if Pundit::PolicyFinder.new(message).policy
11
+ authorize(message, message, :authorise?)
12
+ end
11
13
  @next.call(message)
12
14
  end
13
15
  end
@@ -1,3 +1,3 @@
1
1
  module NexusCqrsAuth
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
15
15
  %x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
16
  end
17
17
  spec.require_paths = ['lib']
18
- spec.add_dependency('nexus_cqrs', '~>0.1.1')
18
+ spec.add_dependency('nexus_cqrs', '~>0.2')
19
19
  spec.add_dependency('pundit')
20
20
  spec.add_dependency('strings-case')
21
21
  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.2
4
+ version: 0.1.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: 2020-10-01 00:00:00.000000000 Z
11
+ date: 2021-08-25 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
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 3.1.4
91
+ rubygems_version: 3.2.26
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Authorisation for the Nexus CQRS pattern