u-authorization 2.3.0 → 3.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 +4 -4
- data/.github/workflows/ci.yml +41 -0
- data/.gitignore +7 -0
- data/CHANGELOG.md +74 -0
- data/CLAUDE.md +124 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -4
- data/LICENSE.txt +21 -0
- data/README.md +480 -93
- data/bin/console +14 -0
- data/bin/setup +10 -0
- data/lib/micro/authorization/model.rb +1 -1
- data/lib/micro/authorization/version.rb +1 -1
- data/u-authorization.gemspec +9 -3
- metadata +33 -13
- data/.travis.yml +0 -28
- data/Gemfile.lock +0 -38
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 51c049fdf0f6232070f3f1bb66812b92401d5fea6ff171990d07eea0e7f81693
|
|
4
|
+
data.tar.gz: 34456d500170bafe3d018687e3bbd3c46b3d373918829150549d2992297b88f8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d62f986439e59cff7acedd82d2d0f608e0a28bbd9870f0d95347b987a13b217f2e71df86c7f32d3e47147273296bf50abe54843e93e6ad9cc69e9a535129730d
|
|
7
|
+
data.tar.gz: d3dfde01673ddf71ff0ec631d35b42275ccfd6f85e74755fa3e6b1ae8303b6ff1ffefe9fb6bf30c51ab11440173529385b57f85dd66d27c85011867aa492f912
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: Ruby
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
- master
|
|
8
|
+
|
|
9
|
+
pull_request:
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
test:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
name: Ruby ${{ matrix.ruby }}
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
strategy:
|
|
18
|
+
fail-fast: false
|
|
19
|
+
matrix:
|
|
20
|
+
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", "3.4", "4.0", head]
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v4
|
|
23
|
+
with:
|
|
24
|
+
persist-credentials: false
|
|
25
|
+
- name: Set up Ruby
|
|
26
|
+
uses: ruby/setup-ruby@v1
|
|
27
|
+
with:
|
|
28
|
+
ruby-version: ${{ matrix.ruby }}
|
|
29
|
+
- name: Install bundler
|
|
30
|
+
run: gem install bundler -v 2.4.22
|
|
31
|
+
if: ${{ matrix.ruby == '2.7' || matrix.ruby == '3.0' }}
|
|
32
|
+
- name: Bundle install
|
|
33
|
+
run: bundle install
|
|
34
|
+
- name: Run tests
|
|
35
|
+
run: bundle exec rake test
|
|
36
|
+
- name: Upload coverage to Qlty
|
|
37
|
+
uses: qltysh/qlty-action/coverage@v2
|
|
38
|
+
if: ${{ matrix.ruby == '3.4' && !github.base_ref }}
|
|
39
|
+
with:
|
|
40
|
+
token: ${{ secrets.QLTY_COVERAGE_TOKEN }}
|
|
41
|
+
files: coverage/.resultset.json
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [3.0.0] - 2026-06-01
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- This `CHANGELOG.md`, following the [Keep a Changelog 1.1.0](https://keepachangelog.com/en/1.1.0/) format and backfilled to cover every tagged release.
|
|
15
|
+
- GitHub Actions CI that runs the test suite across Ruby 2.7 through the current development build, with coverage uploaded to Qlty.
|
|
16
|
+
- A rewritten, comprehensive `README.md`, an API stability notice, and a `CLAUDE.md` with notes for contributors.
|
|
17
|
+
- `bin/setup` and `bin/console` scripts.
|
|
18
|
+
- `homepage_uri`, `source_code_uri`, and `bug_tracker_uri` entries in the gem metadata.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- Raised the minimum Ruby version to `>= 2.7.0` (was `>= 2.2.0`). Ruby 2.2 through 2.6 are end of life.
|
|
23
|
+
- Bumped the `rake` development dependency to `~> 13.0` (was `~> 10.0`).
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- Ruby 3.0+ compatibility in `Micro::Authorization::Model#add_policies`. It no longer relies on `Method#to_proc` auto-splatting the `[key, value]` pair, which raised an `ArgumentError` on Ruby 3.0 and later.
|
|
28
|
+
|
|
29
|
+
### Removed
|
|
30
|
+
|
|
31
|
+
- Travis CI configuration (`.travis.yml`), replaced by GitHub Actions.
|
|
32
|
+
- `Gemfile.lock` is no longer tracked; it is regenerated per environment.
|
|
33
|
+
|
|
34
|
+
## [2.3.0] - 2019-08-04
|
|
35
|
+
|
|
36
|
+
### Changed
|
|
37
|
+
|
|
38
|
+
- A policy's context must now be a Hash. `current_user` (and its `user` alias) reads `context[:user]` and falls back to `context[:current_user]`; passing a non-Hash context to use directly as the user is no longer supported.
|
|
39
|
+
- Clearer `ArgumentError` message from `Micro::Authorization::Model#add_policies` when it is given something other than a Hash.
|
|
40
|
+
|
|
41
|
+
### Removed
|
|
42
|
+
|
|
43
|
+
- The deprecation warning on the permission checker's `required_features`. It is now a plain alias of `#features`.
|
|
44
|
+
|
|
45
|
+
## [2.2.0] - 2019-07-30
|
|
46
|
+
|
|
47
|
+
### Added
|
|
48
|
+
|
|
49
|
+
- Multi-role permissions. `Micro::Authorization::Permissions.new` and `Micro::Authorization::Model.build` accept an array of roles and grant the union of their permissions, so a feature is allowed when any role allows it.
|
|
50
|
+
|
|
51
|
+
### Deprecated
|
|
52
|
+
|
|
53
|
+
- The permission checker's `required_features`, in favor of `required_context`. This was reverted in 2.3.0, where `#features` became the method name and `required_features` a plain alias.
|
|
54
|
+
|
|
55
|
+
## [2.1.0] - 2019-07-29
|
|
56
|
+
|
|
57
|
+
### Added
|
|
58
|
+
|
|
59
|
+
- `:to_permit` as the context key for permission checks in `Micro::Authorization::Model.build`, with `:permissions` kept as an alias.
|
|
60
|
+
- README badges and Travis CI configuration.
|
|
61
|
+
|
|
62
|
+
## [2.0.0] - 2019-07-26
|
|
63
|
+
|
|
64
|
+
### Added
|
|
65
|
+
|
|
66
|
+
- First tagged release of the `Micro::Authorization` architecture: the `Micro::Authorization::Model.build` entry point, the data-driven `Permissions` layer (roles as hashes with `any` / `only` / `except` rules and context matching, including dot-notation segments), and the `Micro::Authorization::Policy` base class for record-level checks that denies undefined predicates by default.
|
|
67
|
+
- Each class organized into its own file under `lib/micro/authorization/`, with the test suite running on Minitest.
|
|
68
|
+
|
|
69
|
+
[Unreleased]: https://github.com/u-gems/u-authorization/compare/v3.0.0...HEAD
|
|
70
|
+
[3.0.0]: https://github.com/u-gems/u-authorization/compare/v2.3.0...v3.0.0
|
|
71
|
+
[2.3.0]: https://github.com/u-gems/u-authorization/compare/v2.2.0...v2.3.0
|
|
72
|
+
[2.2.0]: https://github.com/u-gems/u-authorization/compare/v2.1.0...v2.2.0
|
|
73
|
+
[2.1.0]: https://github.com/u-gems/u-authorization/compare/v2.0.0...v2.1.0
|
|
74
|
+
[2.0.0]: https://github.com/u-gems/u-authorization/releases/tag/v2.0.0
|
data/CLAUDE.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
Notes for AI assistants working in `u-authorization`.
|
|
4
|
+
|
|
5
|
+
## Golden rule: feature-complete, keep it running
|
|
6
|
+
|
|
7
|
+
`u-authorization` is feature-complete. There are no plans to add new features. The only ongoing work is keeping the gem compatible and running on current and future Ruby versions, plus the usual bug fixes, docs, and CI upkeep.
|
|
8
|
+
|
|
9
|
+
That means two things for any task here:
|
|
10
|
+
|
|
11
|
+
- **Don't add features or change the public API.** The API is frozen and backward compatible. If a task as stated would require a new feature or a breaking change, stop and surface that, then propose a compatibility-only path.
|
|
12
|
+
- **Major version bumps are for dependency-floor changes only** (dropping an old Ruby from the supported matrix) per SemVer. They do not signal a behavior break. Note that this gem is pure Ruby with no ActiveModel dependency, so the support matrix is Ruby-only.
|
|
13
|
+
|
|
14
|
+
## How to work in this repo
|
|
15
|
+
|
|
16
|
+
### 1. Think before coding
|
|
17
|
+
|
|
18
|
+
**Don't assume. Don't hide confusion. Surface tradeoffs.**
|
|
19
|
+
|
|
20
|
+
- State assumptions explicitly. If uncertain, ask.
|
|
21
|
+
- If multiple interpretations exist, present them — don't pick silently.
|
|
22
|
+
- If a simpler approach exists, say so. Push back when warranted.
|
|
23
|
+
- If something is unclear, stop. Name what's confusing. Ask.
|
|
24
|
+
|
|
25
|
+
### 2. Simplicity first
|
|
26
|
+
|
|
27
|
+
**Minimum code that solves the problem. Nothing speculative.**
|
|
28
|
+
|
|
29
|
+
- No features beyond what was asked.
|
|
30
|
+
- No abstractions for single-use code.
|
|
31
|
+
- No "flexibility" or "configurability" that wasn't requested.
|
|
32
|
+
- No error handling for impossible scenarios.
|
|
33
|
+
- If you write 200 lines and it could be 50, rewrite it.
|
|
34
|
+
|
|
35
|
+
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes,
|
|
36
|
+
simplify.
|
|
37
|
+
|
|
38
|
+
### 3. Surgical changes
|
|
39
|
+
|
|
40
|
+
**Touch only what you must. Clean up only your own mess.**
|
|
41
|
+
|
|
42
|
+
- Don't "improve" adjacent code, comments, or formatting.
|
|
43
|
+
- Don't refactor things that aren't broken.
|
|
44
|
+
- Match existing style, even if you'd do it differently.
|
|
45
|
+
- If you notice unrelated dead code, mention it — don't delete it.
|
|
46
|
+
- Remove imports/variables/functions that _your_ changes orphaned. Don't
|
|
47
|
+
remove pre-existing dead code unless asked.
|
|
48
|
+
|
|
49
|
+
The test: every changed line should trace directly to the user's request.
|
|
50
|
+
|
|
51
|
+
### 4. Goal-driven execution
|
|
52
|
+
|
|
53
|
+
**Define success criteria. Loop until verified.**
|
|
54
|
+
|
|
55
|
+
Turn vague tasks into verifiable goals:
|
|
56
|
+
|
|
57
|
+
- "Add validation" → "Write tests for invalid inputs, then make them pass"
|
|
58
|
+
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
|
|
59
|
+
- "Refactor X" → "Ensure tests pass before and after"
|
|
60
|
+
|
|
61
|
+
For multi-step work, state a brief plan with a verification check per step.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## What this is
|
|
66
|
+
|
|
67
|
+
`u-authorization` is a small, zero-runtime-dependency Ruby library for
|
|
68
|
+
authorization and role management, living under `lib/micro/authorization/`.
|
|
69
|
+
Its public namespace is `Micro::Authorization`, built from three cohesive
|
|
70
|
+
pieces:
|
|
71
|
+
|
|
72
|
+
- **`Permissions`** (`permissions.rb` + `permissions/`) — role-based,
|
|
73
|
+
per-feature permission checks (`to?`, `to_not?`, context matching via
|
|
74
|
+
`Checker` / `ForEachFeature`).
|
|
75
|
+
- **`Policy`** (`policy.rb`) — per-subject authorization policies, instantiated
|
|
76
|
+
with a context and the caller's permissions.
|
|
77
|
+
- **`Model`** (`model.rb`) — the entry point that ties permissions, policies,
|
|
78
|
+
and a context together (`Model.build`, `#to` / `#policy`, `#map`).
|
|
79
|
+
|
|
80
|
+
`require 'u-authorization'` (or `require 'micro/authorization'`) loads the lot.
|
|
81
|
+
It is a pure-Ruby gem with **no ActiveModel/Rails dependency** — it's designed
|
|
82
|
+
to drop into Rails controllers (`[controller_name, action_name]` style
|
|
83
|
+
contexts) but doesn't require Rails. Because it's a published gem, behavior
|
|
84
|
+
changes — especially anything affecting the public API or the supported `ruby`
|
|
85
|
+
matrix — are highly visible.
|
|
86
|
+
|
|
87
|
+
## Running tests
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
bundle exec rake test # full suite (also the default `rake` task)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The suite is plain Minitest with SimpleCov coverage; there are no Appraisals
|
|
94
|
+
or ActiveModel axes (the gem has no Rails dependency). `bin/setup` reinstalls
|
|
95
|
+
the bundle; `bin/console` opens an IRB session with the gem loaded.
|
|
96
|
+
|
|
97
|
+
To test across Ruby versions locally, use mise — `.tool-versions` lists the
|
|
98
|
+
supported versions. CI (`.github/workflows/ci.yml`) runs the suite across the
|
|
99
|
+
full `ruby` matrix (2.7 → head). Tests are the success criterion for any
|
|
100
|
+
behavior change — write or update a test first, then make it pass (rule 4).
|
|
101
|
+
|
|
102
|
+
## CHANGELOG and README are part of every change
|
|
103
|
+
|
|
104
|
+
Both files are user-facing; keep them in sync with the code.
|
|
105
|
+
|
|
106
|
+
- `CHANGELOG.md` follows [Keep a Changelog 1.1.0](https://keepachangelog.com/en/1.1.0/). Record every user-visible change (behavior change, bug fix, dependency-floor bump, security fix) under the appropriate section of `[Unreleased]`. Pure internal-refactor or CI-only changes generally don't need an entry.
|
|
107
|
+
- `README.md` badges and the **Required Ruby version** section near the top reference the supported Ruby bounds; update them when those bounds move. If you change a documented API, update the relevant **Usage** section in the same commit.
|
|
108
|
+
|
|
109
|
+
## Bumping the version
|
|
110
|
+
|
|
111
|
+
1. Edit `lib/micro/authorization/version.rb` — change
|
|
112
|
+
`Micro::Authorization::VERSION`. Follow [SemVer](https://semver.org/):
|
|
113
|
+
patch for fixes, minor for additive user-visible changes, major for
|
|
114
|
+
breaking changes. For this gem a major bump means an old Ruby was dropped
|
|
115
|
+
from the supported matrix, not a behavior break.
|
|
116
|
+
2. Add a new top entry in `CHANGELOG.md` (`## [X.Y.Z] - YYYY-MM-DD`), move the
|
|
117
|
+
relevant `[Unreleased]` notes under it, and add a matching compare link at
|
|
118
|
+
the bottom (`[X.Y.Z]: …/compare/vPREV...vX.Y.Z`).
|
|
119
|
+
3. If the supported Ruby matrix moved, update the Ruby badge and the
|
|
120
|
+
**Required Ruby version** section in `README.md`, and double-check the
|
|
121
|
+
`required_ruby_version` in `u-authorization.gemspec` and the CI matrix in
|
|
122
|
+
`.github/workflows/ci.yml` agree.
|
|
123
|
+
|
|
124
|
+
Don't tag, push, or `gem release` — humans do that.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at rodrigo@ysimplicity.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
source 'https://rubygems.org'
|
|
2
2
|
|
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
|
4
|
+
|
|
5
|
+
# Specify your gem's dependencies in u-authorization.gemspec
|
|
6
|
+
gemspec
|
|
7
|
+
|
|
8
|
+
gem 'rake', '~> 13.0'
|
|
9
|
+
|
|
3
10
|
group :test do
|
|
4
11
|
gem 'minitest', '~> 5.11', '>= 5.11.3'
|
|
5
12
|
gem 'minitest-reporters', '~> 1.3', '>= 1.3.6'
|
|
6
|
-
gem '
|
|
13
|
+
gem 'ostruct', '~> 0.6.3' if RUBY_VERSION >= '3.5'
|
|
14
|
+
gem 'simplecov', '~> 0.22.0', require: false
|
|
7
15
|
end
|
|
8
|
-
|
|
9
|
-
# Specify your gem's dependencies in u-authorization.gemspec
|
|
10
|
-
gemspec
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Rodrigo Serradura
|
|
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
CHANGED
|
@@ -1,136 +1,523 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<h1 align="center">🔐 µ-authorization</h1>
|
|
3
|
+
<p align="center"><i>Authorization and role management for Ruby, with no runtime dependencies.</i></p>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
<a href="https://rubygems.org/gems/u-authorization">
|
|
8
|
+
<img alt="Gem" src="https://img.shields.io/gem/v/u-authorization.svg?style=flat-square">
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/u-gems/u-authorization/actions/workflows/ci.yml">
|
|
11
|
+
<img alt="Build Status" src="https://github.com/u-gems/u-authorization/actions/workflows/ci.yml/badge.svg">
|
|
12
|
+
</a>
|
|
13
|
+
<br/>
|
|
14
|
+
<a href="https://qlty.sh/gh/u-gems/projects/u-authorization"><img src="https://qlty.sh/gh/u-gems/projects/u-authorization/maintainability.svg" alt="Maintainability" /></a>
|
|
15
|
+
<a href="https://qlty.sh/gh/u-gems/projects/u-authorization"><img src="https://qlty.sh/gh/u-gems/projects/u-authorization/coverage.svg" alt="Code Coverage" /></a>
|
|
16
|
+
<br/>
|
|
17
|
+
<img src="https://img.shields.io/badge/Ruby%20%3E%3D%202.7%2C%20%3C%3D%20Head-ruby.svg?colorA=444&colorB=333" alt="Ruby">
|
|
18
|
+
<img src="https://img.shields.io/badge/Rails%20%3E%3D%206.0%2C%20%3C%3D%20Edge-rails.svg?colorA=444&colorB=333" alt="Rails">
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
> [!IMPORTANT]
|
|
22
|
+
> **Stable and feature-complete.** `u-authorization` has no new features planned. Its public API is frozen and backward compatible, and ongoing work is limited to keeping it running on current and future Ruby versions. You can depend on it without expecting breaking changes.
|
|
23
|
+
>
|
|
24
|
+
> A major version bump signals only that an old Ruby version was dropped from the supported matrix, which is a dependency-floor change under SemVer. Your code keeps working.
|
|
25
|
+
|
|
26
|
+
`u-authorization` splits authorization into two layers that you can use together or on their own:
|
|
27
|
+
|
|
28
|
+
1. **Permissions** answer "is this role allowed to use this feature, in this context?". A role is plain data (a Hash, or JSON loaded from a database), so you can change who can do what without redeploying.
|
|
29
|
+
2. **Policies** answer "is this user allowed to act on this specific record?". A policy is a small Ruby class, similar to [Pundit](https://github.com/varvet/pundit), that returns `true` or `false` and defaults to denying anything it doesn't recognize.
|
|
30
|
+
|
|
31
|
+
The two layers are independent, and you pick the right one for each check. Use permissions to gate a feature by role and context, which fits a controller `before_action`. Use a policy to decide access to a single record. An authorization object carries the current user, the request context, the role's permissions, and the policies for one request, so both checks are available from the same place.
|
|
32
|
+
|
|
33
|
+
## Table of contents
|
|
34
|
+
|
|
35
|
+
- [Table of contents](#table-of-contents)
|
|
36
|
+
- [Installation](#installation)
|
|
37
|
+
- [Supported versions](#supported-versions)
|
|
38
|
+
- [Quick start](#quick-start)
|
|
39
|
+
- [Permissions](#permissions)
|
|
40
|
+
- [Roles are plain data](#roles-are-plain-data)
|
|
41
|
+
- [Permission rules](#permission-rules)
|
|
42
|
+
- [Context matching and dot notation](#context-matching-and-dot-notation)
|
|
43
|
+
- [Checking permissions](#checking-permissions)
|
|
44
|
+
- [Multiple roles](#multiple-roles)
|
|
45
|
+
- [Policies](#policies)
|
|
46
|
+
- [Defining a policy](#defining-a-policy)
|
|
47
|
+
- [The subject](#the-subject)
|
|
48
|
+
- [Reading permissions inside a policy](#reading-permissions-inside-a-policy)
|
|
49
|
+
- [The authorization object](#the-authorization-object)
|
|
50
|
+
- [Building it](#building-it)
|
|
51
|
+
- [Asking about permissions](#asking-about-permissions)
|
|
52
|
+
- [Fetching policies](#fetching-policies)
|
|
53
|
+
- [Registering policies](#registering-policies)
|
|
54
|
+
- [Cloning with map](#cloning-with-map)
|
|
55
|
+
- [Using it with Rails](#using-it-with-rails)
|
|
56
|
+
- [Comparison with Pundit and CanCanCan](#comparison-with-pundit-and-cancancan)
|
|
57
|
+
- [Development](#development)
|
|
58
|
+
- [Contributing](#contributing)
|
|
59
|
+
- [License](#license)
|
|
60
|
+
- [Code of conduct](#code-of-conduct)
|
|
5
61
|
|
|
6
|
-
|
|
62
|
+
## Installation
|
|
7
63
|
|
|
8
|
-
|
|
64
|
+
Add the gem to your `Gemfile`:
|
|
9
65
|
|
|
10
|
-
|
|
66
|
+
```ruby
|
|
67
|
+
gem 'u-authorization'
|
|
68
|
+
```
|
|
11
69
|
|
|
12
|
-
|
|
70
|
+
Then run `bundle install`. Or install it directly:
|
|
13
71
|
|
|
14
|
-
|
|
72
|
+
```bash
|
|
73
|
+
gem install u-authorization
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Require it (or let Bundler do it for you):
|
|
15
77
|
|
|
16
|
-
|
|
78
|
+
```ruby
|
|
79
|
+
require 'u-authorization'
|
|
17
80
|
```
|
|
18
|
-
|
|
81
|
+
|
|
82
|
+
## Supported versions
|
|
83
|
+
|
|
84
|
+
The gem requires Ruby `>= 2.7` and is tested on CI against Ruby 2.7 through the current development build. It has no runtime dependencies and works inside any Rails `>= 6.0` application, but it does not depend on Rails or ActiveModel, so you can use it in plain Ruby, Hanami, Sinatra, or a script.
|
|
85
|
+
|
|
86
|
+
## Quick start
|
|
87
|
+
|
|
88
|
+
Here is a full example using `OpenStruct` to stand in for a user and a database record. It defines a role, a policy, builds an authorization object, and asks it questions.
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
require 'ostruct'
|
|
92
|
+
require 'u-authorization'
|
|
93
|
+
|
|
94
|
+
# 1. Roles are data. Map each feature to a rule.
|
|
95
|
+
module Permissions
|
|
96
|
+
ADMIN = {
|
|
97
|
+
'visit' => { 'any' => true },
|
|
98
|
+
'export' => { 'any' => true }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
USER = {
|
|
102
|
+
'visit' => { 'except' => ['billings'] },
|
|
103
|
+
'export' => { 'except' => ['sales'] }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
ALL = { 'admin' => ADMIN, 'user' => USER }
|
|
107
|
+
|
|
108
|
+
def self.to(role)
|
|
109
|
+
ALL.fetch(role, USER)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# 2. Policies are classes. Predicate methods return true or false.
|
|
114
|
+
class SalesPolicy < Micro::Authorization::Policy
|
|
115
|
+
def edit?(record)
|
|
116
|
+
user.id == record.user_id
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
user = OpenStruct.new(id: 1, role: 'user')
|
|
121
|
+
|
|
122
|
+
# 3. Build the authorization object for this request.
|
|
123
|
+
authorization = Micro::Authorization::Model.build(
|
|
124
|
+
permissions: Permissions.to(user.role),
|
|
125
|
+
policies: { default: :sales, sales: SalesPolicy },
|
|
126
|
+
context: {
|
|
127
|
+
user: user,
|
|
128
|
+
to_permit: ['dashboard', 'controllers', 'sales', 'index']
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
# 4a. Ask about feature permissions for the current context.
|
|
133
|
+
authorization.permissions.to?('visit') # => true
|
|
134
|
+
authorization.permissions.to?('export') # => false
|
|
135
|
+
|
|
136
|
+
# 4b. Ask the same feature about a different context.
|
|
137
|
+
can_export = authorization.permissions.to('export')
|
|
138
|
+
can_export.context?('billings') # => true
|
|
139
|
+
can_export.context?('sales') # => false
|
|
140
|
+
|
|
141
|
+
# 4c. Ask a policy about a record.
|
|
142
|
+
charge = OpenStruct.new(id: 2, user_id: user.id)
|
|
143
|
+
|
|
144
|
+
authorization.to(:sales).edit?(charge) # => true
|
|
145
|
+
authorization.policy.edit?(charge) # => true (uses the default policy)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Permissions
|
|
149
|
+
|
|
150
|
+
A permission check answers one question: given a role and the context the request is happening in, is a feature allowed?
|
|
151
|
+
|
|
152
|
+
### Roles are plain data
|
|
153
|
+
|
|
154
|
+
A role is a Hash whose keys are feature names and whose values are rules:
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
role = {
|
|
158
|
+
'visit' => { 'only' => ['users'] },
|
|
159
|
+
'export' => { 'only' => ['users.reports'] },
|
|
160
|
+
'manage' => { 'any' => false }
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Because a role is plain data, it serializes cleanly. You can store roles as JSON in a database, edit them through an admin screen, and load them at runtime without touching code:
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
require 'json'
|
|
168
|
+
|
|
169
|
+
roles = JSON.parse(current_account.roles_json)
|
|
170
|
+
permissions = Micro::Authorization::Permissions.new(roles['user'], context: [])
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Permission rules
|
|
174
|
+
|
|
175
|
+
Each feature maps to one of these rules:
|
|
176
|
+
|
|
177
|
+
| Rule | Meaning |
|
|
178
|
+
| ----------------------- | ---------------------------------------------- |
|
|
179
|
+
| `true` | Allowed in every context. |
|
|
180
|
+
| `false` | Denied in every context. |
|
|
181
|
+
| missing key or `nil` | Denied. |
|
|
182
|
+
| `{ 'any' => true }` | Allowed in every context. |
|
|
183
|
+
| `{ 'any' => false }` | Denied in every context. |
|
|
184
|
+
| `{ 'only' => [...] }` | Allowed only in the listed contexts. |
|
|
185
|
+
| `{ 'except' => [...] }` | Allowed everywhere except the listed contexts. |
|
|
186
|
+
|
|
187
|
+
`{ 'any' => nil }` and any unrecognized key (for example `{ 'sometimes' => [...] }`) raise `NotImplementedError`, so a malformed role fails loudly instead of silently granting or denying access.
|
|
188
|
+
|
|
189
|
+
### Context matching and dot notation
|
|
190
|
+
|
|
191
|
+
The context is an array of strings that describes where the request is happening. In a Rails controller that is usually `[controller_name, action_name]`, but it can be any list of identifiers. Matching is case insensitive; both the context and the rule values are downcased before comparison.
|
|
192
|
+
|
|
193
|
+
A single string in `only` or `except` matches when the context includes it:
|
|
194
|
+
|
|
195
|
+
```ruby
|
|
196
|
+
role = { 'visit' => { 'only' => ['users'] } }
|
|
197
|
+
permissions = Micro::Authorization::Permissions.new(role, context: [])
|
|
198
|
+
|
|
199
|
+
permissions.to('visit').context?(['users']) # => true
|
|
200
|
+
permissions.to('visit').context?(['sales']) # => false
|
|
19
201
|
```
|
|
20
202
|
|
|
21
|
-
|
|
203
|
+
A string with dots, such as `'users.reports'`, splits on the dot and requires every segment to be present in the context. Entries in the array are still combined with OR, so the rule means "users and reports, or any other listed entry":
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
role = { 'export' => { 'only' => ['users.reports'] } }
|
|
207
|
+
permissions = Micro::Authorization::Permissions.new(role, context: [])
|
|
208
|
+
|
|
209
|
+
permissions.to('export').context?(['users', 'reports']) # => true
|
|
210
|
+
permissions.to('export').context?(['users']) # => false
|
|
22
211
|
```
|
|
23
|
-
|
|
212
|
+
|
|
213
|
+
### Checking permissions
|
|
214
|
+
|
|
215
|
+
`Micro::Authorization::Permissions.new(role, context:)` returns a permissions model bound to a context. From there you have two ways to ask questions.
|
|
216
|
+
|
|
217
|
+
Use `to?` and `to_not?` to check a feature against the context the model was built with. Pass a single feature or an array, in which case every feature must be allowed:
|
|
218
|
+
|
|
219
|
+
```ruby
|
|
220
|
+
role = { 'visit' => true, 'comment' => false }
|
|
221
|
+
permissions = Micro::Authorization::Permissions.new(role, context: ['sales', 'index'])
|
|
222
|
+
|
|
223
|
+
permissions.to?('visit') # => true
|
|
224
|
+
permissions.to?('comment') # => false
|
|
225
|
+
permissions.to?(['visit', 'comment']) # => false (comment is denied)
|
|
226
|
+
permissions.to_not?('comment') # => true
|
|
24
227
|
```
|
|
25
228
|
|
|
26
|
-
|
|
229
|
+
Use `to(feature)` to get a checker you can test against any context with `context?`, regardless of the model's own context:
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
role = {
|
|
233
|
+
'visit' => { 'any' => true },
|
|
234
|
+
'comment' => { 'except' => ['sales'] }
|
|
235
|
+
}
|
|
236
|
+
permissions = Micro::Authorization::Permissions.new(role, context: ['sales', 'index'])
|
|
237
|
+
|
|
238
|
+
can_comment = permissions.to('comment')
|
|
239
|
+
can_comment.context?('invoices') # => true
|
|
240
|
+
can_comment.context?('sales') # => false
|
|
241
|
+
|
|
242
|
+
can_comment.features # => ['comment'] (the features this checker verifies)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
The model caches each `to?` result per feature, so repeated checks in the same request are cheap.
|
|
246
|
+
|
|
247
|
+
### Multiple roles
|
|
248
|
+
|
|
249
|
+
Pass an array of roles to grant a user the union of their permissions. A feature is allowed when at least one role allows it:
|
|
250
|
+
|
|
251
|
+
```ruby
|
|
252
|
+
analytics = { 'export' => { 'only' => ['reports'] } }
|
|
253
|
+
support = { 'export' => { 'only' => ['users.reports'] } }
|
|
254
|
+
|
|
255
|
+
permissions = Micro::Authorization::Permissions.new([analytics, support], context: [])
|
|
256
|
+
|
|
257
|
+
permissions.to('export').context?(['sales', 'reports']) # => true (granted by analytics)
|
|
258
|
+
permissions.to('export').context?(['users', 'reports']) # => true (granted by support)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Policies
|
|
262
|
+
|
|
263
|
+
Permissions decide what a role can do in a place. Policies decide what a user can do to a record. A policy is a class with predicate methods, in the style of Pundit.
|
|
264
|
+
|
|
265
|
+
### Defining a policy
|
|
266
|
+
|
|
267
|
+
Subclass `Micro::Authorization::Policy` and define methods that end in `?`. Inside a policy you can read `user` (an alias of `current_user`), `subject`, `context`, and `permissions`:
|
|
268
|
+
|
|
269
|
+
```ruby
|
|
270
|
+
class CommentPolicy < Micro::Authorization::Policy
|
|
271
|
+
def edit?(comment)
|
|
272
|
+
user.id == comment.author_id
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
policy = CommentPolicy.new({ user: current_user })
|
|
277
|
+
policy.edit?(comment) # => true or false
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Any predicate method you have not defined returns `false`. Deny by default is the standard behavior, so a feature you forget to handle stays forbidden.
|
|
281
|
+
|
|
282
|
+
```ruby
|
|
283
|
+
policy = Micro::Authorization::Policy.new({})
|
|
284
|
+
|
|
285
|
+
policy.index? # => false
|
|
286
|
+
policy.show?(record) # => false
|
|
27
287
|
```
|
|
28
|
-
|
|
288
|
+
|
|
289
|
+
Calling a method that does not end in `?` raises `NoMethodError`, so typos in real method names still surface.
|
|
290
|
+
|
|
291
|
+
Inside a policy, `current_user` reads `context[:user]` first, then falls back to `context[:current_user]`.
|
|
292
|
+
|
|
293
|
+
### The subject
|
|
294
|
+
|
|
295
|
+
A policy can receive the record it is about in two ways. You can pass it as the second argument when constructing the policy, and read it through `subject`:
|
|
296
|
+
|
|
297
|
+
```ruby
|
|
298
|
+
class RecordPolicy < Micro::Authorization::Policy
|
|
299
|
+
def show?
|
|
300
|
+
user.id == subject.user_id
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
RecordPolicy.new({ user: current_user }, record).show?
|
|
29
305
|
```
|
|
30
306
|
|
|
31
|
-
|
|
307
|
+
Or you can pass it as an argument to the predicate method, which is handy when one policy instance answers questions about several records:
|
|
32
308
|
|
|
33
309
|
```ruby
|
|
34
|
-
|
|
35
|
-
|
|
310
|
+
class RecordPolicy < Micro::Authorization::Policy
|
|
311
|
+
def show?(record)
|
|
312
|
+
user.id == record.user_id
|
|
313
|
+
end
|
|
314
|
+
end
|
|
36
315
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
316
|
+
policy = RecordPolicy.new({ user: current_user })
|
|
317
|
+
policy.show?(record_a) # => true
|
|
318
|
+
policy.show?(record_b) # => false
|
|
319
|
+
```
|
|
42
320
|
|
|
43
|
-
|
|
44
|
-
'visit' => { 'except' => ['billings'] },
|
|
45
|
-
'export' => { 'except' => ['sales'] }
|
|
46
|
-
}
|
|
321
|
+
### Reading permissions inside a policy
|
|
47
322
|
|
|
48
|
-
|
|
49
|
-
'admin' => ADMIN,
|
|
50
|
-
'user' => USER
|
|
51
|
-
}
|
|
323
|
+
A policy can combine record-level checks with feature permissions. When the authorization object builds a policy it passes the permissions in, so `permissions` is available inside:
|
|
52
324
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
325
|
+
```ruby
|
|
326
|
+
class ReportPolicy < Micro::Authorization::Policy
|
|
327
|
+
def show?(report)
|
|
328
|
+
permissions.to?('visit') && current_user.id == report.owner_id
|
|
56
329
|
end
|
|
330
|
+
end
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## The authorization object
|
|
334
|
+
|
|
335
|
+
`Micro::Authorization::Model` ties a user, a context, a role's permissions, and a set of policies into a single object for the current request.
|
|
336
|
+
|
|
337
|
+
### Building it
|
|
338
|
+
|
|
339
|
+
Use `Model.build` with three keyword arguments. `policies` is optional:
|
|
340
|
+
|
|
341
|
+
```ruby
|
|
342
|
+
authorization = Micro::Authorization::Model.build(
|
|
343
|
+
permissions: Permissions.to(user.role),
|
|
344
|
+
policies: { default: SalesPolicy, sales: SalesPolicy },
|
|
345
|
+
context: {
|
|
346
|
+
user: user,
|
|
347
|
+
to_permit: ['sales', 'index']
|
|
348
|
+
}
|
|
349
|
+
)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
The `context` Hash is read like this:
|
|
353
|
+
|
|
354
|
+
- `:to_permit` (or its alias `:permissions`) is the context used for permission checks. One of them is required if you want to check permissions.
|
|
355
|
+
- `:user` is the current user. It becomes `user` / `current_user` inside policies.
|
|
356
|
+
- Every other key stays in the context and is handed to policies, so a policy can read anything else you put there.
|
|
357
|
+
|
|
358
|
+
### Asking about permissions
|
|
359
|
+
|
|
360
|
+
`authorization.permissions` returns the permissions model described above, so the full `to?`, `to_not?`, and `to(...).context?` interface is available:
|
|
361
|
+
|
|
362
|
+
```ruby
|
|
363
|
+
authorization.permissions.to?('visit') # => true
|
|
364
|
+
authorization.permissions.to('export').context?('sales') # => false
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### Fetching policies
|
|
368
|
+
|
|
369
|
+
`to(key, subject: nil)` looks up a registered policy by name, builds it with the current context and permissions, and returns the instance:
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
authorization.to(:sales).edit?(charge)
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
`policy(key = :default, subject: nil)` does the same but defaults to the `:default` policy, which reads well when you have one main policy per request:
|
|
376
|
+
|
|
377
|
+
```ruby
|
|
378
|
+
authorization.policy.edit?(charge) # uses :default
|
|
379
|
+
authorization.policy(:sales).edit?(charge) # same as to(:sales)
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
If you ask for a key that was never registered, you get the base `Micro::Authorization::Policy`, which denies every predicate. Unknown features are forbidden rather than raising.
|
|
383
|
+
|
|
384
|
+
Policy instances are cached per key, so calling `to(:sales)` repeatedly returns the same object within a request. Passing a `subject:` builds a fresh instance bound to that subject:
|
|
57
385
|
|
|
58
|
-
|
|
386
|
+
```ruby
|
|
387
|
+
authorization.to(:report, subject: report).show?
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Registering policies
|
|
391
|
+
|
|
392
|
+
You can register policies when building the object through the `policies:` keyword, or afterward with `add_policy` and `add_policies`:
|
|
393
|
+
|
|
394
|
+
```ruby
|
|
395
|
+
authorization.add_policy(:sales, SalesPolicy)
|
|
396
|
+
authorization.add_policies(sales: SalesPolicy, report: ReportPolicy)
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
`add_policies` expects a Hash and raises `ArgumentError` otherwise.
|
|
400
|
+
|
|
401
|
+
The `:default` key is special. It can hold a policy class, or a Symbol that points at another registered policy, so you can name one of your policies as the default:
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
Micro::Authorization::Model.build(
|
|
405
|
+
permissions: role,
|
|
406
|
+
policies: { default: :sales, sales: SalesPolicy },
|
|
407
|
+
context: { user: user }
|
|
408
|
+
)
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### Cloning with map
|
|
412
|
+
|
|
413
|
+
`map` returns a new authorization object, replacing the context, the policies, or both. Whatever you leave out is carried over from the original, and the original is left untouched, which helps when one request needs to check several contexts:
|
|
414
|
+
|
|
415
|
+
```ruby
|
|
416
|
+
on_releases = authorization.map(context: ['dashboard', 'releases', 'index'])
|
|
417
|
+
|
|
418
|
+
on_releases.permissions.to?('visit') # checked against the new context
|
|
419
|
+
authorization.equal?(on_releases) # => false
|
|
420
|
+
|
|
421
|
+
with_admin_policy = authorization.map(policies: { default: AdminPolicy })
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
Calling `map` without `context:` or `policies:` raises `ArgumentError`, since there would be nothing to change.
|
|
59
425
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
426
|
+
## Using it with Rails
|
|
427
|
+
|
|
428
|
+
The context maps naturally onto a Rails controller. Using `controller_path.split('/') + [action_name]` keeps namespaced controllers distinct, so `Admin::UsersController#index` becomes `['admin', 'users', 'index']`. A common setup builds the authorization object once per request and exposes a helper:
|
|
429
|
+
|
|
430
|
+
```ruby
|
|
431
|
+
class ApplicationController < ActionController::Base
|
|
432
|
+
before_action :authenticate_user!
|
|
433
|
+
|
|
434
|
+
private
|
|
435
|
+
|
|
436
|
+
def authorization
|
|
437
|
+
@authorization ||= Micro::Authorization::Model.build(
|
|
438
|
+
permissions: current_user.role_permissions, # a Hash, maybe loaded from the DB
|
|
439
|
+
policies: { default: :record, record: RecordPolicy },
|
|
440
|
+
context: {
|
|
441
|
+
user: current_user,
|
|
442
|
+
to_permit: controller_path.split('/') + [action_name]
|
|
443
|
+
}
|
|
444
|
+
)
|
|
64
445
|
end
|
|
65
446
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
to_permit: ['dashboard', 'controllers', 'sales', 'index']
|
|
72
|
-
}
|
|
73
|
-
)
|
|
447
|
+
def authorize_visit!
|
|
448
|
+
redirect_to root_path unless authorization.permissions.to?('visit')
|
|
449
|
+
end
|
|
450
|
+
end
|
|
451
|
+
```
|
|
74
452
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
# 2. :user is an optional key
|
|
79
|
-
# 3. Any key different of :permissions, will be passed as a policy context.
|
|
453
|
+
```ruby
|
|
454
|
+
class ReportsController < ApplicationController
|
|
455
|
+
before_action :authorize_visit!
|
|
80
456
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
457
|
+
def show
|
|
458
|
+
@report = Report.find(params[:id])
|
|
459
|
+
redirect_to reports_path unless authorization.policy.show?(@report)
|
|
460
|
+
end
|
|
461
|
+
end
|
|
462
|
+
```
|
|
84
463
|
|
|
85
|
-
|
|
86
|
-
has_permission_to = authorization.permissions.to('export')
|
|
87
|
-
has_permission_to.context?('billings') #=> true
|
|
88
|
-
has_permission_to.context?('sales') #=> false
|
|
464
|
+
Because roles are data, `current_user.role_permissions` can come straight from a column or an associated table, which lets non-developers manage roles through your own admin tools.
|
|
89
465
|
|
|
90
|
-
|
|
466
|
+
## Comparison with Pundit and CanCanCan
|
|
91
467
|
|
|
92
|
-
|
|
93
|
-
authorization.to(:sales).edit?(charge) #=> true
|
|
468
|
+
All three gems solve authorization, with different shapes.
|
|
94
469
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
authorization.to(:default).edit?(charge) #=> true
|
|
470
|
+
- [Pundit](https://github.com/varvet/pundit) is built around policy classes, one per resource. `u-authorization` has the same idea in its `Policy` layer, and adds a separate permissions layer for role and context checks. Pundit leaves roles to you.
|
|
471
|
+
- [CanCanCan](https://github.com/CanCanCommunity/cancancan) centralizes rules in one `Ability` class written in Ruby. `u-authorization` keeps role rules as data instead of code, so they can be stored and edited outside the codebase, and keeps record-level logic in policy classes.
|
|
98
472
|
|
|
99
|
-
|
|
100
|
-
# but if there is a policy defined as ":default", it will be fetched and instantiated by default.
|
|
101
|
-
authorization.policy.edit?(charge) #=> true
|
|
102
|
-
authorization.policy(:sales).edit?(charge) #=> true
|
|
473
|
+
Reasons you might reach for `u-authorization`:
|
|
103
474
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
475
|
+
- You want roles defined as data (Hash or JSON) so they can live in a database and change without a deploy.
|
|
476
|
+
- You want permission checks that are aware of the controller and action, not only the model.
|
|
477
|
+
- You want a small library with no runtime dependencies.
|
|
478
|
+
- You want role and context checks and record-level checks to stay separate instead of merging into one concept.
|
|
108
479
|
|
|
109
|
-
|
|
480
|
+
If you only need record-level policies, Pundit is a fine and popular choice. If you prefer one central Ruby file of abilities, CanCanCan fits well.
|
|
110
481
|
|
|
111
|
-
|
|
482
|
+
## Development
|
|
112
483
|
|
|
113
|
-
|
|
114
|
-
# Multi role permissions #
|
|
115
|
-
#========================#
|
|
484
|
+
After cloning the repository, install dependencies and set up the project:
|
|
116
485
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
context: {
|
|
121
|
-
user: user,
|
|
122
|
-
to_permit: ['dashboard', 'controllers', 'sales', 'index']
|
|
123
|
-
}
|
|
124
|
-
)
|
|
486
|
+
```bash
|
|
487
|
+
bin/setup
|
|
488
|
+
```
|
|
125
489
|
|
|
126
|
-
|
|
127
|
-
authorization.permissions.to?('export') #=> true
|
|
490
|
+
Run the test suite, which is the default Rake task:
|
|
128
491
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
492
|
+
```bash
|
|
493
|
+
bundle exec rake test
|
|
494
|
+
# or simply
|
|
495
|
+
bundle exec rake
|
|
132
496
|
```
|
|
133
497
|
|
|
134
|
-
|
|
498
|
+
Open a console with the gem loaded to experiment:
|
|
499
|
+
|
|
500
|
+
```bash
|
|
501
|
+
bin/console
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
To run the suite across Ruby versions locally, use [mise](https://mise.jdx.dev). The `.tool-versions` file pins the project's default Ruby; add the versions you want to cover and run the suite under each. CI runs the full matrix, from Ruby 2.7 through the current development build, defined in `.github/workflows/ci.yml`.
|
|
505
|
+
|
|
506
|
+
## Contributing
|
|
507
|
+
|
|
508
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/u-gems/u-authorization. Please add or update tests for any behavior you change; the test suite is the contract for the public API.
|
|
509
|
+
|
|
510
|
+
1. Fork the repository.
|
|
511
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
|
512
|
+
3. Add tests and make them pass with `bundle exec rake test`.
|
|
513
|
+
4. Commit your changes and open a pull request.
|
|
514
|
+
|
|
515
|
+
Everyone interacting in the project's codebases and issue trackers is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|
|
516
|
+
|
|
517
|
+
## License
|
|
518
|
+
|
|
519
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
|
520
|
+
|
|
521
|
+
## Code of conduct
|
|
135
522
|
|
|
136
|
-
|
|
523
|
+
Everyone interacting in the µ-authorization project is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "u-authorization"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/u-authorization.gemspec
CHANGED
|
@@ -10,8 +10,14 @@ Gem::Specification.new do |spec|
|
|
|
10
10
|
|
|
11
11
|
spec.summary = 'Authorization library and role managment'
|
|
12
12
|
spec.description = 'Simple authorization library and role managment for Ruby.'
|
|
13
|
-
spec.homepage = 'https://github.com/
|
|
13
|
+
spec.homepage = 'https://github.com/u-gems/u-authorization'
|
|
14
14
|
spec.license = 'MIT'
|
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
|
|
16
|
+
|
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/u-gems/u-authorization'
|
|
19
|
+
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
20
|
+
spec.metadata['bug_tracker_uri'] = "#{spec.homepage}/issues"
|
|
15
21
|
|
|
16
22
|
# Specify which files should be added to the gem when it is released.
|
|
17
23
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
@@ -22,6 +28,6 @@ Gem::Specification.new do |spec|
|
|
|
22
28
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
23
29
|
spec.require_paths = ['lib']
|
|
24
30
|
|
|
25
|
-
spec.
|
|
26
|
-
spec.add_development_dependency 'rake', '~>
|
|
31
|
+
spec.add_development_dependency 'bundler'
|
|
32
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
|
27
33
|
end
|
metadata
CHANGED
|
@@ -1,29 +1,42 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: u-authorization
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rodrigo Serradura
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: bundler
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
13
26
|
- !ruby/object:Gem::Dependency
|
|
14
27
|
name: rake
|
|
15
28
|
requirement: !ruby/object:Gem::Requirement
|
|
16
29
|
requirements:
|
|
17
30
|
- - "~>"
|
|
18
31
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
32
|
+
version: '13.0'
|
|
20
33
|
type: :development
|
|
21
34
|
prerelease: false
|
|
22
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
36
|
requirements:
|
|
24
37
|
- - "~>"
|
|
25
38
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
39
|
+
version: '13.0'
|
|
27
40
|
description: Simple authorization library and role managment for Ruby.
|
|
28
41
|
email:
|
|
29
42
|
- rodrigo.serradura@gmail.com
|
|
@@ -31,12 +44,17 @@ executables: []
|
|
|
31
44
|
extensions: []
|
|
32
45
|
extra_rdoc_files: []
|
|
33
46
|
files:
|
|
47
|
+
- ".github/workflows/ci.yml"
|
|
34
48
|
- ".gitignore"
|
|
35
|
-
-
|
|
49
|
+
- CHANGELOG.md
|
|
50
|
+
- CLAUDE.md
|
|
51
|
+
- CODE_OF_CONDUCT.md
|
|
36
52
|
- Gemfile
|
|
37
|
-
-
|
|
53
|
+
- LICENSE.txt
|
|
38
54
|
- README.md
|
|
39
55
|
- Rakefile
|
|
56
|
+
- bin/console
|
|
57
|
+
- bin/setup
|
|
40
58
|
- lib/micro/authorization.rb
|
|
41
59
|
- lib/micro/authorization/model.rb
|
|
42
60
|
- lib/micro/authorization/permissions.rb
|
|
@@ -48,11 +66,14 @@ files:
|
|
|
48
66
|
- lib/micro/authorization/version.rb
|
|
49
67
|
- lib/u-authorization.rb
|
|
50
68
|
- u-authorization.gemspec
|
|
51
|
-
homepage: https://github.com/
|
|
69
|
+
homepage: https://github.com/u-gems/u-authorization
|
|
52
70
|
licenses:
|
|
53
71
|
- MIT
|
|
54
|
-
metadata:
|
|
55
|
-
|
|
72
|
+
metadata:
|
|
73
|
+
homepage_uri: https://github.com/u-gems/u-authorization
|
|
74
|
+
source_code_uri: https://github.com/u-gems/u-authorization
|
|
75
|
+
changelog_uri: https://github.com/u-gems/u-authorization/blob/main/CHANGELOG.md
|
|
76
|
+
bug_tracker_uri: https://github.com/u-gems/u-authorization/issues
|
|
56
77
|
rdoc_options: []
|
|
57
78
|
require_paths:
|
|
58
79
|
- lib
|
|
@@ -60,15 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
60
81
|
requirements:
|
|
61
82
|
- - ">="
|
|
62
83
|
- !ruby/object:Gem::Version
|
|
63
|
-
version: 2.
|
|
84
|
+
version: 2.7.0
|
|
64
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
86
|
requirements:
|
|
66
87
|
- - ">="
|
|
67
88
|
- !ruby/object:Gem::Version
|
|
68
89
|
version: '0'
|
|
69
90
|
requirements: []
|
|
70
|
-
rubygems_version:
|
|
71
|
-
signing_key:
|
|
91
|
+
rubygems_version: 4.0.12
|
|
72
92
|
specification_version: 4
|
|
73
93
|
summary: Authorization library and role managment
|
|
74
94
|
test_files: []
|
data/.travis.yml
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
language: ruby
|
|
2
|
-
|
|
3
|
-
sudo: false
|
|
4
|
-
|
|
5
|
-
rvm:
|
|
6
|
-
- 2.2.0
|
|
7
|
-
- 2.3.0
|
|
8
|
-
- 2.4.0
|
|
9
|
-
- 2.5.0
|
|
10
|
-
- 2.6.0
|
|
11
|
-
|
|
12
|
-
cache: bundler
|
|
13
|
-
|
|
14
|
-
before_install:
|
|
15
|
-
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
|
16
|
-
- gem install bundler -v '< 2'
|
|
17
|
-
|
|
18
|
-
install: bundle install --jobs=3 --retry=3
|
|
19
|
-
|
|
20
|
-
before_script:
|
|
21
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
|
22
|
-
- chmod +x ./cc-test-reporter
|
|
23
|
-
- "./cc-test-reporter before-build"
|
|
24
|
-
|
|
25
|
-
script: "rake"
|
|
26
|
-
|
|
27
|
-
after_success:
|
|
28
|
-
- "./cc-test-reporter after-build -t simplecov"
|
data/Gemfile.lock
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
u-authorization (2.3.0)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
ansi (1.5.0)
|
|
10
|
-
builder (3.2.3)
|
|
11
|
-
docile (1.3.2)
|
|
12
|
-
json (2.2.0)
|
|
13
|
-
minitest (5.11.3)
|
|
14
|
-
minitest-reporters (1.3.6)
|
|
15
|
-
ansi
|
|
16
|
-
builder
|
|
17
|
-
minitest (>= 5.0)
|
|
18
|
-
ruby-progressbar
|
|
19
|
-
rake (10.5.0)
|
|
20
|
-
ruby-progressbar (1.10.1)
|
|
21
|
-
simplecov (0.17.0)
|
|
22
|
-
docile (~> 1.1)
|
|
23
|
-
json (>= 1.8, < 3)
|
|
24
|
-
simplecov-html (~> 0.10.0)
|
|
25
|
-
simplecov-html (0.10.2)
|
|
26
|
-
|
|
27
|
-
PLATFORMS
|
|
28
|
-
ruby
|
|
29
|
-
|
|
30
|
-
DEPENDENCIES
|
|
31
|
-
minitest (~> 5.11, >= 5.11.3)
|
|
32
|
-
minitest-reporters (~> 1.3, >= 1.3.6)
|
|
33
|
-
rake (~> 10.0)
|
|
34
|
-
simplecov
|
|
35
|
-
u-authorization!
|
|
36
|
-
|
|
37
|
-
BUNDLED WITH
|
|
38
|
-
1.17.2
|