pundit_role_play 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 967fbb651ade454c1781fd00745665b608714f9bd1753ba305270f16118996c2
4
+ data.tar.gz: 0f79a19cd94aca3fe9f38e7702c3d019c2288405caf89907cf011bfa776060e0
5
+ SHA512:
6
+ metadata.gz: a463aaba01c4447057465d183d3a3f8845e747bf72851da91e33ba813d13327b3176d7f58cf59e0d01fdfeea4f583809cbf5e24bcd49a4f06ef0cefd0a50072f
7
+ data.tar.gz: 5ec06b0a8cfaa49500a2fbdce564463a75d52fe58150ff49f35f198d494a04acbcdc63d8ea0f6d9f7caaf075b0d4994f6e208fa0c52bf6eafc4c34a2408244f7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ # Omakase Ruby styling for Rails
2
+ AllCops:
3
+ Exclude:
4
+ - "lib/generators/pundit_role_play/role/templates/role.rb"
5
+ inherit_gem:
6
+ rubocop-rails-omakase: rubocop.yml
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-12-10
4
+
5
+ - Initial release
@@ -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 jimmypoulsen96@gmail.com. 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/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pundit_role_play.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "rubocop-rails-omakase", require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,247 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pundit_role_play (0.1.0)
5
+ rails (>= 7.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (8.0.1)
11
+ actionpack (= 8.0.1)
12
+ activesupport (= 8.0.1)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ zeitwerk (~> 2.6)
16
+ actionmailbox (8.0.1)
17
+ actionpack (= 8.0.1)
18
+ activejob (= 8.0.1)
19
+ activerecord (= 8.0.1)
20
+ activestorage (= 8.0.1)
21
+ activesupport (= 8.0.1)
22
+ mail (>= 2.8.0)
23
+ actionmailer (8.0.1)
24
+ actionpack (= 8.0.1)
25
+ actionview (= 8.0.1)
26
+ activejob (= 8.0.1)
27
+ activesupport (= 8.0.1)
28
+ mail (>= 2.8.0)
29
+ rails-dom-testing (~> 2.2)
30
+ actionpack (8.0.1)
31
+ actionview (= 8.0.1)
32
+ activesupport (= 8.0.1)
33
+ nokogiri (>= 1.8.5)
34
+ rack (>= 2.2.4)
35
+ rack-session (>= 1.0.1)
36
+ rack-test (>= 0.6.3)
37
+ rails-dom-testing (~> 2.2)
38
+ rails-html-sanitizer (~> 1.6)
39
+ useragent (~> 0.16)
40
+ actiontext (8.0.1)
41
+ actionpack (= 8.0.1)
42
+ activerecord (= 8.0.1)
43
+ activestorage (= 8.0.1)
44
+ activesupport (= 8.0.1)
45
+ globalid (>= 0.6.0)
46
+ nokogiri (>= 1.8.5)
47
+ actionview (8.0.1)
48
+ activesupport (= 8.0.1)
49
+ builder (~> 3.1)
50
+ erubi (~> 1.11)
51
+ rails-dom-testing (~> 2.2)
52
+ rails-html-sanitizer (~> 1.6)
53
+ activejob (8.0.1)
54
+ activesupport (= 8.0.1)
55
+ globalid (>= 0.3.6)
56
+ activemodel (8.0.1)
57
+ activesupport (= 8.0.1)
58
+ activerecord (8.0.1)
59
+ activemodel (= 8.0.1)
60
+ activesupport (= 8.0.1)
61
+ timeout (>= 0.4.0)
62
+ activestorage (8.0.1)
63
+ actionpack (= 8.0.1)
64
+ activejob (= 8.0.1)
65
+ activerecord (= 8.0.1)
66
+ activesupport (= 8.0.1)
67
+ marcel (~> 1.0)
68
+ activesupport (8.0.1)
69
+ base64
70
+ benchmark (>= 0.3)
71
+ bigdecimal
72
+ concurrent-ruby (~> 1.0, >= 1.3.1)
73
+ connection_pool (>= 2.2.5)
74
+ drb
75
+ i18n (>= 1.6, < 2)
76
+ logger (>= 1.4.2)
77
+ minitest (>= 5.1)
78
+ securerandom (>= 0.3)
79
+ tzinfo (~> 2.0, >= 2.0.5)
80
+ uri (>= 0.13.1)
81
+ ast (2.4.2)
82
+ base64 (0.2.0)
83
+ benchmark (0.4.0)
84
+ bigdecimal (3.1.8)
85
+ builder (3.3.0)
86
+ concurrent-ruby (1.3.4)
87
+ connection_pool (2.4.1)
88
+ crass (1.0.6)
89
+ date (3.4.1)
90
+ diff-lcs (1.5.1)
91
+ drb (2.2.1)
92
+ erubi (1.13.0)
93
+ globalid (1.2.1)
94
+ activesupport (>= 6.1)
95
+ i18n (1.14.6)
96
+ concurrent-ruby (~> 1.0)
97
+ io-console (0.8.0)
98
+ irb (1.14.2)
99
+ rdoc (>= 4.0.0)
100
+ reline (>= 0.4.2)
101
+ json (2.9.0)
102
+ language_server-protocol (3.17.0.3)
103
+ logger (1.6.3)
104
+ loofah (2.23.1)
105
+ crass (~> 1.0.2)
106
+ nokogiri (>= 1.12.0)
107
+ mail (2.8.1)
108
+ mini_mime (>= 0.1.1)
109
+ net-imap
110
+ net-pop
111
+ net-smtp
112
+ marcel (1.0.4)
113
+ mini_mime (1.1.5)
114
+ minitest (5.25.4)
115
+ net-imap (0.5.1)
116
+ date
117
+ net-protocol
118
+ net-pop (0.1.2)
119
+ net-protocol
120
+ net-protocol (0.2.2)
121
+ timeout
122
+ net-smtp (0.5.0)
123
+ net-protocol
124
+ nio4r (2.7.4)
125
+ nokogiri (1.17.2-x86_64-linux)
126
+ racc (~> 1.4)
127
+ parallel (1.26.3)
128
+ parser (3.3.6.0)
129
+ ast (~> 2.4.1)
130
+ racc
131
+ psych (5.2.1)
132
+ date
133
+ stringio
134
+ racc (1.8.1)
135
+ rack (3.1.8)
136
+ rack-session (2.0.0)
137
+ rack (>= 3.0.0)
138
+ rack-test (2.1.0)
139
+ rack (>= 1.3)
140
+ rackup (2.2.1)
141
+ rack (>= 3)
142
+ rails (8.0.1)
143
+ actioncable (= 8.0.1)
144
+ actionmailbox (= 8.0.1)
145
+ actionmailer (= 8.0.1)
146
+ actionpack (= 8.0.1)
147
+ actiontext (= 8.0.1)
148
+ actionview (= 8.0.1)
149
+ activejob (= 8.0.1)
150
+ activemodel (= 8.0.1)
151
+ activerecord (= 8.0.1)
152
+ activestorage (= 8.0.1)
153
+ activesupport (= 8.0.1)
154
+ bundler (>= 1.15.0)
155
+ railties (= 8.0.1)
156
+ rails-dom-testing (2.2.0)
157
+ activesupport (>= 5.0.0)
158
+ minitest
159
+ nokogiri (>= 1.6)
160
+ rails-html-sanitizer (1.6.2)
161
+ loofah (~> 2.21)
162
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
163
+ railties (8.0.1)
164
+ actionpack (= 8.0.1)
165
+ activesupport (= 8.0.1)
166
+ irb (~> 1.13)
167
+ rackup (>= 1.0.0)
168
+ rake (>= 12.2)
169
+ thor (~> 1.0, >= 1.2.2)
170
+ zeitwerk (~> 2.6)
171
+ rainbow (3.1.1)
172
+ rake (13.2.1)
173
+ rdoc (6.9.0)
174
+ psych (>= 4.0.0)
175
+ regexp_parser (2.9.3)
176
+ reline (0.5.12)
177
+ io-console (~> 0.5)
178
+ rspec (3.13.0)
179
+ rspec-core (~> 3.13.0)
180
+ rspec-expectations (~> 3.13.0)
181
+ rspec-mocks (~> 3.13.0)
182
+ rspec-core (3.13.2)
183
+ rspec-support (~> 3.13.0)
184
+ rspec-expectations (3.13.3)
185
+ diff-lcs (>= 1.2.0, < 2.0)
186
+ rspec-support (~> 3.13.0)
187
+ rspec-mocks (3.13.2)
188
+ diff-lcs (>= 1.2.0, < 2.0)
189
+ rspec-support (~> 3.13.0)
190
+ rspec-support (3.13.2)
191
+ rubocop (1.69.1)
192
+ json (~> 2.3)
193
+ language_server-protocol (>= 3.17.0)
194
+ parallel (~> 1.10)
195
+ parser (>= 3.3.0.2)
196
+ rainbow (>= 2.2.2, < 4.0)
197
+ regexp_parser (>= 2.9.3, < 3.0)
198
+ rubocop-ast (>= 1.36.2, < 2.0)
199
+ ruby-progressbar (~> 1.7)
200
+ unicode-display_width (>= 2.4.0, < 4.0)
201
+ rubocop-ast (1.36.2)
202
+ parser (>= 3.3.1.0)
203
+ rubocop-minitest (0.36.0)
204
+ rubocop (>= 1.61, < 2.0)
205
+ rubocop-ast (>= 1.31.1, < 2.0)
206
+ rubocop-performance (1.23.0)
207
+ rubocop (>= 1.48.1, < 2.0)
208
+ rubocop-ast (>= 1.31.1, < 2.0)
209
+ rubocop-rails (2.27.0)
210
+ activesupport (>= 4.2.0)
211
+ rack (>= 1.1)
212
+ rubocop (>= 1.52.0, < 2.0)
213
+ rubocop-ast (>= 1.31.1, < 2.0)
214
+ rubocop-rails-omakase (1.0.0)
215
+ rubocop
216
+ rubocop-minitest
217
+ rubocop-performance
218
+ rubocop-rails
219
+ ruby-progressbar (1.13.0)
220
+ securerandom (0.4.0)
221
+ stringio (3.1.2)
222
+ thor (1.3.2)
223
+ timeout (0.4.2)
224
+ tzinfo (2.0.6)
225
+ concurrent-ruby (~> 1.0)
226
+ unicode-display_width (3.1.2)
227
+ unicode-emoji (~> 4.0, >= 4.0.4)
228
+ unicode-emoji (4.0.4)
229
+ uri (1.0.2)
230
+ useragent (0.16.11)
231
+ websocket-driver (0.7.6)
232
+ websocket-extensions (>= 0.1.0)
233
+ websocket-extensions (0.1.5)
234
+ zeitwerk (2.7.1)
235
+
236
+ PLATFORMS
237
+ x86_64-linux
238
+
239
+ DEPENDENCIES
240
+ pundit_role_play!
241
+ rake (~> 13.0)
242
+ rspec (~> 3.0)
243
+ rubocop (~> 1.21)
244
+ rubocop-rails-omakase
245
+
246
+ BUNDLED WITH
247
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jimmy Poulsen
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,182 @@
1
+ # Pundit Role Play
2
+
3
+ A simple and flexible role-based authorization gem built on top of [Pundit](https://github.com/varvet/pundit) for Ruby on Rails applications. Pundit Role Play allows developers to manage user roles and permissions in an intuitive way while leveraging Pundit's policy-driven architecture.
4
+
5
+ ## Features
6
+ - Easy role management for users.
7
+ - Built-in support for Pundit policies.
8
+ - Flexible and lightweight design with minimal dependencies.
9
+ - Supports role inheritance and custom roles.
10
+
11
+ ## Installation
12
+ Add the gem to your Rails application's `Gemfile`:
13
+
14
+ ```ruby
15
+ gem "pundit_role_play", github: "Synsbasen/pundit-role-play", branch: "master"
16
+ ```
17
+
18
+ Run the bundle command to install it:
19
+
20
+ ```bash
21
+ bundle install
22
+ ```
23
+
24
+ Run the install generator to setup an application role:
25
+ ```bash
26
+ bin/rails generate pundit_role_play:install
27
+ ```
28
+
29
+ ## Preconditions
30
+ Ensure that the models you want to use roles and permissions with have a `role` attribute. This attribute should be a string and should be present in the database.
31
+
32
+ ```ruby
33
+ class AddRoleToUsers < ActiveRecord::Migration[6.0]
34
+ def change
35
+ add_column :users, :role, :string #, default: "MemberRole"
36
+ end
37
+ end
38
+ ```
39
+
40
+ Furthermore, ensure that the `has_pundit_role` method is called in the User model.
41
+
42
+ ```ruby
43
+ class User < ApplicationRecord
44
+ has_pundit_role
45
+ end
46
+ ```
47
+
48
+ ## Usage
49
+ ### Setting up Roles
50
+ Define roles for your application by utilizing the built-in role generator:
51
+
52
+ ```bash
53
+ bin/rails generate pundit_role_play:role admin
54
+ ```
55
+
56
+ This will generate the following role:
57
+
58
+ ```ruby
59
+ class AdminRole < ApplicationRole
60
+ PERMISSIONS = {
61
+ users: :read,
62
+ another_resource: :write
63
+ }.freeze
64
+ end
65
+ ```
66
+
67
+ Now, it's up to you to define the resources for the role. Example:
68
+
69
+ ```ruby
70
+ class AdminRole < ApplicationRole
71
+ PERMISSIONS = {
72
+ areas: :write,
73
+ invoices: :read,
74
+ organizations: :write,
75
+ user_areas: :write,
76
+ users: :write,
77
+ user_organizations: :write
78
+ }.freeze
79
+ end
80
+ ```
81
+
82
+ ### Using roles in Pundit policies
83
+ Pundit Role Play integrates seamlessly with Pundit policies. Use the current user's permissions within your Pundit policy files with the `can_read?` and `can_write?` helper methods:
84
+
85
+ ```ruby
86
+ class UserPolicy < ApplicationPolicy
87
+ class Scope < Scope
88
+ def resolve
89
+ scope.all
90
+ end
91
+ end
92
+
93
+ def edit?
94
+ can_write? || user == record
95
+ end
96
+
97
+ def index?
98
+ can_read?
99
+ end
100
+
101
+ def show?
102
+ can_read?
103
+ end
104
+
105
+ def new?
106
+ can_write?
107
+ end
108
+
109
+ def create?
110
+ can_write?
111
+ end
112
+
113
+ def update?
114
+ can_write? || user == record
115
+ end
116
+ end
117
+ ```
118
+
119
+ PunditRolePlay knows which resource from the role's permissions to target based on the name of the policy. In the example above it will target the `users` resource and evaluate if read/write is allowed.
120
+
121
+ The `can_edit?` and `can_write?` methods takes an optional argument `:resource` in case you're working with a policy that depends on other resources. Example:
122
+
123
+ ```ruby
124
+ class UserOrganizationPolicy < ApplicationPolicy
125
+ def edit?
126
+ can_write?(resource: :users)
127
+ end
128
+ end
129
+ ```
130
+
131
+ ### Checking roles in controllers
132
+ You can use the role helper methods in your controllers:
133
+
134
+ ```ruby
135
+ class PostsController < ApplicationController
136
+ def update
137
+ if current_user.has_role?(:editor)
138
+ # Proceed with update logic
139
+ else
140
+ redirect_to root_path, alert: 'Access Denied!'
141
+ end
142
+ end
143
+ end
144
+ ```
145
+
146
+ ### Role helper in views
147
+ You can use the role helper methods in your views:
148
+
149
+ ```erb
150
+ <% if current_user.has_role?(:admin) %>
151
+ <%= link_to 'Delete Post', post_path(@post), method: :delete %>
152
+ <% end %>
153
+ ```
154
+
155
+ ## Role inheritance
156
+ Pundit Role Play supports role inheritance out of the box. For example:
157
+
158
+ - **Admin** inherits permissions from **Manager** and **Editor**.
159
+ - **Manager** inherits permissions from **Editor**.
160
+
161
+ This means an admin user will automatically have all the permissions of a manager and editor.
162
+
163
+ ## Contributing
164
+ We welcome contributions! Please fork the repository, make changes, and submit a pull request.
165
+
166
+ ### Steps to contribute
167
+ 1. Fork the repository.
168
+ 2. Create a new branch for your feature: `git checkout -b my-new-feature`.
169
+ 3. Commit your changes: `git commit -am 'Add new feature'`.
170
+ 4. Push the branch: `git push origin my-new-feature`.
171
+ 5. Submit a pull request.
172
+
173
+ ## License
174
+ This gem is released under the [MIT License](LICENSE).
175
+
176
+ ## Support
177
+ If you encounter any issues, please open an issue in the GitHub repository.
178
+
179
+ ---
180
+
181
+ **Pundit Role Play** simplifies role-based authorization for Pundit users. Get started today and make authorization in your Rails app clean, flexible, and powerful!
182
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module PunditRolePlay
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ def create_roles_folder
11
+ empty_directory "app/roles"
12
+ end
13
+
14
+ def create_application_role
15
+ template "application_role.rb", "app/roles/application_role.rb"
16
+ end
17
+
18
+ def display_readme
19
+ readme "README.md" if behavior == :invoke
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ PunditRolePlay was successfully installed! Generate a new role by running `rails g pundit_role_play:role <role_name>`.
@@ -0,0 +1 @@
1
+ class ApplicationRole < PunditRolePlay::Role; end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module PunditRolePlay
6
+ module Generators
7
+ class RoleGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("templates", __dir__)
9
+ argument :role_name, type: :string, required: true, banner: "Role name"
10
+
11
+ def create_role_file
12
+ # Generate the role file in app/roles
13
+ template "role.rb", File.join("app/roles", "#{file_name}.rb")
14
+ end
15
+
16
+ private
17
+
18
+ # Ensure the class name ends with "Role"
19
+ def class_name
20
+ role_name.camelize.end_with?("Role") ? role_name.camelize : "#{role_name.camelize}Role"
21
+ end
22
+
23
+ # Convert the class name to a file name
24
+ def file_name
25
+ class_name.underscore
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ class <%= class_name %> < ApplicationRole
2
+ PERMISSIONS = {
3
+ users: :read,
4
+ another_resource: :write
5
+ }.freeze
6
+ end
7
+
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+
5
+ module PunditRolePlay
6
+ module Adapter
7
+ module ActiveRecord
8
+ def has_pundit_role
9
+ include PunditRolePlay::Adapter::ActiveRecordMethods
10
+ end
11
+ end
12
+
13
+ module ActiveRecordMethods
14
+ def role
15
+ value = super
16
+
17
+ return nil unless value.present?
18
+
19
+ value.constantize
20
+
21
+ value
22
+ rescue NameError
23
+ # Handle the case where the string can't be converted to a constant
24
+ # eg. "testrole" (which should be "TestRole")
25
+ #
26
+ # Handle the case where the constant does not exist
27
+ raise RoleNotFoundError
28
+ end
29
+
30
+ def has_role?(role)
31
+ role.to_sym == self.role.underscore.sub(/_role$/, '').to_sym
32
+ end
33
+
34
+ def pretty_role_name
35
+ role.gsub(/Role/, "")
36
+ end
37
+
38
+ def permissions
39
+ role.constantize.permissions
40
+ rescue NoMethodError
41
+ Permissions.new(nil)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PunditRolePlay
4
+ module Adapter
5
+ module Pundit
6
+ def can_read?(resource: self.class.to_s.gsub("Policy", "").pluralize.underscore)
7
+ user.permissions.can?(:read, resource.to_sym)
8
+ end
9
+
10
+ def can_write?(resource: self.class.to_s.gsub("Policy", "").pluralize.underscore)
11
+ user.permissions.can?(:write, resource.to_sym)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PunditRolePlay
4
+ class Permissions
5
+ attr_reader :permissions
6
+
7
+ def initialize(role)
8
+ @permissions = role::PERMISSIONS
9
+ rescue TypeError => e
10
+ # Handle the case where the role is nil
11
+ raise e unless role.nil?
12
+
13
+ @permissions = {}
14
+ end
15
+
16
+ def can?(operation, resource)
17
+ # If you can write to a resource, you can also read from it
18
+ return true if permissions[resource.to_sym] == :write
19
+
20
+ permissions[resource.to_sym] == operation.to_sym
21
+ end
22
+
23
+ def [](resource)
24
+ permissions[resource.to_sym]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module PunditRolePlay
6
+ class Railtie < Rails::Railtie
7
+ config.before_configuration do
8
+ # Add app/roles to eager_load_paths early
9
+ Rails.application.config.eager_load_paths << Rails.root.join("app", "roles")
10
+ end
11
+
12
+ initializer "pundit_role_play.eager_load_roles" do
13
+ Rails.application.config.to_prepare do
14
+ # Force Zeitwerk to load all role classes
15
+ Dir[Rails.root.join("app/roles/**/*.rb")].each { |file| require_dependency file }
16
+ end
17
+ end
18
+
19
+ initializer "pundit_role_play.extend_active_record" do
20
+ ActiveSupport.on_load(:active_record) do
21
+ extend PunditRolePlay::Adapter::ActiveRecord
22
+ end
23
+ end
24
+
25
+ initializer "pundit_role_play.extend_application_policy" do
26
+ Rails.application.config.to_prepare do
27
+ if defined?(ApplicationPolicy)
28
+ ApplicationPolicy.include(PunditRolePlay::Adapter::Pundit)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "permissions"
4
+
5
+ module PunditRolePlay
6
+ class Role
7
+ def self.inherited(subclass)
8
+ super
9
+
10
+ PunditRolePlay.register_role(subclass.name)
11
+ end
12
+
13
+ def self.permissions
14
+ Permissions.new(self)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PunditRolePlay
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "pundit_role_play/adapter/active_record"
4
+ require_relative "pundit_role_play/adapter/pundit"
5
+ require_relative "pundit_role_play/railtie"
6
+ require_relative "pundit_role_play/role"
7
+ require_relative "pundit_role_play/version"
8
+
9
+ module PunditRolePlay
10
+ class Error < StandardError; end
11
+ class RoleNotFoundError < Error
12
+ def message
13
+ "Role does not exist"
14
+ end
15
+ end
16
+
17
+ def self.roles
18
+ @roles ||= []
19
+ end
20
+
21
+ def self.register_role(name)
22
+ roles << name unless name == "ApplicationRole"
23
+ roles.uniq!
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pundit_role_play
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Poulsen
8
+ - Tobias Knudsen
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2025-01-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 7.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 7.0.0
28
+ description: |-
29
+ This gem is a simple role based authorization gem for Pundit.
30
+ It allows you to define roles and permissions in a simple way and use them in your Pundit policies.
31
+ email:
32
+ - jimmypoulsen96@gmail.com
33
+ - tobias.knudsen@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ".rspec"
39
+ - ".rubocop.yml"
40
+ - CHANGELOG.md
41
+ - CODE_OF_CONDUCT.md
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - lib/generators/pundit_role_play/install/install_generator.rb
48
+ - lib/generators/pundit_role_play/install/templates/README.md
49
+ - lib/generators/pundit_role_play/install/templates/application_role.rb
50
+ - lib/generators/pundit_role_play/role/role_generator.rb
51
+ - lib/generators/pundit_role_play/role/templates/role.rb
52
+ - lib/pundit_role_play.rb
53
+ - lib/pundit_role_play/adapter/active_record.rb
54
+ - lib/pundit_role_play/adapter/pundit.rb
55
+ - lib/pundit_role_play/permissions.rb
56
+ - lib/pundit_role_play/railtie.rb
57
+ - lib/pundit_role_play/role.rb
58
+ - lib/pundit_role_play/version.rb
59
+ homepage: https://github.com/synsbasen/pundit-role-play
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/synsbasen/pundit-role-play
64
+ source_code_uri: https://github.com/synsbasen/pundit-role-play
65
+ changelog_uri: https://github.com/synsbasen/pundit-role-play
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.6.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.4.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A simple role based authorization gem for Pundit
85
+ test_files: []