/var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/authorization/src
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/authorization/src/AuthorizationService.php (5390B)
resolver = $resolver;
}
/**
* @inheritDoc
*/
public function can(?IdentityInterface $user, string $action, $resource): bool
{
$result = $this->performCheck($user, $action, $resource);
return is_bool($result) ? $result : $result->getStatus();
}
/**
* @inheritDoc
*/
public function canResult(?IdentityInterface $user, string $action, $resource): ResultInterface
{
$result = $this->performCheck($user, $action, $resource);
return is_bool($result) ? new Result($result) : $result;
}
/**
* Check whether the provided user can perform an action on a resource.
*
* @param \Authorization\IdentityInterface|null $user The user to check permissions for.
* @param string $action The action/operation being performed.
* @param mixed $resource The resource being operated on.
* @return bool|\Authorization\Policy\ResultInterface
*/
protected function performCheck(?IdentityInterface $user, string $action, $resource)
{
$this->authorizationChecked = true;
$policy = $this->resolver->getPolicy($resource);
if ($policy instanceof BeforePolicyInterface) {
$result = $policy->before($user, $resource, $action);
if ($result !== null) {
return $this->resultTypeCheck($result);
}
}
$handler = $this->getCanHandler($policy, $action);
$result = $handler($user, $resource);
return $this->resultTypeCheck($result);
}
/**
* Check result type.
*
* @param mixed $result Result from policy class instance.
* @return bool|\Authorization\Policy\ResultInterface
* @throws \Authorization\Exception\Exception If $result argument is not a boolean or ResultInterface instance.
*/
protected function resultTypeCheck($result)
{
if (is_bool($result) || $result instanceof ResultInterface) {
return $result;
}
throw new Exception(sprintf(
'Pre-authorization check must return `%s`, `bool` or `null`.',
ResultInterface::class
));
}
/**
* @inheritDoc
*/
public function applyScope(?IdentityInterface $user, string $action, $resource)
{
$this->authorizationChecked = true;
$policy = $this->resolver->getPolicy($resource);
$handler = $this->getScopeHandler($policy, $action);
return $handler($user, $resource);
}
/**
* Returns a policy action handler.
*
* @param mixed $policy Policy object.
* @param string $action Action name.
* @return callable
* @throws \Authorization\Policy\Exception\MissingMethodException
*/
protected function getCanHandler($policy, $action): callable
{
$method = 'can' . ucfirst($action);
if (!method_exists($policy, $method) && !method_exists($policy, '__call')) {
throw new MissingMethodException([$method, $action, get_class($policy)]);
}
return [$policy, $method];
}
/**
* Returns a policy scope action handler.
*
* @param mixed $policy Policy object.
* @param string $action Action name.
* @return callable
* @throws \Authorization\Policy\Exception\MissingMethodException
*/
protected function getScopeHandler($policy, $action): callable
{
$method = 'scope' . ucfirst($action);
if (!method_exists($policy, $method)) {
throw new MissingMethodException([$method, $action, get_class($policy)]);
}
return [$policy, $method];
}
/**
* @inheritDoc
*/
public function authorizationChecked(): bool
{
return $this->authorizationChecked;
}
/**
* @inheritDoc
*/
public function skipAuthorization()
{
$this->authorizationChecked = true;
return $this;
}
}