<?php declare(strict_types=1);
namespace App\StartPlatz\Bundle\ToolBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SystemAccessVoter extends Voter
{
public const TOOL_ACCESS = 'TOOL_ACCESS';
public const PERSONNEL_ACCESS = 'PERSONNEL_ACCESS';
public const PERSONNEL_HOLDING = 'PERSONNEL_HOLDING';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::TOOL_ACCESS, self::PERSONNEL_ACCESS, self::PERSONNEL_HOLDING], true) && $subject === null;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user || !method_exists($user, 'getMemberId')) {
return false;
}
$memberId = $user->getMemberId();
if ($memberId === null) {
return false;
}
if ($attribute === self::PERSONNEL_HOLDING) {
return SystemAccessProvider::isPersonnelHolding($memberId);
}
if ($attribute === self::PERSONNEL_ACCESS) {
return SystemAccessProvider::isPersonnelAuthorized($memberId);
}
return SystemAccessProvider::isAuthorized($memberId);
}
}