Download the PHP package brzez/access-policy-bundle without Composer
On this page you can find all versions of the php package brzez/access-policy-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package access-policy-bundle
symfony-access-policy
Overview
Access checker, inspired by Laravel.
It allows for checking access via simple can and cannot methods accessible via the brzez_access_policy.access_policy_provider service.
It also extends twig with those two methods as global functions.
can needs minimum 2 arguments:
- intent - what are you checking access for ex. view, edit etc
- object - what object are you checking the access for cannot is just an inverse of can (so !can())
It is also possible to pass additional variables to the can/cannot methods.
The 2nd arg is always used for finding the matching policy.
The rest are just passed to the policy *can()** method.
Policy needs to implement AccessPolicyInterface which requires the getPoliciedClass method. Policied objects are checked via
Which means that it will work for mocked entities. It's also possible to implement 'global' policies for interfaces / parent classes.
Policies are registered as services.
The policy service needs to be tagged as access_policy so it will be recognized by the access policy provider.
Installation
Enable the bundle in the kernel
Registering policies
In services.yml
Naming intent methods
When using can/cannot methods the intent is written in kebab-case, without the can/cannot word.
Example:
Usage
PolicyProvider can be accessed via container
Controllers can use Brzez\AccessPolicyBundle\Traits\AccessCheckerTrait which will extend the controller by adding:
- can(intent, object)
- cannot(intent, object)
- getPolicyProvider() methods
It also adds twig global functions - can(...) and cannot(...) which can be used like this:
Example
Let's say we have SomeEntity and we need to check view access via our policy.
We need to create SomeEntityPolicy with canView method.
Link the policy to the entity
In app/config/services.yml
Now you can check access in the controller:
You can also check access in twig views:
Changelog
- 0.9.0 Less strict class checking - is_a (instanceof) instead of strict get_class == classname This way it will work with mocked entities