1. Go to this page and download the library: Download typo3/access-control 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/ */
typo3 / access-control example snippets
namespace App\Security\AccessControl\Attribute;
use TYPO3\AccessControl\Attribute\PrincipalAttribute;
class RoleAttribute extends PrincipalAttribute
{
public function __construct(string $identifier)
{
parent::__construct($identifier);
}
}
namespace App\Security\AccessControl\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use TYPO3\AccessControl\Expression\ResolverInterface;
class ExpressionLanguageResolver implements ResolverInterface
{
private $expressionLanguage;
public function __construct()
{
$this->expressionLanguage = new ExpressionLanguage();
// register a custom function `hasAuthority`
$this->expressionLanguage->register(
'hasAuthority', function () {
// not implemented, we only use the evaluator
},
function ($variables, ...$arguments) {
if (count($arguments) == 1) {
// checks if the subject has the given principal
return isset($variables['subject']->principals[$arguments[0]]);
}
return false;
}
);
}
public function validate(string $expression): void
{
// only allow the attributes `subject`, `resource` and `action`
$this->expressionLanguage->parse($expression, ['subject', 'resource', 'action']);
}
public function evaluate(string $expression, array $attributes): bool
{
return $this->expressionLanguage->evaluate($expression, $attributes);
}
}
use App\Security\AccessControl\Expression\ExpressionLanguageResolver;
use Symfony\Component\Yaml\Parser;
use TYPO3\AccessControl\Policy\PolicyFactory;
$resolver = new ExpressionLanguageResolver();
$factory = new PolicyFactory();
$parser = new Parser();
$policy = $factory->build(
$parser->parseFile('/path/to/policies.yaml'),
$resolver
);
use App\Security\AccessControl\Attribute\ActionAttribute;
use App\Security\AccessControl\Attribute\ResourceAttribute;
use Symfony\Component\EventDispatcher\EventDispatcher;
use TYPO3\AccessControl\Policy\PolicyDecisionPoint;
use TYPO3\AccessControl\Policy\PolicyInformationPoint;
$dispatcher = new EventDispatcher();
// creeates an policy information point
$policyInformationPoint = new PolicyInformationPoint(
$dispatcher
);
// creates a policy decision point
$policyDecisionPoint = new PolicyDecisionPoint(
$dispatcher,
$policy,
$policyInformationPoint
);
// perform an authorization request
$policyDecision = $policyDecisionPoint->authorize(
[
// concrete resource to access
'resource' => new ResourceAttribute('identifier'),
// concrete action on resource
'action' => new ActionAttribute()
]
);
if (!$policyDecision->isApplicable()) {
// access request is not applicable
}
// process determining policy rule
$determinigRule = $policyDecision->getRule();
foreach ($policyDecision->getObligations() as $obligation) {
// process obligations
}
if ($policyDecision->getValue() === PolicyDecision::PERMIT)
// access is granted
}
// access is denied otherwise
namespace App\Security\AccessControl\EventListener;
use TYPO3\AccessControl\Event\PolicyDecisionEvent;
class PolicyDecisionListener
{
public function __invoke(PolicyDecisionEvent $event)
{
// ...
}
}
namespace App\Security\AccessControl\EventListener;
use TYPO3\AccessControl\Event\AttributeRetrievalEvent;
class AttributeRetrievalListener
{
public function __invoke(AttributeRetrievalEvent $event)
{
// ...
}
}
namespace App\Security\AccessControl\EventListener;
use TYPO3\AccessControl\Attribute\PrincipalAttribute;
use TYPO3\AccessControl\Event\SubjectRetrievalEvent;
class SubjectRetrievalListener
{
public function __invoke(SubjectRetrievalEvent $event)
{
// Adds administrator principal
$event->addPrincipal(new PrincipalAttribute('administrator'));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.