authorizy 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +29 -19
- data/lib/authorizy/config.rb +2 -1
- data/lib/authorizy/core.rb +1 -1
- data/lib/authorizy/extension.rb +5 -13
- data/lib/authorizy/version.rb +1 -1
- data/spec/authorizy/config/current_user_spec.rb +1 -3
- data/spec/authorizy/config/field_spec.rb +29 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bdbf8fe26ec2fa456858b922f21efa63830f329018f07d6c2bed196d7e5cd8a
|
4
|
+
data.tar.gz: 49ad3862405c7707a3ab83c458e8c950d4fcf8b3e6488d7edc6fa6416e912cd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88568952618d7984a5ec9b548a74adaa64a263d4bfecdae71ec5e28225df8a5a93d5febf6aedb5a27a87f22ced4c066da8b330a5e8c96bc77abc7cea71cbde20
|
7
|
+
data.tar.gz: 2be439b99e310b42fca0122ba1a2c7bdf79d932a38be47e09722f6308763f73f25f76ad3481874ac0f7bd3b3e6ea7f33728c1605d372aba71a15c6de8143ffc7
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -84,25 +84,6 @@ Authorizy.configure do |config|
|
|
84
84
|
end
|
85
85
|
```
|
86
86
|
|
87
|
-
### Dependencies
|
88
|
-
|
89
|
-
You can allow access to one or more controllers and actions based on your permissions. It'll consider not only the `action`, like [aliases](#aliases) but the controller either.
|
90
|
-
|
91
|
-
```ruby
|
92
|
-
Authorizy.configure do |config|
|
93
|
-
config.dependencies = {
|
94
|
-
payments: {
|
95
|
-
index: [
|
96
|
-
['system/users', :index],
|
97
|
-
['system/enrollments', :index],
|
98
|
-
]
|
99
|
-
}
|
100
|
-
}
|
101
|
-
end
|
102
|
-
```
|
103
|
-
|
104
|
-
So now if a have the permission `payments#index` I'll receive more two permissions: `users#index` and `enrollments#index`.
|
105
|
-
|
106
87
|
### Cop
|
107
88
|
|
108
89
|
Sometimes we need to allow access in runtime because the permission will depend on the request data and/or some dynamic logic. For this you can create a *Cop* class, that inherits from `Authorizy::BaseCop`, to allow it based on logic. It works like a [Interceptor](https://en.wikipedia.org/wiki/Interceptor_pattern).
|
@@ -161,6 +142,35 @@ Authorizy.configure do |config|
|
|
161
142
|
end
|
162
143
|
```
|
163
144
|
|
145
|
+
### Dependencies
|
146
|
+
|
147
|
+
You can allow access to one or more controllers and actions based on your permissions. It'll consider not only the `action`, like [aliases](#aliases) but the controller either.
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
Authorizy.configure do |config|
|
151
|
+
config.dependencies = {
|
152
|
+
payments: {
|
153
|
+
index: [
|
154
|
+
['system/users', :index],
|
155
|
+
['system/enrollments', :index],
|
156
|
+
]
|
157
|
+
}
|
158
|
+
}
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
So now if a have the permission `payments#index` I'll receive more two permissions: `users#index` and `enrollments#index`.
|
163
|
+
|
164
|
+
### Field
|
165
|
+
|
166
|
+
By default the permissions are located inside the field called `authorizy` in the configured `current_user`. You can change how this field is fetched:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
Authorizy.configure do |config|
|
170
|
+
@field = ->(current_user) { current_user.profile.authorizy }
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
164
174
|
### Redirect URL
|
165
175
|
|
166
176
|
When authorization fails and the request is not a XHR request a redirect happens to `/` path. You can change it:
|
data/lib/authorizy/config.rb
CHANGED
@@ -2,13 +2,14 @@
|
|
2
2
|
|
3
3
|
module Authorizy
|
4
4
|
class Config
|
5
|
-
attr_accessor :aliases, :
|
5
|
+
attr_accessor :aliases, :cop, :current_user, :dependencies, :field, :redirect_url
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
@aliases = {}
|
9
9
|
@cop = Authorizy::BaseCop
|
10
10
|
@current_user = ->(context) { context.respond_to?(:current_user) ? context.current_user : nil }
|
11
11
|
@dependencies = {}
|
12
|
+
@field = ->(current_user) { current_user.respond_to?(:authorizy) ? current_user.authorizy : {} }
|
12
13
|
@redirect_url = ->(context) { context.respond_to?(:root_url) ? context.root_url : '/' }
|
13
14
|
end
|
14
15
|
end
|
data/lib/authorizy/core.rb
CHANGED
data/lib/authorizy/extension.rb
CHANGED
@@ -8,38 +8,30 @@ module Authorizy
|
|
8
8
|
helper_method(:authorizy?)
|
9
9
|
|
10
10
|
def authorizy
|
11
|
-
return if
|
11
|
+
return if Authorizy::Core.new(authorizy_user, params, session, cop: authorizy_cop).access?
|
12
12
|
|
13
13
|
info = I18n.t('authorizy.denied', controller: params[:controller], action: params[:action])
|
14
14
|
|
15
15
|
return render(json: { message: info }, status: 401) if request.xhr?
|
16
16
|
|
17
|
-
redirect_to
|
17
|
+
redirect_to Authorizy.config.redirect_url.call(self), info: info
|
18
18
|
end
|
19
19
|
|
20
20
|
def authorizy?(controller, action)
|
21
21
|
params['controller'] = controller
|
22
22
|
params['action'] = action
|
23
23
|
|
24
|
-
|
24
|
+
Authorizy::Core.new(authorizy_user, params, session, cop: authorizy_cop).access?
|
25
25
|
end
|
26
26
|
|
27
27
|
private
|
28
28
|
|
29
|
-
def authorizy_core
|
30
|
-
Authorizy::Core
|
31
|
-
end
|
32
|
-
|
33
29
|
def authorizy_user
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
def authorizy_config
|
38
|
-
Authorizy.config
|
30
|
+
Authorizy.config.current_user.call(self)
|
39
31
|
end
|
40
32
|
|
41
33
|
def authorizy_cop
|
42
|
-
|
34
|
+
Authorizy.config.cop.new(authorizy_user, params, session)
|
43
35
|
end
|
44
36
|
end
|
45
37
|
end
|
data/lib/authorizy/version.rb
CHANGED
@@ -15,9 +15,7 @@ RSpec.describe Authorizy::Config, '#current_user' do
|
|
15
15
|
context 'when context does not respond to current_user' do
|
16
16
|
let!(:context) { 'context' }
|
17
17
|
|
18
|
-
it
|
19
|
-
expect(config.current_user.call(context)).to be(nil)
|
20
|
-
end
|
18
|
+
it { expect(config.current_user.call(context)).to be(nil) }
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Authorizy::Config, '#field' do
|
4
|
+
let!(:config) { described_class.new }
|
5
|
+
|
6
|
+
context 'when uses default value' do
|
7
|
+
context 'when current_user responds to authorizy' do
|
8
|
+
let!(:current_user) { OpenStruct.new(authorizy: { permissions: [%i[users index]] }) }
|
9
|
+
|
10
|
+
it 'is called' do
|
11
|
+
expect(config.field.call(current_user)).to eq(permissions: [%i[users index]])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when current_user does not respond to field' do
|
16
|
+
let!(:current_user) { nil }
|
17
|
+
|
18
|
+
it { expect(config.field.call(current_user)).to eq({}) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when uses custom value' do
|
23
|
+
it 'executes what you want' do
|
24
|
+
config.field = ->(current_user) { current_user[:value] }
|
25
|
+
|
26
|
+
expect(config.field.call({ value: 'value' })).to eq('value')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: authorizy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- spec/authorizy/config/cop_spec.rb
|
165
165
|
- spec/authorizy/config/current_user_spec.rb
|
166
166
|
- spec/authorizy/config/dependencies_spec.rb
|
167
|
+
- spec/authorizy/config/field_spec.rb
|
167
168
|
- spec/authorizy/config/initialize_spec.rb
|
168
169
|
- spec/authorizy/config/redirect_url_spec.rb
|
169
170
|
- spec/authorizy/cop/controller_spec.rb
|
@@ -217,6 +218,7 @@ test_files:
|
|
217
218
|
- spec/authorizy/config/cop_spec.rb
|
218
219
|
- spec/authorizy/config/current_user_spec.rb
|
219
220
|
- spec/authorizy/config/dependencies_spec.rb
|
221
|
+
- spec/authorizy/config/field_spec.rb
|
220
222
|
- spec/authorizy/config/initialize_spec.rb
|
221
223
|
- spec/authorizy/config/redirect_url_spec.rb
|
222
224
|
- spec/authorizy/cop/controller_spec.rb
|