<?php
declare(strict_types=1);
namespace App\Bundles\ReportBundle\Security;
use App\Bundles\OrganizationBundle\Exception\UserOrganizationNotFoundException;
use App\Bundles\OrganizationBundle\Service\UserOrganization\UserOrganizationProvider;
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 ReportTemplateViewVoter extends Voter
{
public function __construct(
private readonly Security $security,
private readonly UserOrganizationProvider $userOrganizationProvider,
) {
}
protected function supports(string $attribute, $subject): bool
{
return $attribute === SystemPermissionEnum::SINGLE_REPORT_TEMPLATE_VIEW->value;
}
/** @param ReportTemplate $subject
* @throws UserOrganizationNotFoundException
*/
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;
}
$organization = $this->userOrganizationProvider->provideFromSession()->getOrganization();
if ($organization === $subject->getOrganization() and $subject->isShared()) {
return true;
}
return false;
}
}