Files
chill-bundles/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelper.php
2021-12-21 10:59:23 +01:00

69 lines
1.9 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
use function in_array;
final class DefaultVoterHelper implements VoterHelperInterface
{
private AuthorizationHelper $authorizationHelper;
private CenterResolverDispatcherInterface $centerResolverDispatcher;
private array $configuration = [];
public function __construct(
AuthorizationHelper $authorizationHelper,
CenterResolverDispatcherInterface $centerResolverDispatcher,
array $configuration
) {
$this->authorizationHelper = $authorizationHelper;
$this->centerResolverDispatcher = $centerResolverDispatcher;
$this->configuration = $configuration;
}
public function supports($attribute, $subject): bool
{
foreach ($this->configuration as [$attributes, $subj]) {
if (null === $subj) {
if (null === $subject && in_array($attribute, $attributes, true)) {
return true;
}
} elseif ($subject instanceof $subj) {
return in_array($attribute, $attributes, true);
}
}
return false;
}
public function voteOnAttribute($attribute, $subject, $token): bool
{
if (!$token->getUser() instanceof User) {
return false;
}
if (null === $subject) {
return [] !== $this->authorizationHelper->getReachableCenters($token->getUser(), $attribute, null);
}
return $this->authorizationHelper->userHasAccess(
$token->getUser(),
$subject,
$attribute
);
}
}