https://github.com/wilddima/pundit_kit
Extension for pundit which allows to describe namespaces of policies in routes-like style
https://github.com/wilddima/pundit_kit
authorization pundit rails srp
Last synced: about 1 year ago
JSON representation
Extension for pundit which allows to describe namespaces of policies in routes-like style
- Host: GitHub
- URL: https://github.com/wilddima/pundit_kit
- Owner: wilddima
- License: mit
- Created: 2018-03-09T20:37:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-16T13:48:06.000Z (about 8 years ago)
- Last Synced: 2025-03-25T07:36:13.898Z (about 1 year ago)
- Topics: authorization, pundit, rails, srp
- Language: Ruby
- Homepage:
- Size: 68.4 KB
- Stars: 13
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# PunditKit
[](https://codeclimate.com/github/WildDima/pundit_kit/maintainability)
[](https://travis-ci.org/wilddima/pundit_kit)
## Instalation
Add pundit_kit to your gemfile:
```ruby
gem 'pundit_kit'
```
## Usage
Example of initializer routes:
``` ruby
class ClientNotAllowedError < StandardError; end
class UserNotAllowedError < StandardError; end
PunditKit.routes do
namespace :staff, if: -> (user) { user.staff? }, presence: false do
namespace :admin, if: -> (user) { user.admin? }
namespace :user, if: -> (user) { user.user? }, error: UserNotAllowedError
end
namespace :client, if: -> (user) { user.client? }, error: ClientNotAllowedError do
namespace :superclient,
if: -> (user) { user.superclient? },
error: ClientNotAllowedError,
presence: false
end
end
```
Each namespace has these options:
|options|default|description|
|-------|-------|-----------|
|if:|-> { true }| lamda(or any callable object) evaluation of which determines should be used this namespace or not|
|presence:| true | if true then will raise error if policy in this namespace can't be found |
|error:| Pundit::NotAuthorizedError | error which would be raised if authorize call will return false |
## Example
For example yours application logic looks like this:
Include PunditKit to ApplicationController
```
class ApplicationController < ActionController::Base
include PunditKit
end
```
This'll add helpers to yours controllers:
* `authorize_all` - this method will call authorize on every namespace
* `all_policies` - this method will return all namespaces matches to `pundit_namespace_matcher`
## TODO
* scope
* fallbacks
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/wilddima/pundit_kit.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the PunditKit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/wilddima/pundit_kit/blob/master/CODE_OF_CONDUCT.md).