Passpartu - changelog
Passpartu makes policies great again (works awesome with Pundit).
- 3.1.1
- 3.0.0
- 2.7.3
Instead of this:
classPostPolicy < ApplicationPolicydefupdate?user.super_admin? || user.admin? || user.manager? || user.supervisor?endendjust this:
classPostPolicy < ApplicationPolicydefupdate?user.can?(:post,:update)endendInclude Passpartu into your policy model.
classUserincludePasspartuendNOTE: Your User model must respond to role method that returns a string or a symbol!
Keep all your policies in one place.
Create ./config/passpartu.yml and start writing your policies.
# ./config/passpartu.ymlmanager: &managerorder:
create: trueedit: truedelete: falseproduct:
create: trueedit: truedelete: false# yaml files supports inheritance!admin:
<<: *managerpost:
create: falseupdate: truedelete: trueorder:
create: trueedit: truedelete: trueproduct:
create: falseedit: truedelete: trueitems: crud: truedelete: falseIt's possible to use crud key to set values for create, read, update, delete at once.
create, read, update, delete has higher priority than crud
In case crud: true and delete: false - result false
It's possible to include specific roles to checks
user_admin.can?(:orders,:edit)# check policy for admin and returns true if policy trueuser_admin.can?(:orders,:edit,only: :admin)# returns true because the user is an admin and we included only adminuser_manager.can?(:orders,:edit,only: :admin)# returns false because user is manager and we included only adminIt's possible to give an array as only attribute
user_admin.can?(:orders,:edit,only: [:admin,:manager])# returns trueuser_manager.can?(:orders,:edit,only: [:admin,:manager])# returns trueNote: only has higher priority than except/skip. Do not use both.
user_admin.can?(:orders,:edit,only: :admin,except: :admin)# returns trueIt's possible to exclude roles from checks
user_admin.can?(:orders,:edit)# check policy for admin and returns true if policy trueuser_admin.can?(:orders,:edit,except: :admin)# returns false because user is admin and we excluded adminIt's possible to give an array as except attribute
user_admin.can?(:orders,:edit,except: [:admin,:manager])# returns falseuser_manager.can?(:orders,:edit,except: [:admin,:manager])# returns falseskip alias to except
Note: expect has higher priority than skip. Do not use both.
user_agent.can?(:orders,:edit,except: [:admin,:manager]){user_agent.orders.include?(order)}# equals touser_agent.can?(:orders,:edit,skip: [:admin,:manager]){user_agent.orders.include?(order)}Check user roles AND policy rule
# check if user admin AND returns true if policy trueuser_admin.admin_can?(:orders,:edit)# true# check if user manager AND returns true if policy trueuser_admin.manager_can?(:orders,:edit)# false# check rules as usual AND code in the block user_agent.can?(:orders,:edit,except: [:admin,:manager]){user_agent.orders.include?(order)}# OR user_agent.agent_can?(:orders,:edit,except: [:admin,:manager]){user_agent.orders.include?(order)}Option 'maybe' means that user can do something if the block returns true. In this case block is required and error is raised when option is maybe and no block given.
manager:
products:
create: truedelete: falsebookings:
update: maybemanager.can?(:bookings,:update)# raises errormanager.can?(:bookings,:update){user.bookings.include?(booking)}# returns true or falseAllow or restrict absolutely everything for particular role or/and particular domain.
# ./config/initializers/passpartu.rbPasspartu.configuredo |config|
config.check_waterfall=trueend# ./config/passpartu.ymlsuper_admin: truesuper_looser: falsemedium_looser:
orders:
create: truedelete: falseproducts: trueuser_super_admin.can?(:do,:whatever,:want)# trueuser_super_loser.can?(:do,:whatever,:want)# falseuser_medium_loser.can?(:orders,:create)# trueuser_medium_loser.can?(:orders,:delete)# falseuser_medium_loser.can?(:products,:create)# trueuser_medium_loser.can?(:products,:create,:and_delete)# trueYou need to check custom rule for agent
# ./config/passpartu.ymladmin:
order:
create: trueedit: truedelete: truemanager:
order:
create: trueedit: truedelete: falseagent:
order:
create: trueedit: truedelete: falseuser.can?(:order,:edit,except: :agent) || user.agent_can?(:order,:edit){user.orders.include?(order)}- This code returns
trueif user isadminormanager - This code returns
trueif user isagentAND if agent policy set totrueAND if given block returns true
You can configure Passpartu by creating ./config/initializers/passpartu.rb.
Passpartu.configuredo |config|
config.policy_file='./config/passpartu.yml'config.raise_policy_missed_error=trueconfig.check_waterfall=falseconfig.role_access_method=:roleendBy default Passpartu will raise an PolicyMissedError if policy is missed in passpartu.yml. In initializer set config.raise_policy_missed_error = false in order to return false in case when policy is not defined. This is a good approach to write only "positive" policies (only true) and automatically restricts everything that is not mentioned in passpartu.yml
Add this line to your application's Gemfile:
gem'passpartu'And then execute:
$ bundle
Or install it yourself as:
$ gem install passpartu
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/coaxsoft/passpartu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Passpartu project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Initially designed and created by Orest Falchuk
