controller_policies 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 +7 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/Rakefile +8 -0
- data/controller_policies.gemspec +35 -0
- data/lib/ability.rb +103 -0
- data/lib/controller_policies/action_controller_patch.rb +14 -0
- data/lib/controller_policies/enforcement.rb +44 -0
- data/lib/controller_policies/railtie.rb +28 -0
- data/lib/controller_policies/version.rb +5 -0
- data/lib/controller_policies.rb +19 -0
- data/lib/generators/policy_definition_generator.rb +20 -0
- data/lib/generators/templates/definitions.rb.tt +22 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: db99c9c096c0c752d9d035f928f7c92e9f6efff716975d2e4672b3c18443b16a
|
4
|
+
data.tar.gz: 01a131a24ad0f546ada81e5d10c1d27564591147c09004a97e6691150d18be71
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 05ebbc1bad897b0b6fecf8280f625a5f4d73013255c0dee1e7ab5a6e564fc9aedd21772b9b0bd27d159cf698e09377b3b829dcf5ee60584c95a250841536a336
|
7
|
+
data.tar.gz: 5e23aa2064c53ea42acea98b1bffaa89767558e4fe2a7a7938b2331b258f0dc0c661f1b8542d866cacf44ec453a222e34019b0c8b7e1b77e1091be89281b84bd
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# Controller Policies
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'controller_policies', '~> 0.1'
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
## Generating Definitions
|
14
|
+
|
15
|
+
This gem takes advantage of placing all your policy definitions for controllers in one folder (`app/policies`). To generate a policy, run:
|
16
|
+
|
17
|
+
```sh
|
18
|
+
rails g policy_definition my/namespace
|
19
|
+
```
|
20
|
+
|
21
|
+
This will generate a file: `app/policies/my/namespace/definitions.rb`
|
22
|
+
|
23
|
+
The developer should edit this file and add the policies for the app.
|
24
|
+
|
25
|
+
### `actions` key
|
26
|
+
|
27
|
+
The `actions` key is an array of strings that contain a list of supported controller actions for automation of policy checking.
|
28
|
+
|
29
|
+
For example, you have this definition:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
module Base
|
33
|
+
DEFINITIONS = {
|
34
|
+
code: 'policy_code',
|
35
|
+
name: 'Policy Name',
|
36
|
+
description: 'I am a policy.',
|
37
|
+
actions: ['feature_app/users', 'data_app/products#index', 'subscriptions']
|
38
|
+
}
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
The above definition will have enforced policies on `Base::FeatureApp::UsersController` (all actions), `Base::DataApp::ProductsController` (`index` action) and `Base::SubscriptionController`.
|
43
|
+
|
44
|
+
**Do note that the `actions` array implement *Regular Expression Matching*. That means that if you have multiple controllers with the same name on their parent namespace, the parent will be matched first.** To avoid this problem, simply add the namespace to match the intended child instead.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
module Base
|
48
|
+
DEFINITIONS = {
|
49
|
+
code: 'policy_code',
|
50
|
+
name: 'Policy Name',
|
51
|
+
description: 'I am a policy.',
|
52
|
+
actions: ['feature_app/users', 'another_base/data_app/products#index', 'base/subscriptions']
|
53
|
+
}
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Adding Policy Enforcement in Controllers
|
58
|
+
|
59
|
+
Simply add the line `has_enforced_policies`, and pass a block with one argument (`ability_code`), or override the `ability?(ability_code)` method.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class MyController < ApplicationController
|
63
|
+
has_enforced_policies do |ability_code|
|
64
|
+
current_user.abilities.include? ability_code
|
65
|
+
end
|
66
|
+
|
67
|
+
# ...
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class MyController < ApplicationController
|
73
|
+
has_enforced_policies
|
74
|
+
|
75
|
+
def ability?(ability_code)
|
76
|
+
current_user.abilities.include? ability_code
|
77
|
+
end
|
78
|
+
# ...
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
Since storing abilities are very flexible and there are truly infinite ways of doing it, *this gem did not support that feature.* Instead, the developer must define their own ability checking.
|
83
|
+
|
84
|
+
## Ability
|
85
|
+
|
86
|
+
The Ability class is a model for abilities that come from the definition files.
|
87
|
+
|
88
|
+
### Class Methods
|
89
|
+
|
90
|
+
#### #all
|
91
|
+
|
92
|
+
Get all abilities from all definitions.
|
93
|
+
|
94
|
+
#### #all_codes
|
95
|
+
|
96
|
+
Get all ability codes from all definitions.
|
97
|
+
|
98
|
+
#### #where(query)
|
99
|
+
|
100
|
+
Filter abilities based on namespace. `query` can be a String, Module or Class.
|
101
|
+
|
102
|
+
#### #find(query)
|
103
|
+
|
104
|
+
Find an ability within a namespace. `query` can be a String, Module or Class.
|
105
|
+
|
106
|
+
#### #match(expression)
|
107
|
+
|
108
|
+
Match abilities based on a matching string or regex. The matcher is based on the namespace. `expression` can be a Regexp or String.
|
109
|
+
|
110
|
+
#### #mill(expression)
|
111
|
+
|
112
|
+
Find an ability based on a matching string or regex. The matcher is based on the namespace. `expression` can be a Regexp or String.
|
113
|
+
|
114
|
+
### Instance Methods
|
115
|
+
|
116
|
+
#### #code
|
117
|
+
|
118
|
+
The code of the ability.
|
119
|
+
|
120
|
+
#### #name
|
121
|
+
|
122
|
+
The name of the ability.
|
123
|
+
|
124
|
+
#### #description
|
125
|
+
|
126
|
+
The description of the ability.
|
127
|
+
|
128
|
+
#### #actions
|
129
|
+
|
130
|
+
Controller actions that the ability can check against.
|
131
|
+
|
132
|
+
## Contributing
|
133
|
+
|
134
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tieeeeen1994/controller_policies. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tieeeeen1994/controller_policies/blob/master/CODE_OF_CONDUCT.md).
|
135
|
+
|
136
|
+
## License
|
137
|
+
|
138
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
139
|
+
|
140
|
+
## Code of Conduct
|
141
|
+
|
142
|
+
Everyone interacting in the ControllerPolicies project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tieeeeen1994/controller_policies/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/controller_policies/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'controller_policies'
|
7
|
+
spec.version = ControllerPolicies::VERSION
|
8
|
+
spec.authors = ['Tien']
|
9
|
+
spec.email = ['tieeeeen1994@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Allows the developer to define policies for controllers.'
|
12
|
+
spec.homepage = 'https://github.com/tieeeeen1994/rails-controller-policies'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = '>= 3.0.0'
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(__dir__) do
|
21
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
(File.expand_path(f) == __FILE__) ||
|
23
|
+
f.start_with?(*%w[spec/ .git .github Gemfile .vscode]) ||
|
24
|
+
f.end_with?(*%w[.gem .yml .rspec .gitignore])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
# Uncomment to register a new dependency of your gem
|
30
|
+
spec.add_dependency 'rails', '~> 7'
|
31
|
+
|
32
|
+
# For more information and examples about making a new gem, check out our
|
33
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
34
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
35
|
+
end
|
data/lib/ability.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Ability struct that should house the ability definitions.
|
4
|
+
class Ability
|
5
|
+
attr_reader :code, :name, :description, :actions, :namespace
|
6
|
+
|
7
|
+
# Constructor for Ability object
|
8
|
+
# code - The code of the ability (see definitions.rb)
|
9
|
+
# name - The name of the ability (see definitions.rb)
|
10
|
+
# namespace - The namespace of the ability. You don't need to worry about this.
|
11
|
+
# description - The description of the ability (see definitions.rb)
|
12
|
+
# actions - Controller actions that the ability can check against (see definitions.rb)
|
13
|
+
def initialize(code:, name:, namespace: nil, description: '', actions: [])
|
14
|
+
@code = code
|
15
|
+
@name = name
|
16
|
+
@description = description
|
17
|
+
@actions = actions
|
18
|
+
@namespace = namespace
|
19
|
+
end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
alias instantiate new
|
23
|
+
|
24
|
+
# Get all abilities from all definitions.
|
25
|
+
def all
|
26
|
+
definitions.map { |ability| instantiate(**ability) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get all ability codes from all definitions.
|
30
|
+
# Unused args. Need to allow filtering through namespace.
|
31
|
+
def all_codes(_query_string = nil)
|
32
|
+
definitions.pluck(:code)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Filter abilities based on namespace.
|
36
|
+
def where(query)
|
37
|
+
case query.class.to_s
|
38
|
+
when 'String'
|
39
|
+
all.select { |ability| ability.namespace.to_s == trim(query).camelize }
|
40
|
+
when 'Module', 'Class'
|
41
|
+
all.select { |ability| ability.namespace == query }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Find an ability within a namespace.
|
46
|
+
def find(query_string)
|
47
|
+
where(query_string).first
|
48
|
+
end
|
49
|
+
|
50
|
+
# Match abilities based on a matching string or regex. The matcher is based on the namespace.
|
51
|
+
def match(expression)
|
52
|
+
case expression.class.to_s
|
53
|
+
when 'String' then all.select { |ability| ability.namespace.to_s.match?(/#{trim(expression).camelize}/) }
|
54
|
+
when 'Regexp' then regex_matcher(expression)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Find an ability based on a matching string or regex. The matcher is based on the namespace.
|
59
|
+
def mill(expression)
|
60
|
+
match(expression).first
|
61
|
+
end
|
62
|
+
|
63
|
+
# Path to the policy folder.
|
64
|
+
def policy_path
|
65
|
+
@policy_path ||= Rails.root.join('app/policies')
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def definitions
|
71
|
+
@definitions ||= begin
|
72
|
+
data = []
|
73
|
+
Dir["#{policy_path}/**/definitions.rb"].each do |file_path|
|
74
|
+
data += definition_files_post_processing(file_path)
|
75
|
+
end
|
76
|
+
data
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def definition_files_post_processing(file_path)
|
81
|
+
module_constant = convert_namespace(file_path)
|
82
|
+
policy_definitions = module_constant::DEFINITIONS
|
83
|
+
policy_definitions.map do |policy_definition|
|
84
|
+
policy_definition[:namespace] = module_constant
|
85
|
+
policy_definition
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def regex_matcher(expression)
|
90
|
+
all.select do |ability|
|
91
|
+
ability.namespace.to_s.match?(expression) || ability.namespace.to_s.underscore.match?(expression)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def trim(namespace)
|
96
|
+
namespace.delete_prefix('/').delete_suffix('/')
|
97
|
+
end
|
98
|
+
|
99
|
+
def convert_namespace(file_path)
|
100
|
+
trim(file_path[policy_path.to_s.length..-4].split('/')[0...-1].join('/')).camelize.constantize
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ControllerPolicies
|
4
|
+
# Module containing action controller patches.
|
5
|
+
module ActionControllerPatch
|
6
|
+
def has_enforced_policies(&block) # rubocop:disable Naming/PredicateName
|
7
|
+
include ControllerPolicies::Enforcement
|
8
|
+
|
9
|
+
return unless block_given?
|
10
|
+
|
11
|
+
define_method(:ability?, &block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ControllerPolicies
|
4
|
+
# Concern that includes the enforcement of abilities.
|
5
|
+
module Enforcement
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
before_action :check_abilities_by_definition
|
10
|
+
|
11
|
+
# Main driver for checking abilities.
|
12
|
+
# It is still possible to manually check abilities by calling this method in a controller.
|
13
|
+
# Developers need to override this method to implement the own ability checking logic.
|
14
|
+
# It needs to accept an ability code and return a boolean.
|
15
|
+
def ability?(ability_code) # rubocop:disable Lint/UnusedMethodArgument
|
16
|
+
raise AbilityMethodUnimplementedError
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# Automatically check abilities based on definition provided in policies.
|
22
|
+
# It has the power to match based on controller, actions and wildcards.
|
23
|
+
def check_abilities_by_definition
|
24
|
+
applicable_abilities.each do |ability|
|
25
|
+
break unless ability?(ability.code)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Simply gets the controller namespace.
|
30
|
+
def controller_namespace
|
31
|
+
@controller_namespace ||= controller_path.split('/')[0...-1].join('/')
|
32
|
+
end
|
33
|
+
|
34
|
+
# Method that attempts to go through policy definitions and check if it matches the current controller.
|
35
|
+
# If it finds a match, it will be an applicable definition to check abilities for.
|
36
|
+
def applicable_abilities
|
37
|
+
@applicable_abilities ||= Ability.where(controller_namespace).select do |ability|
|
38
|
+
matching_actions = ability.actions.select { |action| /#{action}/.match?("#{controller_path}##{action_name}") }
|
39
|
+
matching_actions.present?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'action_controller_patch'
|
4
|
+
require_relative 'enforcement'
|
5
|
+
|
6
|
+
module ControllerPolicies
|
7
|
+
# Railtie for Breadcrumb Helper for Rails integration.
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
generators do
|
10
|
+
require_relative '../generators/policy_definition_generator'
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer 'controller_policies.action_controller' do |app|
|
14
|
+
app.reloader.to_prepare do
|
15
|
+
::ActionController::Base.singleton_class.prepend ActionControllerPatch
|
16
|
+
::ActionController::API.singleton_class.prepend ActionControllerPatch
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
initializer 'controller_policies.autoloaders' do
|
21
|
+
Rails.autoloaders.each do |autoloader|
|
22
|
+
autoloader.ignore(Rails.root.join('app/policies'))
|
23
|
+
end
|
24
|
+
|
25
|
+
Dir[Rails.root.join('app/policies/**/*.rb')].each { |definition| require definition }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'controller_policies/version'
|
4
|
+
|
5
|
+
raise LoadError, 'The project is not a Rails project!' unless defined?(Rails)
|
6
|
+
|
7
|
+
require_relative 'ability'
|
8
|
+
|
9
|
+
# Base namespace for the gem.
|
10
|
+
module ControllerPolicies
|
11
|
+
# Exception raised for unimplemented ability? method for those including ControllerPolicies::Enforcement.
|
12
|
+
class AbilityMethodUnimplementedError < RuntimeError
|
13
|
+
def initialize(_msg)
|
14
|
+
super('`ability?(ability_code)` method is not implemented')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
require_relative 'controller_policies/railtie'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Generates a definition file for a namespace.
|
4
|
+
class PolicyDefinitionGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('templates', __dir__)
|
6
|
+
desc 'Generates a policy definition file based on given namespace.'
|
7
|
+
argument :name, type: :string, required: true, desc: 'The namespace for the definition file.'
|
8
|
+
|
9
|
+
def create_policy_definition_file
|
10
|
+
return if processed_name.blank?
|
11
|
+
|
12
|
+
template 'definitions.rb', "app/policies/#{processed_name}/definitions.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def processed_name
|
18
|
+
@processed_name ||= name.strip.delete_prefix('/').delete_suffix('/')
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module <%= name.camelize %>
|
4
|
+
DEFINITIONS = [
|
5
|
+
{
|
6
|
+
# Used as identifier for the policy.
|
7
|
+
code: 'Policy-Code',
|
8
|
+
# Readable name for the policy.
|
9
|
+
name: 'Readable Policy Name',
|
10
|
+
# Readable description for the policy.
|
11
|
+
description: 'Short description of what this policy allows',
|
12
|
+
# Controller actions the policy applies to. It works as matchers based on routes.
|
13
|
+
# It can be empty for manual policy checking.
|
14
|
+
actions: ['feature_app/users', 'data_app/products#index', 'subscriptions']
|
15
|
+
},
|
16
|
+
{
|
17
|
+
code: 'Another-Policy-Code',
|
18
|
+
name: 'Another Policy',
|
19
|
+
description: 'Long description.',
|
20
|
+
}
|
21
|
+
].freeze
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: controller_policies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tien
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- tieeeeen1994@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CODE_OF_CONDUCT.md
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- controller_policies.gemspec
|
39
|
+
- lib/ability.rb
|
40
|
+
- lib/controller_policies.rb
|
41
|
+
- lib/controller_policies/action_controller_patch.rb
|
42
|
+
- lib/controller_policies/enforcement.rb
|
43
|
+
- lib/controller_policies/railtie.rb
|
44
|
+
- lib/controller_policies/version.rb
|
45
|
+
- lib/generators/policy_definition_generator.rb
|
46
|
+
- lib/generators/templates/definitions.rb.tt
|
47
|
+
homepage: https://github.com/tieeeeen1994/rails-controller-policies
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata:
|
51
|
+
homepage_uri: https://github.com/tieeeeen1994/rails-controller-policies
|
52
|
+
rubygems_mfa_required: 'true'
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubygems_version: 3.5.3
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Allows the developer to define policies for controllers.
|
72
|
+
test_files: []
|