<?php
declare(strict_types=1);
namespace App\Bundles\ReportBundle\Security;
use App\Bundles\ReportBundle\Entity\ReportTemplate;
use App\Bundles\UserBundle\Entity\User;
use App\Bundles\UserBundle\Enum\SystemPermissionEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class ReportTemplateShareVoter extends Voter
{
public function __construct(private readonly Security $security)
{
}
protected function supports(string $attribute, $subject): bool
{
return $attribute === SystemPermissionEnum::SINGLE_REPORT_TEMPLATE_SHARE->value;
}
/** @param ReportTemplate $subject */
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $this->security->getUser();
if (!$user instanceof User) {
return false;
}
if ($subject->getUser() === $user) {
return true;
}
return false;
}
}