u-authorization 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +53 -22
- data/lib/micro/authorization/permissions/checker.rb +36 -6
- data/lib/micro/authorization/permissions/model.rb +7 -7
- data/lib/micro/authorization/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3eed3ce0a228018f007c58ae1c1e70ae5b782b841f98ae3baad6519c6fc40531
|
4
|
+
data.tar.gz: 2201d27e01a1f206bf395334b536596dd88fb05760a986c1e29fe4a9253e3f2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 467a55948c8864903d7024c250dd4b9a93244fbb379dc16aa7d91f1f983ba7f5da6d14c6af8e39a52a9639aa30cad129dc84ad2e56bb34b4bfd6245491845809
|
7
|
+
data.tar.gz: 6c28d09268e034911b19b66a192b53057d21c8c0995fc16e3d1754249da968eeb51d71b074782e1867b2885c67437da25c8c3fb235b7826048f957dc35440514
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -34,16 +34,28 @@ $ gem install u-authorization
|
|
34
34
|
require 'ostruct'
|
35
35
|
require 'u-authorization'
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
'
|
41
|
-
|
42
|
-
|
37
|
+
module Permissions
|
38
|
+
ADMIN = {
|
39
|
+
'visit' => { 'any' => true },
|
40
|
+
'export' => { 'any' => true }
|
41
|
+
}
|
42
|
+
|
43
|
+
USER = {
|
44
|
+
'visit' => { 'except' => ['billings'] },
|
45
|
+
'export' => { 'except' => ['sales'] }
|
46
|
+
}
|
47
|
+
|
48
|
+
ALL = {
|
49
|
+
'admin' => ADMIN,
|
50
|
+
'user' => USER
|
43
51
|
}
|
44
|
-
)
|
45
52
|
|
46
|
-
|
53
|
+
def self.to(role)
|
54
|
+
ALL.fetch(role, 'user')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
user = OpenStruct.new(id: 1, role: 'user')
|
47
59
|
|
48
60
|
class SalesPolicy < Micro::Authorization::Policy
|
49
61
|
def edit?(record)
|
@@ -52,26 +64,26 @@ $ gem install u-authorization
|
|
52
64
|
end
|
53
65
|
|
54
66
|
authorization = Micro::Authorization::Model.build(
|
55
|
-
permissions: user.role
|
56
|
-
policies: { default: :sales, sales: SalesPolicy }
|
67
|
+
permissions: Permissions.to(user.role),
|
68
|
+
policies: { default: :sales, sales: SalesPolicy },
|
57
69
|
context: {
|
58
70
|
user: user,
|
59
71
|
to_permit: ['dashboard', 'controllers', 'sales', 'index']
|
60
72
|
}
|
61
73
|
)
|
62
74
|
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
75
|
+
# Info about the `context` data:
|
76
|
+
# 1. :to_permit is a required key
|
77
|
+
# 1.1. :permissions is an alternative of :to_permit key.
|
78
|
+
# 2. :user is an optional key
|
79
|
+
# 3. Any key different of :permissions, will be passed as a policy context.
|
68
80
|
|
69
81
|
# Verifying the permissions for the given context
|
70
|
-
authorization.permissions.to?('visit')
|
71
|
-
authorization.permissions.to?('
|
82
|
+
authorization.permissions.to?('visit') #=> true
|
83
|
+
authorization.permissions.to?('export') #=> false
|
72
84
|
|
73
85
|
# Verifying permission for a given feature in different contexts
|
74
|
-
has_permission_to = authorization.permissions.to('
|
86
|
+
has_permission_to = authorization.permissions.to('export')
|
75
87
|
has_permission_to.context?('billings') #=> true
|
76
88
|
has_permission_to.context?('sales') #=> false
|
77
89
|
|
@@ -81,15 +93,14 @@ $ gem install u-authorization
|
|
81
93
|
authorization.to(:sales).edit?(charge) #=> true
|
82
94
|
|
83
95
|
# :default is the only permitted key to receive
|
84
|
-
# another symbol as value (a policy reference).
|
96
|
+
# another symbol as a value (a policy reference).
|
85
97
|
authorization.to(:default).edit?(charge) #=> true
|
86
98
|
|
87
99
|
# #policy() method has a similar behavior of #to(),
|
88
|
-
# but if there is a policy
|
100
|
+
# but if there is a policy defined as ":default", it will be fetched and instantiated by default.
|
89
101
|
authorization.policy.edit?(charge) #=> true
|
90
102
|
authorization.policy(:sales).edit?(charge) #=> true
|
91
103
|
|
92
|
-
|
93
104
|
# Cloning the authorization changing only its context.
|
94
105
|
new_authorization = authorization.map(context: [
|
95
106
|
'dashboard', 'controllers', 'billings', 'index'
|
@@ -97,7 +108,27 @@ $ gem install u-authorization
|
|
97
108
|
|
98
109
|
new_authorization.permissions.to?('visit') #=> false
|
99
110
|
|
100
|
-
authorization
|
111
|
+
authorization.equal?(new_authorization) #=> false
|
112
|
+
|
113
|
+
#========================#
|
114
|
+
# Multi role permissions #
|
115
|
+
#========================#
|
116
|
+
|
117
|
+
authorization = Micro::Authorization::Model.build(
|
118
|
+
permissions: [Permissions::USER, Permissions::ADMIN], # An array of permissions
|
119
|
+
policies: { default: :sales, sales: SalesPolicy },
|
120
|
+
context: {
|
121
|
+
user: user,
|
122
|
+
to_permit: ['dashboard', 'controllers', 'sales', 'index']
|
123
|
+
}
|
124
|
+
)
|
125
|
+
|
126
|
+
authorization.permissions.to?('visit') #=> true
|
127
|
+
authorization.permissions.to?('export') #=> true
|
128
|
+
|
129
|
+
has_permission_to = authorization.permissions.to('export')
|
130
|
+
has_permission_to.context?('billings') #=> true
|
131
|
+
has_permission_to.context?('sales') #=> true
|
101
132
|
```
|
102
133
|
|
103
134
|
## Original implementation
|
@@ -36,16 +36,46 @@ module Micro
|
|
36
36
|
|
37
37
|
private_constant :CheckRole
|
38
38
|
|
39
|
-
class
|
40
|
-
attr_reader :
|
39
|
+
class RoleChecker
|
40
|
+
attr_reader :required_context
|
41
41
|
|
42
|
-
def initialize(role,
|
43
|
-
@role = role
|
44
|
-
@required_features = Utils.values_as_downcased_strings(features)
|
42
|
+
def initialize(role, required_context)
|
43
|
+
@role, @required_context = role, required_context
|
45
44
|
end
|
46
45
|
|
46
|
+
def context?(_context)
|
47
|
+
raise NotImplementedError
|
48
|
+
end
|
49
|
+
|
50
|
+
def required_features
|
51
|
+
warn "[DEPRECATION] `#{self.class.name}#required_features` is deprecated.\nPlease use `#{self.class.name}#required_context` instead."
|
52
|
+
required_context
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class SingleRoleChecker < RoleChecker
|
57
|
+
def context?(context)
|
58
|
+
CheckRole.call(context, @role, @required_context)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class MultiRoleChecker < RoleChecker
|
47
63
|
def context?(context)
|
48
|
-
|
64
|
+
@role.any? do |role|
|
65
|
+
CheckRole.call(context, role, @required_context)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private_constant :RoleChecker, :SingleRoleChecker, :MultiRoleChecker
|
71
|
+
|
72
|
+
module Checker
|
73
|
+
def self.of(role, required_context:)
|
74
|
+
checker = role.is_a?(Array) ? MultiRoleChecker : SingleRoleChecker
|
75
|
+
checker.new(
|
76
|
+
role,
|
77
|
+
Utils.values_as_downcased_strings(required_context)
|
78
|
+
)
|
49
79
|
end
|
50
80
|
end
|
51
81
|
end
|
@@ -10,22 +10,22 @@ module Micro
|
|
10
10
|
@context = Utils.values_as_downcased_strings(context).freeze
|
11
11
|
end
|
12
12
|
|
13
|
-
def to(
|
14
|
-
Permissions::Checker.
|
13
|
+
def to(required_context)
|
14
|
+
Permissions::Checker.of(@role, required_context: required_context)
|
15
15
|
end
|
16
16
|
|
17
|
-
def to?(
|
18
|
-
has_permission_to = to(
|
17
|
+
def to?(required_context = nil)
|
18
|
+
has_permission_to = to(required_context)
|
19
19
|
|
20
|
-
cache_key = has_permission_to.
|
20
|
+
cache_key = has_permission_to.required_context.inspect
|
21
21
|
|
22
22
|
return @cache[cache_key] unless @cache[cache_key].nil?
|
23
23
|
|
24
24
|
@cache[cache_key] = has_permission_to.context?(@context)
|
25
25
|
end
|
26
26
|
|
27
|
-
def to_not?(
|
28
|
-
!to?(
|
27
|
+
def to_not?(required_context = nil)
|
28
|
+
!to?(required_context)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-authorization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|