https://github.com/phpmv/ubiquity-acl
Access control lists for Ubiquity framework
https://github.com/phpmv/ubiquity-acl
php security ubiquity-framework
Last synced: 11 months ago
JSON representation
Access control lists for Ubiquity framework
- Host: GitHub
- URL: https://github.com/phpmv/ubiquity-acl
- Owner: phpMv
- License: apache-2.0
- Created: 2020-10-03T12:26:21.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-04T00:41:33.000Z (about 3 years ago)
- Last Synced: 2025-02-18T12:32:15.953Z (12 months ago)
- Topics: php, security, ubiquity-framework
- Language: PHP
- Homepage: https://ubiquity.kobject.net
- Size: 237 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ubiquity-acl
[](https://scrutinizer-ci.com/g/phpMv/ubiquity-acl/?branch=main)
[](https://scrutinizer-ci.com/g/phpMv/ubiquity-acl/build-status/main)
[](https://scrutinizer-ci.com/code-intelligence)
[](https://scrutinizer-ci.com/g/phpMv/ubiquity-acl/?branch=main)
Access control lists for Ubiquity framework
## Samples
### Defining ACLs at runtime
#### One by one
```php
AclManager::start();
AclManager::addRole('@USER');
AclManager::addResource('Home');
AclManager::addPermission('READ',1);
AclManager::allow('@USER','Home','READ');
```
#### By grouping
```php
AclManager::start();
AclManager::addAndAllow('@USER','Home','READ');
```
### Defining ACLs with annotations or attributes
#### Starting
```php
use Ubiquity\security\acl\AclManager;
use Ubiquity\security\acl\persistence\AclCacheProvider;
AclManager::start();
AclManager::initFromProviders([
new AclCacheProvider()
]);
```
#### Defining ACLs in controllers
##### A controller as a resource, authorized for a role
With annotations:
```php
namespace controllers;
/**
* @resource('Main')
* @allow('role'=>'@USER')
*/
class TestAclController extends ControllerBase {
use AclControllerTrait;
}
```
With attributes:
```php
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;
}
```
#### Overriding
It is necessary to override the _getRole method so that it returns the role of the active user:
```php
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();
}
}
}
```
### Defining ACLs with Database
The ACLs defined in the database are additional to the ACLs defined via annotations or attributes.
#### Initializing
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.
```php
use Ubiquity\controllers\Startup;
use Ubiquity\security\acl\AclManager;
$config=Startup::$config;
AclManager::initializeDAOProvider($config, 'default');
```
#### Starting
In `app/config/services.php` file :
```php
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)
]);
```