1. Go to this page and download the library: Download damejidlo/permissions 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/ */
damejidlo / permissions example snippets
$callback = function (IUser $user, $queriedRole, $queriedResource) {
return $user->getEntity()->getId() === $queriedResource->getEntity()->getCreatorId();
};
// god can destroy world, but only the one he created
$authorizator->allow('god', 'world', 'destroy', $callback);
class AccessList extends Authorizator
{
/**
* @param string[][] $roles
*/
public function addRoles(array $roles)
{
foreach ($roles as $role => $parentRoles) {
$this->addRole($role, $parentRoles);
}
}
/**
* @param @param string[] $resources
*/
public function addResources(array $resources)
{
foreach ($resources as $resource) {
$this->addResource($resource);
}
}
/**
* @param string[][][] $directives
*/
public function addDirectives(array $directives)
{
foreach ($directives as $resource => $resourceDirectives) {
foreach ($resourceDirectives as $privilege => $privilegeDirectives) {
foreach ($privilegeDirectives as $roleIdentifier => $directiveType) {
$this->createDirective($directiveType, $roleIdentifier, $resource, $privilege);
}
}
}
}
public function someStuff()
{
$callback = function (IUser $user, $queriedRole, $queriedResource) {
return $user->getEntity()->getId() === $queriedResource->getEntity()->getCreatorId();
};
// god can destroy world, but only the one he created
$authorizator->allow('god', 'world', 'destroy', $callback);
}
}
class MyLoggedUser extends \Nette\Security\User
{
/**
* @param IUserStorage $storage
* @param IAuthenticator|NULL $authenticator
*/
public function __construct(IUserStorage $storage, IAuthenticator $authenticator = NULL)
{
parent::__construct($storage, $authenticator); // No IAuthorizator here !!!
}
/**
* @inheritdoc
*/
public function isAllowed($resource = IAuthorizator::ALL, $privilege = IAuthorizator::ALL)
{
throw new LogicException('Use Damejidlo\ACL\Authorizator directly. User shouldn\'t have such a responsibility');
}
/**
* @inheritdoc
*/
public function isInRole($role)
{
throw new LogicException('Use Damejidlo\ACL\Authorizator directly. User shouldn\'t have such a responsibility');
}
/**
* @return AclUser
*/
public function getAclUser()
{
$entity = $this->getEntity(); // depens on your implementation
return new AclUser($entity, $this->getRoles());
}
}
// In some Presenter
public function handleDestroy($worldId)
{
$world = $this->worldFinder->findWorld($worldId);
$resource = new WorldResource($world);
$permission = 'destroy';
if (!$this->authorizator->isAllowed($this->user->getAclUser(), $resource, $permission) {
throw new NotAllowedException($resource, $permission);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.