PHPackages                             brzez/access-policy-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. brzez/access-policy-bundle

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

brzez/access-policy-bundle
==========================

v0.9.0(9y ago)148[1 issues](https://github.com/brzez/symfony-access-policy/issues)MITPHP

Since May 22Pushed 9y ago1 watchersCompare

[ Source](https://github.com/brzez/symfony-access-policy)[ Packagist](https://packagist.org/packages/brzez/access-policy-bundle)[ RSS](/packages/brzez-access-policy-bundle/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (8)Used By (0)

symfony-access-policy
=====================

[](#symfony-access-policy)

Overview
--------

[](#overview)

Access checker, inspired by [Laravel](https://laravel.com/docs/5.1/authorization#checking-abilities).

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

```
is_a($object, $policy->getPoliciedClass());
```

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
------------

[](#installation)

```
composer require brzez/access-policy-bundle
```

Enable the bundle in the kernel

```
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        //...
        new Brzez\AccessPolicyBundle\BrzezAccessPolicyBundle(),
        //...
    );
    return $bundles;
}
```

Registering policies
--------------------

[](#registering-policies)

In **services.yml**

```
services:
  test_policy:
    class: AppBundle\TestPolicy
    tags:
      - {name: access_policy}
```

Naming intent methods
---------------------

[](#naming-intent-methods)

When using **can**/**cannot** methods the intent is written in kebab-case, without the can/cannot word.

Example:

```
// Will run canChangeStatus($something) on the policy
$this->can('change-status', $something);

// Will return negated canChangeStatus($something)
$this->cannot('change-status', $something);
```

Usage
-----

[](#usage)

*PolicyProvider* can be accessed via container

```
$container->get('brzez_access_policy.access_policy_provider')
```

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:

```
    {% if can('view', someObject) %}
        i can view someObject
    {% endif %}
    {% if cannot('view', someObject) %}
        i cannot view someObject
    {% endif %}
```

Example
-------

[](#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.

```
use Brzez\AccessPolicyBundle\Service\AccessPolicyInterface;

class SomeEntityPolicy implements AccessPolicyInterface
{
    public function canView(SomeEntity $entity)
    {
        // access logic here
        return false;
    }
}
```

Link the policy to the entity

In **app/config/services.yml**

```
services:
  test_policy:
    class: AppBundle\SomeEntityPolicy
    tags:
      - {name: access_policy}
```

Now you can check access in the controller:

```
use Brzez\AccessPolicyBundle\Traits\AccessCheckerTrait;

class DefaultController extends Controller
{
    use AccessCheckerTrait;
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // get $someObject from somewhere
        ...

        if($this->cannot('view', $someObject)){
            throw new AccessDeniedException('...');
        }

        // render view
        ...
    }
}
```

You can also check access in twig views:

```
    {% if can('view', someObject) %}
        i can view someObject
    {% endif %}
    {% if cannot('view', someObject) %}
        i cannot view someObject
    {% endif %}
```

Changelog
---------

[](#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

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

6

Last Release

3636d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1938838?v=4)[Paweł Brzeziński](/maintainers/brzez)[@brzez](https://github.com/brzez)

---

Top Contributors

[![brzez](https://avatars.githubusercontent.com/u/1938838?v=4)](https://github.com/brzez "brzez (42 commits)")

### Embed Badge

![Health badge](/badges/brzez-access-policy-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/brzez-access-policy-bundle/health.svg)](https://phpackages.com/packages/brzez-access-policy-bundle)
```

###  Alternatives

[kartik-v/yii2-password

Useful password strength validation utilities for Yii Framework 2.0

761.2M17](/packages/kartik-v-yii2-password)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
