Access control lists for Ubiquity framework
AclManager::start();
AclManager::addRole('@USER');
AclManager::addResource('Home');
AclManager::addPermission('READ',1);
AclManager::allow('@USER','Home','READ');
AclManager::start();
AclManager::addAndAllow('@USER','Home','READ');
use Ubiquity\security\acl\AclManager;
use Ubiquity\security\acl\persistence\AclCacheProvider;
AclManager::start();
AclManager::initFromProviders([
new AclCacheProvider()
]);
With annotations:
namespace controllers;
/**
* @resource('Main')
* @allow('role'=>'@USER')
*/
class TestAclController extends ControllerBase {
use AclControllerTrait;
}
With attributes:
namespace controllers;
use Ubiquity\attributes\items\acl\Resource;
use Ubiquity\attributes\items\acl\Allow;
#[Resource('Main')]
#[Allow(role: '@USER')]
class TestAclController extends ControllerBase {
use AclControllerTrait;
}
It is necessary to override the _getRole method so that it returns the role of the active user:
namespace controllers;
use Ubiquity\attributes\items\acl\Resource;
use Ubiquity\attributes\items\acl\Allow;use Ubiquity\utils\http\USession;
use Ubiquity\utils\http\USession;
#[Resource('Main')]
#[Allow(role: '@USER')]
class TestAclController extends ControllerBase {
use AclControllerTrait;
public function _getRole(){
$activeUser=USession::get('activeUser');
if(isset($activeUser)){
return $activeUser->getRole();
}
}
}
The ACLs defined in the database are additional to the ACLs defined via annotations or attributes.
The initialization allows to create the tables associated to the ACLs (Role
, Resource
, Permission
, AclElement
).
It needs to be done only once, and in dev mode only.
use Ubiquity\controllers\Startup;
use Ubiquity\security\acl\AclManager;
$config=Startup::$config;
AclManager::initializeDAOProvider($config, 'default');
In app/config/services.php
file :
use Ubiquity\security\acl\AclManager;
use Ubiquity\security\acl\persistence\AclCacheProvider;
use Ubiquity\security\acl\persistence\AclDAOProvider;
use Ubiquity\orm\DAO;
DAO::start();//Optional, to use only if dbOffset is not default
AclManager::start();
AclManager::initFromProviders([
new AclCacheProvider(), new AclDAOProvider($config)
]);