access_context 1.0.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: 35f221094e6d947704c8e17e0c4644318d636fc077a0c4d62262f8b5378c076d
4
+ data.tar.gz: 867c0f9a28e8c95c3a031b248ab4531856b82c7933408faee8ec54c6b787719c
5
+ SHA512:
6
+ metadata.gz: 79f979e9c208881bb092bd43e72882fc978f9f75fc98e83d2a7e346b47c8b3f048fa67f6d6221083f21249d3291b175030a4d938d5b433f76ad01db107403e2a
7
+ data.tar.gz: 0d2e2bd9bb250b6b38da3383a5fac485dc8c93a4a644a351e1ac5579a6d0ec91a551cc6e2b00fdf9782eb6936ac744c30456f1fce8f796733872e3a5de3e84d4
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2025-03-23
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 hopper.gee@hey.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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Hopper Gee
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,149 @@
1
+ # AccessContext
2
+
3
+ A flexible and efficient context based permission management library for Ruby applications.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ $ bundle add access_context
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ 1. Setup roles and permission with context
14
+
15
+ ```ruby
16
+ AccessContext.config do
17
+
18
+ roles do
19
+ grant :admin, ['default', 'order_read', 'admin_default']
20
+ grant :support, 'default'
21
+ grant :support, 'order_read'
22
+ end
23
+
24
+ permissions do
25
+ grant 'default' do
26
+ controller :home, [:show]
27
+ controller :dashboard, [:show]
28
+ service :visit, :call
29
+ job :welcome, :perform
30
+ end
31
+ grant 'admin_default' do
32
+ controller 'admin/dashboard', [:show]
33
+ context :controller, "admin/orders", :show
34
+ end
35
+ grant 'order_read' do
36
+ controller :orders, [:show]
37
+ view 'admin/orders', ["order_edit_button"]
38
+ end
39
+ end
40
+
41
+ end
42
+ ```
43
+
44
+ 2. Check access by role on each context
45
+
46
+ ```ruby
47
+ AccessContext.controller(:home, :show).permit_role?(:admin) #=> true
48
+ AccessContext.service(:visit, :call).permit_role?(:support) #=> true
49
+ AccessContext.job(:welcome, :perform).permit_role?(:admin) #=> true
50
+ AccessContext.view("admin/orders", :order_edit_button).permit_role?(:support) #=> false
51
+ ```
52
+
53
+ Or we can use `permit?` if we have `role` column/attribute on `User`.
54
+
55
+ ```ruby
56
+ admin = User.new(role: :admin)
57
+ AccessContext.controller(:home, :show).permit?(admin) #=> true
58
+ AccessContext.job(:welcome, :perform).permit?(admin) #=> true
59
+
60
+ support = User.new(role: :support)
61
+ AccessContext.service(:visit, :call).permit?(support) #=> true
62
+ AccessContext.view("admin/orders", :order_edit_button).permit?(support) #=> false
63
+ ```
64
+
65
+ 3. Integrate it with Rails
66
+
67
+ ```ruby
68
+ # Controller
69
+ class ApplicationController < ActionController::Base
70
+ before_action :verify_permission
71
+
72
+ private
73
+
74
+ def verify_permission
75
+ return unless current_user
76
+
77
+ controller_name = self.class.name.underscore.sub(/_controller$/, '')
78
+ context = AccessContext.new(controller_name, action_name)
79
+ head :forbidden unless context.permit?(current_user)
80
+ end
81
+ end
82
+
83
+ # View: app/views/home/index.html.erb
84
+ <%= context = AccessContext.view(:home, :login_button) %>
85
+
86
+ <%= if context.permit?(current_user) %>
87
+ <button>Login</button>
88
+ <% end %>
89
+
90
+ # Job: app/jobs/notify_job.rb
91
+ class NotifyJob < ApplicationJob
92
+
93
+ def perform(user_id)
94
+ user = User.find(user_id)
95
+ context = AccessContext.job(:notify_job, :perform)
96
+ if context.permit?(user)
97
+ # ...
98
+ end
99
+ end
100
+
101
+ end
102
+
103
+ # Service app/services/order_service.rb
104
+ class OrderService
105
+ def initialize(user)
106
+ @user = user
107
+ end
108
+
109
+ def test_a
110
+ if AccessContext.service(:order, :test_a).permit?(@user)
111
+ # ...
112
+ end
113
+ end
114
+
115
+ def test_b
116
+ if AccessContext.service(:order, :test_b).permit?(@user)
117
+ # ...
118
+ end
119
+ end
120
+ end
121
+ ```
122
+
123
+ ## Development
124
+
125
+ ```ruby
126
+ bundle install
127
+ meval bundle install
128
+ meval rake
129
+
130
+ # Test with a specific Ruby version
131
+ meval -r 3.1 bundle install
132
+ meval -r 3.1 rake
133
+
134
+ # Or test for all support Ruby version
135
+ meavl -a bundle install
136
+ meavl -a rake
137
+ ```
138
+
139
+ ## Contributing
140
+
141
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hoppergee/access_context. 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/hoppergee/access_context/blob/main/CODE_OF_CONDUCT.md).
142
+
143
+ ## License
144
+
145
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
146
+
147
+ ## Code of Conduct
148
+
149
+ Everyone interacting in the AccessContext project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hoppergee/access_context/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,13 @@
1
+ class AccessContext
2
+ module Configuration
3
+ module_function
4
+
5
+ def roles(&block)
6
+ Role.class_eval(&block)
7
+ end
8
+
9
+ def permissions(&block)
10
+ Permission.class_eval(&block)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,61 @@
1
+ class AccessContext
2
+ class Permission
3
+
4
+ class << self
5
+ def grant(name, &block)
6
+ permissions[name.to_sym] = new(name, &block)
7
+ end
8
+
9
+ def [](name)
10
+ permissions[name.to_sym]
11
+ end
12
+
13
+ def all
14
+ permissions
15
+ end
16
+
17
+ def permissions
18
+ @permissions ||= {}
19
+ end
20
+ end
21
+
22
+ attr_reader :name
23
+
24
+ def initialize(name, &block)
25
+ @name = name.to_sym
26
+ instance_eval(&block)
27
+ end
28
+
29
+ def permit?(context)
30
+ contexts.any? do |c|
31
+ c == context
32
+ end
33
+ end
34
+
35
+ def context(type, name, targets=[])
36
+ ([targets] || []).flatten.each do |target|
37
+ contexts << AccessContext.new(type, name, target)
38
+ end
39
+ end
40
+
41
+ def controller(name, actions=[])
42
+ context(:controller, name, actions)
43
+ end
44
+
45
+ def service(name, actions=[])
46
+ context(:service, name, actions)
47
+ end
48
+
49
+ def view(name, elements=[])
50
+ context(:view, name, elements)
51
+ end
52
+
53
+ def job(name, actions=[])
54
+ context(:job, name, actions)
55
+ end
56
+
57
+ def contexts
58
+ @contexts ||= []
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,51 @@
1
+ class AccessContext
2
+ class Role
3
+
4
+ class << self
5
+ def grant(role_name, permission_names)
6
+ name = role_name.to_sym
7
+ role = roles[name]
8
+ if role
9
+ role.add_permissions(permission_names)
10
+ else
11
+ roles[name] = new(name, permission_names)
12
+ end
13
+ end
14
+
15
+ def [](role)
16
+ if role.is_a?(Role)
17
+ role
18
+ else
19
+ roles[role.to_sym]
20
+ end
21
+ end
22
+
23
+ def all
24
+ roles
25
+ end
26
+
27
+ def roles
28
+ @roles ||= {}
29
+ end
30
+ end
31
+
32
+ attr_reader :name, :permission_names
33
+
34
+ def initialize(name, permission_names)
35
+ @name = name
36
+ @permission_names = ([permission_names] || []).flatten.map(&:to_sym).uniq
37
+ end
38
+
39
+ def add_permissions(names)
40
+ symbolized_names = ([names] || []).flatten.map(&:to_sym)
41
+ @permission_names = (@permission_names + symbolized_names).uniq
42
+ end
43
+
44
+ def permissions
45
+ @permissions ||= permission_names.map do |permission_name|
46
+ AccessContext::Permission.all[permission_name]
47
+ end.compact
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AccessContext
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "access_context/version"
4
+
5
+ class AccessContext
6
+
7
+ class Error < StandardError; end
8
+
9
+ autoload :Configuration, 'access_context/configuration'
10
+ autoload :Permission, 'access_context/permission'
11
+ autoload :Role, 'access_context/role'
12
+
13
+ class << self
14
+
15
+ def config(&block)
16
+ Configuration.module_eval(&block)
17
+ end
18
+
19
+ def controller(name, target)
20
+ AccessContext.new(:controller, name, target)
21
+ end
22
+
23
+ def service(name, target)
24
+ AccessContext.new(:service, name, target)
25
+ end
26
+
27
+ def view(name, target)
28
+ AccessContext.new(:view, name, target)
29
+ end
30
+
31
+ def job(name, target)
32
+ AccessContext.new(:job, name, target)
33
+ end
34
+
35
+ end
36
+
37
+ attr_reader :type, :name, :target
38
+
39
+ def initialize(type, name, target)
40
+ @type = type.to_sym
41
+ @name = name.to_sym
42
+ @target = target.to_sym
43
+ end
44
+
45
+ def permit?(user)
46
+ permit_role?(user.role)
47
+ end
48
+
49
+ def permit_role?(role)
50
+ AccessContext::Role[role].permissions.any? do |permission|
51
+ permission.permit?(self)
52
+ end
53
+ end
54
+
55
+ def ==(context)
56
+ type == context.type &&
57
+ name == context.name &&
58
+ target == context.target
59
+ end
60
+
61
+ end
data/matrixeval.yml ADDED
@@ -0,0 +1,76 @@
1
+ version: 0.4
2
+ target: ruby
3
+ project_name: access_context
4
+ parallel_workers: number_of_processors
5
+ # commands:
6
+ # - ps
7
+ # - top
8
+ # - an_additional_command
9
+ # mounts:
10
+ # - /a/path/need/to/mount:/a/path/mount/to
11
+ matrix:
12
+ ruby:
13
+ variants:
14
+ - key: 3.0
15
+ container:
16
+ image: ruby:3.0.7
17
+ - key: 3.1
18
+ default: true
19
+ container:
20
+ image: ruby:3.1.6
21
+ - key: 3.2
22
+ container:
23
+ image: ruby:3.2.7
24
+ - key: 3.3
25
+ container:
26
+ image: ruby:3.3.7
27
+ - key: 3.4
28
+ container:
29
+ image: ruby:3.4.2
30
+ # - key: jruby-9.3
31
+ # container:
32
+ # image: jruby:9.3
33
+ # env:
34
+ # PATH: "/opt/jruby/bin:/app/bin:/bundle/bin:$PATH"
35
+ # mounts:
36
+ # - /a/path/need/to/mount:/a/path/mount/to
37
+
38
+ # rails:
39
+ # variants:
40
+ # - key: 6.1
41
+ # default: true
42
+ # env:
43
+ # RAILS_VERSION: "~> 6.1.0"
44
+ # - key: 7.0
45
+ # env:
46
+ # RAILS_VERSION: "~> 7.0.0"
47
+ # another:
48
+ # variants:
49
+ # - key: key1
50
+ # default: true
51
+ # env:
52
+ # ENV_KEY: 1
53
+ # - key: key2
54
+ # env:
55
+ # ENV_KEY: 2
56
+
57
+ exclude:
58
+ # - ruby: 3.0
59
+ # rails: 4.2
60
+ # - ruby: jruby-9.3
61
+ # rails: 7.0
62
+
63
+ docker-compose-extend:
64
+ # services:
65
+ # postgres:
66
+ # image: postgres:12.8
67
+ # volumes:
68
+ # - postgres12:/var/lib/postgresql/data
69
+ # environment:
70
+ # POSTGRES_HOST_AUTH_METHOD: trust
71
+
72
+ # redis:
73
+ # image: redis:6.2-alpine
74
+
75
+ # volumes:
76
+ # postgres12:
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: access_context
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hopper Gee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-03-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Setup role with lightweight context-based permission
14
+ email:
15
+ - hopper.gee@hey.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/access_context.rb
26
+ - lib/access_context/configuration.rb
27
+ - lib/access_context/permission.rb
28
+ - lib/access_context/role.rb
29
+ - lib/access_context/version.rb
30
+ - matrixeval.yml
31
+ homepage: https://github.com/hoppergee/access_context
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/hoppergee/access_context
36
+ source_code_uri: https://github.com/hoppergee/access_context
37
+ changelog_uri: https://github.com/hoppergee/access_context/blob/main/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.3.7
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A flexible and efficient permission management library for Ruby applications.
57
+ test_files: []