src/Security/Voter/SaleVoter.php line 13
<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Sale\Upwork\Rss;
use App\Entity\User\User;
use App\Enum\User\UserRoleEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SaleVoter extends Voter
{
const EDIT = 'EDIT';
const CHANGE_STATUS = 'CHANGE_STATUS';
protected function supports(string $attribute, mixed $subject): bool
{
if (!\in_array($attribute, [self::CHANGE_STATUS, self::EDIT])) {
return false;
}
if (!$subject instanceof Rss) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return match ($attribute) {
self::EDIT => $this->hasAccessSale($user),
self::CHANGE_STATUS => $this->hasAccessSale($user),
default => throw new \LogicException('This code should not be reached!'),
};
}
private function hasAccessSale(User $user): bool
{
if (\in_array(UserRoleEnum::ROLE_SALE->value, $user->getRoles())) {
return true;
}
return false;
}
}