<?php
namespace App\Security\Voter;
use App\Entity\Company\User;
use App\Entity\ConstructionSite\ConstructionSite;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ConstructionVoter extends Voter
{
public const EDIT = 'EDIT';
public const VIEW = 'VIEW';
public const DELETE = 'DELETE';
public const CONTINUE = 'CONTINUE';
public const SEND_MAIL = 'SEND_MAIL';
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof ConstructionSite;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::EDIT => $this->canPerformSimpleAction($subject, $user),
self::VIEW => $this->canPerformSimpleAction($subject, $user),
self::DELETE => $this->canPerformSimpleAction($subject, $user),
self::CONTINUE => $this->canContinue($subject, $user),
self::SEND_MAIL => $this->canSendMail($subject, $user)
};
}
/**
*
* @param ConstructionSite $construction
* @param User $user
* @return boolean
*/
private function canPerformSimpleAction(ConstructionSite $construction, User $user): bool
{
return $construction->getCustomerCompany()->getId() == $user->getCustomerCompany()->getId();
}
/**
*
* @param ConstructionSite $construction
* @param User $user
* @return boolean
*/
private function canContinue(ConstructionSite $construction, User $user): bool
{
return $this->canPerformSimpleAction($construction, $user) && $construction->isStatus() == 0;
}
/**
*
* @param ConstructionSite $quote
* @param User $user
* @return boolean
*/
private function canSendMail(ConstructionSite $construction, User $user): bool
{
return $this->canPerformSimpleAction($construction,$user) && $construction->isStatus() == 1;
}
}