<?php
namespace App\Security\Voter\Promotions;
use App\Entity\Promotions\LocalPromotion;
use App\Entity\User;
use App\Enum\MenuRolesAssociatedEnum;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class LocalPromotionVoter extends Voter
{
private Security $security;
private array $voters;
public function __construct(Security $security)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_LOCAL_PROMOTION,
VotersEnum::LIST_LOCAL_PROMOTION_ASSOCIATED,
VotersEnum::LIST_LOCAL_PROMOTION_COORDINATOR,
VotersEnum::CREATE_LOCAL_PROMOTION,
VotersEnum::READ,
VotersEnum::UPDATE,
VotersEnum::DELETE,
];
}
protected function supports(string $attribute, $subject): bool
{
// first check the $subject and last if the $attribute is supported,
// because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
if ($subject && !$subject instanceof LocalPromotion) {
// only vote on these objects
return false;
}
if (in_array($attribute, $this->voters)) {
// if the attribute is one we support
return true;
}
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// you know $subject is a LocalPromotion object, thanks to `supports()`
/** @var LocalPromotion $localPromotion */
$localPromotion = $subject;
switch ($attribute) {
case VotersEnum::LIST_LOCAL_PROMOTION:
return $this->canList();
case VotersEnum::LIST_LOCAL_PROMOTION_ASSOCIATED:
return $this->canListAssociated();
case VotersEnum::LIST_LOCAL_PROMOTION_COORDINATOR:
return $this->canListCoordinator();
case VotersEnum::CREATE_LOCAL_PROMOTION:
return $this->canCreate();
case VotersEnum::READ:
return $this->canRead($localPromotion, $user);
case VotersEnum::UPDATE:
return $this->canUpdate($localPromotion, $user);
case VotersEnum::DELETE:
return $this->canDelete($localPromotion, $user);
}
throw new LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser();
}
private function canListAssociated(): bool
{
return $this->isAdminUser()
|| $this->isAssociatedUser()
;
}
private function canListCoordinator(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
;
}
private function canCreate(): bool
{
if ($this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedUser()) {
return true;
}
return false;
}
private function canRead(LocalPromotion $localPromotion, User $user): bool
{
return $this->isOwner($localPromotion, $user);
}
private function canUpdate(LocalPromotion $localPromotion, User $user): bool
{
return $this->isOwner($localPromotion, $user);
}
private function canDelete(LocalPromotion $localPromotion, User $user): bool
{
return $this->isOwner($localPromotion, $user);
}
private function isOwner(LocalPromotion $localPromotion, User $user): bool
{
if ($this->isAdminUser()) {
return true;
} elseif ($this->isCoordinatorUser()) {
foreach ($user->getWorkProvinces() as $province) {
if ($localPromotion->getGarage()->getAddress()->getProvince()->getId() === $province->getId()) {
return true;
}
}
} elseif ($this->isAssociatedUser()) {
if ($localPromotion->getGarage()->getOwner()->getId() === $user->getId()) {
return true;
}
}
return false;
}
private function isAssociatedUser(): bool
{
return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_LOCAL_PROMOTIONS_ASSOCIATED);
}
private function isCoordinatorUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG);
}
private function isAdminUser(): bool
{
return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_LOCAL_PROMOTIONS);
}
}