1. Go to this page and download the library: Download slince/cakephp-permission library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
slince / cakephp-permission example snippets
//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"
// Load the plugin.
Plugin::load('Slince/CakePermission');
'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' => Slince\CakePermission\Model\Table\RolesTable::class,
/**
* The Permissions model class;If you want to use the default configuration. you don't need to change.
*/
//'Permissions' => Slince\CakePermission\Model\Table\PermissionsTable::class
]
]
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Slince\CakePermission\Model\Entity\UserTrait;
class User extends Entity
{
use UserTrait; //Use trait provied by CakePermission
protected $_accessible = [
'*' => true,
'id' => false
];
// ...
}
namespace App\Model\Table;
use Cake\ORM\Table;
use Slince\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
}
// ...
}
$role = Role::create('editor');
//You can also use the following method.
$role = Role::findOrCreate('editor');
$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']);
$role->revokePermission($addPermission);
//Or by its name
$role->revokePermission('add article');
//Revokes all permissions
$role->revokeAllPermissions();
$user->assignRole($role);
$user->assignRole('editor');
//You can also assign multiple roles at once
$user->assignRole(['editor', 'other role']);