PHPackages                             jeffersonsimaogoncalves/cakephp-permission - 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. jeffersonsimaogoncalves/cakephp-permission

ActiveCakephp-plugin[Authentication &amp; Authorization](/categories/authentication)

jeffersonsimaogoncalves/cakephp-permission
==========================================

The library provides a flexible way to add role-based access control management to CakePHP 3.x

1.1.0(6mo ago)220MITPHPPHP &gt;=7.0CI passing

Since Oct 20Pushed 6mo agoCompare

[ Source](https://github.com/jeffersongoncalves/cakephp-permission)[ Packagist](https://packagist.org/packages/jeffersonsimaogoncalves/cakephp-permission)[ RSS](/packages/jeffersonsimaogoncalves-cakephp-permission/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Permission Management For CakePHP 3.x
=====================================

[](#permission-management-for-cakephp-3x)

[![Build Status](https://camo.githubusercontent.com/5d522e0692ad6d011507deb1c7105876358b08414c401de3f1131a166c7aa6f4/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736c696e63652f63616b657068702d7065726d697373696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/slince/cakephp-permission)[![Coverage Status](https://camo.githubusercontent.com/0da5995097a29fe2c64a7fa0516724e007ab1adeb07ebf40f099e4abdedbc897/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f736c696e63652f63616b657068702d7065726d697373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/slince/cakephp-permission)[![Latest Stable Version](https://camo.githubusercontent.com/f720153cf0911df95dbda714a05dada94114800c2ce448dd78ef17021ac35544/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c696e63652f63616b657068702d7065726d697373696f6e2e7376673f7374796c653d666c61742d737175617265266c6162656c3d737461626c65)](https://packagist.org/packages/slince/cakephp-permission)[![Scrutinizer](https://camo.githubusercontent.com/ff37586b48716dbb3604a8ba3edd964e50503ca79cabf30545899f6df5965404/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f736c696e63652f63616b657068702d7065726d697373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/slince/cakephp-permission/?branch=master)

The library provides a flexible way to add role-based access control management to CakePHP 3.x

Inspired by [Laravel Permission](https://github.com/spatie/laravel-permission)

Quick example
-------------

[](#quick-example)

```
//Creats a role
$role = Role::create('editor');

//Givs a permission to the role
$role->givePermission('edit article');

//Adds the role to the user
$user->assignRole($role);
// You can also give it directly by its name
$user->assignRole('editor');

//Checks whether the user has the permission
var_dump($user->hasPermission('edit article')) //output "true"
```

Installation
------------

[](#installation)

1. Install via composer

```
composer require jeffersonsimaogoncalves/cakephp-permission
```

2. Load the plugin in `config/bootstrap.php`:

```
// Load the plugin.
Plugin::load('JeffersonSimaoGoncalves/CakePermission');
```

3. Add the following configuration to your `app.php`

```
'Permission' => [

    'tableNameMap' => [
        /**
         * Your users table, remember to modify it
         */
        'users' => 'your users table name',

        /**
         * Your roles table;If you want to use the default configuration. you don't need to change.
         */
        //'roles' => 'roles',

        /**
         * Your permissions table;If you want to use the default configuration. you don't need to change.
         */
        //'permissions' => 'permissions',

        /**
         * The join table between users and roles;If you want to use the default configuration. you don't need to change.
         */
        //'users_roles' => 'users_roles',

        /**
         * The join table between roles and permissions;If you want to use the default configuration. you don't need to change.
         */
        //'roles_permissions' => 'roles_permissions',
    ],

    'tableClassMap' => [
        /**
         * The Users model class, remember to modify it
         */
        'Users' => App\Model\Table\YourUsersTable::class,

        /**
         * The Roles model class;If you want to use the default configuration. you don't need to change.
         */
        //'Roles' => JeffersonSimaoGoncalves\CakePermission\Model\Table\RolesTable::class,

        /**
         * The Permissions model class;If you want to use the default configuration. you don't need to change.
         */
        //'Permissions' => JeffersonSimaoGoncalves\CakePermission\Model\Table\PermissionsTable::class
    ]
]
```

4. Generate the permission migration

```
./cake permission_migrate
```

If ok, now run the migrate command

```
./cake migrations migrate
```

Usage
-----

[](#usage)

### Models

[](#models)

Open your `User` entity, use `UserTrait` like this:

```
namespace App\Model\Entity;

use Cake\ORM\Entity;
use JeffersonSimaoGoncalves\CakePermission\Model\Entity\UserTrait;

class User extends Entity
{
    use UserTrait; //Use trait provied by CakePermission

    protected $_accessible = [
        '*' => true,
        'id' => false
    ];

    // ...
}
```

Open your `UsersTable`, use `UserTableTrait` like this:

```
namespace App\Model\Table;

use Cake\ORM\Table;
use JeffersonSimaoGoncalves\CakePermission\Model\Table\UsersTableTrait;

class UsersTable extends Table
{
    use UsersTableTrait;  // Use `UsersTableTrait`

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->buildPermissionRelationship(); // Creats the relationship
    }

    // ...
}
```

### Using permissions

[](#using-permissions)

Creates the permissions with `PermissionTrait::create` or `PermissionTrait::findOrCreate`

```
$addPermission = Permission::findOrCreate('add article');

$editPermission = Permission::create('edit article');
```

### Using roles and permissions

[](#using-roles-and-permissions)

Creates a role to the database with the `RoleTrait::create` or `RoleTrait::findOrCreate`

```
$role = Role::create('editor');

//You can also use the following method.
$role = Role::findOrCreate('editor');
```

Give the permission to the role; You must confirm that the permission exists.

```
$role->givePermission($addPermission);
$role->givePermission($editPermission);

//You can also directly give them by thier name
$role->givePermission('add article');
$role->givePermission('edit article');

//You can also give multiple permissions at once
$role->givePermission(['add article', 'edit article']);
```

Gets all permissions of the role

```
$role->getAllPermissions();
```

Checks whether the role has permission to do something:

```
$role->hasPermission('edit article'); //true

$role->hasPermission(['edit artic;e', 'add article']); //true

$role->hasPermission(['edit article', 'drop article']); // false

$role->hasAnyPermission('edit article', 'drop article'); // true
```

Removes the permission

```
$role->revokePermission($addPermission);

//Or by its name
$role->revokePermission('add article');

//Revokes all permissions
$role->revokeAllPermissions();
```

### User's roles and permissions

[](#users-roles-and-permissions)

Add the role to the user:

```
$user->assignRole($role);

$user->assignRole('editor');

//You can also assign multiple roles at once
$user->assignRole(['editor', 'other role']);
```

Gets all the roles of user

```
$user->getAllRoles();
```

Gets all permissions of user:

```
$user->getAllPermissions();
```

Checks whether the user has permission to do something:

```
$user->hasPermission('edit article'); //true

$user->hasPermission(['edit artic;e', 'add article']); //true

$user->hasPermission(['edit article', 'drop article']); // false

$user->hasAnyPermission('edit article', 'drop article'); // true
```

Removes the role of the user:

```
$user->removeRole('editor');

//Or removes all roles of the user
$user->removeAllRoles();
```

Extending
---------

[](#extending)

You can extends all existing Entity or Table. Do not forget to modify the default configuration in your `app.php`

Requirements
------------

[](#requirements)

- CakePHP &gt;=3.6
- PHP 7.0+

LICENSE
-------

[](#license)

The MIT license. See [MIT](https://opensource.org/licenses/MIT)

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance66

Regular maintenance activity

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

205d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/411493?v=4)[Jefferson Gonçalves](/maintainers/jeffersongoncalves)[@jeffersongoncalves](https://github.com/jeffersongoncalves)

---

Top Contributors

[![jeffersongoncalves](https://avatars.githubusercontent.com/u/411493?v=4)](https://github.com/jeffersongoncalves "jeffersongoncalves (1 commits)")

---

Tags

securitycakephppermissionrbacmanage usersmanage permissionmanage rolesrole based

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jeffersonsimaogoncalves-cakephp-permission/health.svg)

```
[![Health](https://phpackages.com/badges/jeffersonsimaogoncalves-cakephp-permission/health.svg)](https://phpackages.com/packages/jeffersonsimaogoncalves-cakephp-permission)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[backpack/permissionmanager

Users and permissions management interface for Laravel 5 using Backpack CRUD.

5591.8M16](/packages/backpack-permissionmanager)[efficiently/authority-controller

AuthorityController is an PHP authorization library for Laravel 5 which restricts what resources a given user is allowed to access.

15533.2k](/packages/efficiently-authority-controller)[hosseinhezami/laravel-permission-manager

Advanced permission manager for Laravel.

403.3k](/packages/hosseinhezami-laravel-permission-manager)[wnikk/laravel-access-rules

Simple system of ACR (access control rules) for Laravel, with roles, groups, unlimited inheritance and possibility of multiplayer use.

103.6k1](/packages/wnikk-laravel-access-rules)

PHPackages © 2026

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