controller_policies 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -10
- data/lib/ability.rb +3 -3
- data/lib/controller_policies/action_controller_patch.rb +4 -0
- data/lib/controller_policies/version.rb +1 -1
- data/lib/generators/templates/definitions.rb.tt +21 -19
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77ea62355c4991dfc6d3aa1da19d75bbb231e170c6451ed11cc43cb38d53f158
|
4
|
+
data.tar.gz: be3d3097f5385f681016c8fd7ee415fc3a6fbdf21acc1320ce6d8891c4f723d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 289d8b06bca9542f79799779f2fba6444434ce41b85725e0c89cea6a182f5add24c5a32dbfbd9ed52aae4607cd36c9cd830db4b780fecf050075d1c15b94bd4a
|
7
|
+
data.tar.gz: 0d06f4170f6d708f487ce08e4533b93aaaac9916e342fa7a58eaeae3a86be0fde188459ea2299819cc6cc4c553402fc8d80373a00d2c98a3136f6a86ffc3513d
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
Add this line to your application's Gemfile:
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
gem 'controller_policies', '~> 0
|
8
|
+
gem 'controller_policies', '~> 1.0'
|
9
9
|
```
|
10
10
|
|
11
11
|
## Usage
|
@@ -29,13 +29,15 @@ The `actions` key is an array of strings that contain a list of supported contro
|
|
29
29
|
For example, you have this definition:
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
module
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
32
|
+
module Policies
|
33
|
+
module Base
|
34
|
+
DEFINITIONS = {
|
35
|
+
code: 'policy_code',
|
36
|
+
name: 'Policy Name',
|
37
|
+
description: 'I am a policy.',
|
38
|
+
actions: ['feature_app/users', 'data_app/products#index', 'subscriptions']
|
39
|
+
}
|
40
|
+
end
|
39
41
|
end
|
40
42
|
```
|
41
43
|
|
@@ -122,7 +124,11 @@ Ability.all_codes
|
|
122
124
|
Filter abilities based on namespace. `queries` can be an array of Strings, Modules or Classes.
|
123
125
|
|
124
126
|
```ruby
|
125
|
-
Ability.where(FeatureOne, FeatureTwo, FeatureOne::SubFeatureA)
|
127
|
+
Ability.where(Policies::FeatureOne, Policies::FeatureTwo, Policies::FeatureOne::SubFeatureA)
|
128
|
+
```
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
Ability.where('/feature_one', '/feature_two', '/feature_one/sub_feature_a')
|
126
132
|
```
|
127
133
|
|
128
134
|
#### #match(expression)
|
@@ -130,7 +136,7 @@ Ability.where(FeatureOne, FeatureTwo, FeatureOne::SubFeatureA)
|
|
130
136
|
Match abilities based on a matching string or regex. The matcher is based on the namespace. `expression` can be a Regexp or String.
|
131
137
|
|
132
138
|
```ruby
|
133
|
-
Ability.match(/FeatureOne(::)?(.)*/)
|
139
|
+
Ability.match(/Policies::FeatureOne(::)?(.)*/)
|
134
140
|
```
|
135
141
|
|
136
142
|
### Instance Methods
|
data/lib/ability.rb
CHANGED
@@ -38,7 +38,7 @@ class Ability
|
|
38
38
|
queries.each do |query|
|
39
39
|
case query.class.to_s
|
40
40
|
when 'String'
|
41
|
-
results += all.select { |ability| ability.namespace.to_s == trim(query).camelize }
|
41
|
+
results += all.select { |ability| ability.namespace.to_s == "Policies::#{trim(query).camelize}" }
|
42
42
|
when 'Module', 'Class'
|
43
43
|
results += all.select { |ability| ability.namespace == query }
|
44
44
|
end
|
@@ -82,7 +82,7 @@ class Ability
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def definition_files_post_processing(file_path)
|
85
|
-
module_constant = convert_namespace(file_path)
|
85
|
+
module_constant = "Policies::#{convert_namespace(file_path)}".constantize
|
86
86
|
policy_definitions = module_constant::DEFINITIONS
|
87
87
|
policy_definitions.map do |policy_definition|
|
88
88
|
policy_definition[:namespace] = module_constant
|
@@ -101,7 +101,7 @@ class Ability
|
|
101
101
|
end
|
102
102
|
|
103
103
|
def convert_namespace(file_path)
|
104
|
-
trim(file_path[policy_path.to_s.length..-4].split('/')[0...-1].join('/')).camelize
|
104
|
+
trim(file_path[policy_path.to_s.length..-4].split('/')[0...-1].join('/')).camelize
|
105
105
|
end
|
106
106
|
end
|
107
107
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
3
|
+
module Policies
|
4
|
+
module <%= name.camelize %>
|
5
|
+
DEFINITIONS = [
|
6
|
+
{
|
7
|
+
# Used as identifier for the policy.
|
8
|
+
code: 'Policy-Code',
|
9
|
+
# Readable name for the policy.
|
10
|
+
name: 'Readable Policy Name',
|
11
|
+
# Readable description for the policy.
|
12
|
+
description: 'Short description of what this policy allows',
|
13
|
+
# Controller actions the policy applies to. It works as matchers based on routes.
|
14
|
+
# It can be empty for manual policy checking.
|
15
|
+
actions: ['feature_app/users', 'data_app/products#index', 'subscriptions']
|
16
|
+
},
|
17
|
+
{
|
18
|
+
code: 'Another-Policy-Code',
|
19
|
+
name: 'Another Policy',
|
20
|
+
description: 'Long description.',
|
21
|
+
}
|
22
|
+
].freeze
|
23
|
+
end
|
22
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: controller_policies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tien
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
|
-
rubygems_version: 3.5.
|
68
|
+
rubygems_version: 3.5.11
|
69
69
|
signing_key:
|
70
70
|
specification_version: 4
|
71
71
|
summary: Allows the developer to define policies for controllers.
|