Passpartu - changelog
Passpartu makes policies great again (works awesome with Pundit).
- 3.1.1
- 3.0.0
- 2.7.3
Instead of this:
class PostPolicy < ApplicationPolicy
def update?
user.super_admin? || user.admin? || user.manager? || user.supervisor?
end
endjust this:
class PostPolicy < ApplicationPolicy
def update?
user.can?(:post, :update)
end
endInclude Passpartu into your policy model.
class User
include Passpartu
endNOTE: 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.yml
manager: &manager
order:
create: true
edit: true
delete: false
product:
create: true
edit: true
delete: false
# yaml files supports inheritance!
admin:
<<: *manager
post:
create: false
update: true
delete: true
order:
create: true
edit: true
delete: true
product:
create: false
edit: true
delete: true
items:
crud: true
delete: 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 true
user_admin.can?(:orders, :edit, only: :admin) # returns true because the user is an admin and we included only admin
user_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 true
user_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 true
user_admin.can?(:orders, :edit, except: :admin) # returns false because user is admin and we excluded admin
It's possible to give an array as except attribute
user_admin.can?(:orders, :edit, except: [:admin, :manager]) # returns false
user_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 to
user_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 true
user_admin.admin_can?(:orders, :edit) # true
# check if user manager AND returns true if policy true
user_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: true
delete: false
bookings:
update: maybemanager.can?(:bookings, :update) # raises error
manager.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.rb
Passpartu.configure do |config|
config.check_waterfall = true
end# ./config/passpartu.yml
super_admin: true
super_looser: false
medium_looser:
orders:
create: true
delete: false
products: trueuser_super_admin.can?(:do, :whatever, :want) # true
user_super_loser.can?(:do, :whatever, :want) # false
user_medium_loser.can?(:orders, :create) # true
user_medium_loser.can?(:orders, :delete) # false
user_medium_loser.can?(:products, :create) # true
user_medium_loser.can?(:products, :create, :and_delete) # trueYou need to check custom rule for agent
# ./config/passpartu.yml
admin:
order:
create: true
edit: true
delete: true
manager:
order:
create: true
edit: true
delete: false
agent:
order:
create: true
edit: true
delete: false user.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.configure do |config|
config.policy_file = './config/passpartu.yml'
config.raise_policy_missed_error = true
config.check_waterfall = false
config.role_access_method = :role
endBy 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
