src/Security/Voter/Customer/DownloadVoter.php line 12
<?php
namespace App\Security\Voter\Customer;
use App\Entity\Customer\File;
use App\Entity\Admin\CustomerUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bundle\SecurityBundle\Security;
class DownloadVoter extends Voter
{
final public const VIEW = 'DOWNLOAD_ENTITY_VIEW';
public function __construct(
private readonly Security $security)
{
}
protected function supports(string $attribute, mixed $subject): bool
{
if(!in_array($attribute, [self::VIEW])){
return false;
}
return $subject instanceof File;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if (!$this->security->isGranted('ROLE_STAFF')){
return false;
}
return match($attribute) {
self::VIEW => $this->canView($subject, $user),
default => throw new \LogicException('This code should not be reached!')
};
}
private function canView(File $entity, CustomerUser $user): bool
{
if ($entity->getRights() == $user->getStaff() or $entity->getType() == 1) {
return true;
}
return false;
}
}