<?php
namespace App\Security\Voter\Garages;
use App\Entity\Garages\Garage;
use App\Entity\User;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
final class GarageVoter extends Voter
{
private Security $security;
private array $voters;
public function __construct(Security $security)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_GARAGE,
VotersEnum::CREATE_GARAGE,
VotersEnum::READ,
VotersEnum::UPDATE,
VotersEnum::DELETE,
VotersEnum::EXPORT_GARAGE,
VotersEnum::PURCHASE_TRACKING_READ,
VotersEnum::UPDATE_GARAGE_OWNER,
];
}
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 Garage) {
// 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;
}
switch ($attribute) {
case VotersEnum::LIST_GARAGE:
return $this->canList();
case VotersEnum::CREATE_GARAGE:
return $this->canCreate();
case VotersEnum::READ:
return $this->canRead($subject, $user);
case VotersEnum::UPDATE:
return $this->canUpdate($subject, $user);
case VotersEnum::UPDATE_GARAGE_OWNER:
return $this->canUpdateGarageOwner($subject, $user);
case VotersEnum::DELETE:
return $this->canDelete();
case VotersEnum::EXPORT_GARAGE:
return $this->canExport();
case VotersEnum::PURCHASE_TRACKING_READ:
return $this->canReadPurchaseTracking($subject, $user);
}
throw new \LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser() || $this->isQualityAdvisorUser() || $this->isAssociatedManagerUser();
}
private function canCreate(): bool
{
return $this->isAdminUser();
}
private function canRead(Garage $garage, User $user): bool
{
if ($this->isAdminUser() || $this->isAssociatedManagerUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) {
return true;
}
return false;
}
private function canUpdate(Garage $garage, User $user): bool
{
if (($this->isAdminUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) && !$this->isQualityAdvisorUser() && !$this->isAssociatedManagerUser()) {
return true;
}
return false;
}
private function canUpdateGarageOwner(Garage $garage, User $user): bool
{
if (($this->isAdminUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) && !$this->isAssociatedManagerUser()) {
return true;
}
return false;
}
private function canDelete(): bool
{
return $this->isAdminUser();
}
private function canExport(): bool
{
return $this->isAdminUser();
}
private function canReadPurchaseTracking(Garage $garage, User $user): bool
{
if (($this->isAdminUser() || $garage->isOwner($user) || $garage->isCoordinator($user)) || $this->isAssociatedManagerUser()) {
return true;
}
return false;
}
private function isAdminUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG);
}
private function isAssociatedManagerUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_ASSOCIATED_MANAGER_LONG);
}
private function isQualityAdvisorUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_QUALITY_ADVISOR_LONG);
}
}