PHP code example of mgomezbuceta / cakephp-aclmanager
1. Go to this page and download the library: Download mgomezbuceta/cakephp-aclmanager 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/ */
mgomezbuceta / cakephp-aclmanager example snippets
public function bootstrap(): void
{
parent::bootstrap();
$this->addPlugin('AclManager', ['bootstrap' => true, 'routes' => true]);
}
// In your AppController or specific controller
public function initialize(): void
{
parent::initialize();
$this->loadComponent('AclManager.AuthorizationManager', [
'userModel' => 'Users',
'roleField' => 'role_id'
]);
}
public function isAuthorized($user = null): bool
{
return $this->AuthorizationManager->isAuthorized($user);
}
// In config/app.php, add to the return array:
return [
// ... existing configuration ...
'AclManager' => [
'adminAccess' => [
// Which role IDs can access the Authorization Manager
'adminRoleIds' => [1, 2], // Allow role IDs 1 and 2
// Which role names can access
'adminRoleNames' => ['admin', 'superuser'],
// Specific emails (useful for initial setup)
'adminEmails' => [
'[email protected]',
],
],
'redirects' => [
'login' => ['controller' => 'Users', 'action' => 'login'],
'unauthorized' => ['controller' => 'Dashboard', 'action' => 'index'],
],
],
];
// In your UsersController login action, after successful authentication:
public function login()
{
$result = $this->Authentication->getResult();
if ($result->isValid()) {
// Check if there's a redirect parameter
$redirect = $this->request->getQuery('redirect');
if ($redirect) {
// Redirect back to the original URL
return $this->redirect($redirect);
}
// Default redirect
$target = $this->Authentication->getLoginRedirect() ?? '/dashboard';
return $this->redirect($target);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
use Cake\I18n\I18n;
// Set Spanish (default)
I18n::setLocale('es_ES');
// Or Galician
I18n::setLocale('gl_ES');
// Or English
I18n::setLocale('en_US');
use Cake\Core\Configure;
// Actions to ignore during resource scan
Configure::write('AclManager.ignoreActions', [
'isAuthorized',
'beforeFilter',
'initialize',
'AclManager.*', // Ignore plugin
'DebugKit.*' // Ignore DebugKit
]);
// Permission checking mode
Configure::write('AclManager.permissionMode', 'strict'); // or 'permissive'
// Enable permission caching
Configure::write('AclManager.cachePermissions', true);
Configure::write('AclManager.cacheDuration', '+1 hour');
// Default role for new users
Configure::write('AclManager.defaultRoleId', 2);
// Load the component
$this->loadComponent('AclManager.AuthorizationManager');
// Check if user is authorized
$allowed = $this->AuthorizationManager->isAuthorized($user);
// Check specific permission
$allowed = $this->AuthorizationManager->checkPermission(
$roleId,
'Articles',
'edit',
'Blog' // plugin name (optional)
);
// Clear permission cache
$this->AuthorizationManager->clearCache();
// Handle unauthorized access
return $this->AuthorizationManager->handleUnauthorized();
use AclManager\Service\PermissionService;
use AclManager\Service\ResourceScannerService;
// Permission management
$permissionService = new PermissionService();
// Grant permission
$permissionService->grant($roleId, 'Articles', 'edit');
// Deny permission
$permissionService->deny($roleId, 'Articles', 'delete');
// Get permission matrix
$matrix = $permissionService->getPermissionMatrix($roleId);
// Copy permissions between roles
$permissionService->copyPermissions($sourceRoleId, $targetRoleId);
// Resource scanning
$scannerService = new ResourceScannerService();
$stats = $scannerService->scanAndSync();
// Get grouped resources
$resources = $scannerService->getGroupedResources();
// Clear cache
Configure::write('AclManager.cachePermissions', false);
// Or clear specific cache
$this->AuthorizationManager->clearCache();
// Old
$this->loadComponent('AclManager.AclManager');
// New
$this->loadComponent('AclManager.AuthorizationManager');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.