<?php
declare(strict_types=1);
namespace App\Bundles\ReportBundle\Security;
use App\Bundles\LocationBundle\Enum\LocationTypeEnum;
use App\Bundles\OrganizationBundle\Enum\UserOrganizationResponsibleLevelEnum;
use App\Bundles\OrganizationBundle\Exception\UserOrganizationNotFoundException;
use App\Bundles\OrganizationBundle\Service\UserOrganization\UserOrganizationProvider;
use App\Bundles\OrganizationBundle\Service\UserOrganization\UserOrganizationResponsibleLevelResolver;
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 $organizationProvider,
private readonly UserOrganizationProvider $userOrganizationProvider,
private readonly UserOrganizationResponsibleLevelResolver $levelResolver,
) {
}
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;
}
if (!$subject->isSent()) {
return false;
}
$userLevel = $this->levelResolver->resolve(
$this->organizationProvider->provideFromSession()
);
$location = $subject->getLocation();
if (in_array($location->getType(), LocationTypeEnum::getRegionalTypes())) {
if ($userLevel === UserOrganizationResponsibleLevelEnum::NATIONAL_LEVEL->value) {
return true;
}
}
if (in_array($location->getType(), LocationTypeEnum::getDistrictTypes())) {
if ($userLevel === UserOrganizationResponsibleLevelEnum::REGIONAL_LEVEL->value) {
return true;
}
}
return false;
}
}