Original docs: https://github.com/stffn/declarative_authorization/blob/master/README.rdoc
This fork keeps Declarative Authorization working on newer Rails versions, including Rails 8 compatibility work, while aiming for backwards compatibility as far as Rails 4.2 and Ruby 2.2 on a best-effort basis.
It also includes performance improvements for larger authorization rule sets and repeated permission checks:
- indexed rule lookup by context, role, and privilege
- cached flattened role and privilege hierarchies
- fast-path handling for unconditional rules
- per-check caching of repeated
if_attributevalue blocks - per-check caching of repeated nested
if_permitted_tochecks - opt-in request-local caching for repeated
permit?/permitted_to?calls
The request-local cache is deliberately short lived and thread local. It does not use Redis, Memcached, or Rails.cache, and it does not cache permit! exception behavior.
To enable request-local permission caching around a block:
Authorization.with_permission_cache do
authorization_engine.permit?(:show, object: record, user: current_user)
endFor controllers with repeated view/helper permission checks:
class ApplicationController < ActionController::Base
cache_permission_checks
endCall Authorization.clear_cache! after permission-relevant writes inside a cached block/request if the same request can re-check mutated objects or roles.
cp gemfiles/{RAILS_VERSION}.gemfile Gemfile
bundle
bundle exec rake test
Run the opt-in performance tests with:
bundle exec rake test:performance
Run the generic authorization benchmark with:
ruby benchmarks/authorization_rules_benchmark.rb