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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d7006471814660a18a36666a304b2a8178d0e6c206c08b4c7d84f1bb0619214
4
- data.tar.gz: 75484b0f5b9960632799937725244e38552e2ad90999ddb9390572cf1880f2e5
3
+ metadata.gz: 1bdbf8fe26ec2fa456858b922f21efa63830f329018f07d6c2bed196d7e5cd8a
4
+ data.tar.gz: 49ad3862405c7707a3ab83c458e8c950d4fcf8b3e6488d7edc6fa6416e912cd9
5
5
  SHA512:
6
- metadata.gz: b2591d21df9e87ac0161c6a03327797350f93b9a230733a2597f9ec703cd853a7de62c3e0fc52c1f6e2fe16fbaabc2c1317ce3e93a5c83284283262b750be875
7
- data.tar.gz: 4e2d661a5560bb7f2fd3aca9fb8846e147828bac74b3cea9bcfd803ac1a64a0a95de9d7b7a4a2019cc7ad31c41694ae73a21bd0fb770e9412f537dd406403816
6
+ metadata.gz: 88568952618d7984a5ec9b548a74adaa64a263d4bfecdae71ec5e28225df8a5a93d5febf6aedb5a27a87f22ced4c066da8b330a5e8c96bc77abc7cea71cbde20
7
+ data.tar.gz: 2be439b99e310b42fca0122ba1a2c7bdf79d932a38be47e09722f6308763f73f25f76ad3481874ac0f7bd3b3e6ea7f33728c1605d372aba71a15c6de8143ffc7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # v0.3.0
2
+
3
+ ## Features
4
+
5
+ - Added options `field` to customize how the authorizy field is fetched;
6
+
1
7
  # v0.2.2
2
8
 
3
9
  ## Fixes
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:
@@ -2,13 +2,14 @@
2
2
 
3
3
  module Authorizy
4
4
  class Config
5
- attr_accessor :aliases, :dependencies, :cop, :current_user, :redirect_url
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
@@ -50,7 +50,7 @@ module Authorizy
50
50
  end
51
51
 
52
52
  def user_permissions
53
- expand(@user.authorizy.try(:[], 'permissions'))
53
+ expand(Authorizy.config.field.call(@user).try(:[], 'permissions'))
54
54
  end
55
55
  end
56
56
  end
@@ -8,38 +8,30 @@ module Authorizy
8
8
  helper_method(:authorizy?)
9
9
 
10
10
  def authorizy
11
- return if authorizy_core.new(authorizy_user, params, session, cop: authorizy_cop).access?
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 authorizy_config.redirect_url.call(self), info: info
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
- authorizy_core.new(authorizy_user, params, session, cop: authorizy_cop).access?
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
- authorizy_config.current_user.call(self)
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
- authorizy_config.cop.new(authorizy_user, params, session)
34
+ Authorizy.config.cop.new(authorizy_user, params, session)
43
35
  end
44
36
  end
45
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Authorizy
4
- VERSION = '0.2.2'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -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 'returns nil' do
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.2.2
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-06 00:00:00.000000000 Z
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