<?php
namespace App\Security\Voter;
use App\Entity\Utilisateur\Utilisateur;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AppVoter extends Voter
{
public const SUPER_ADMIN_TYPE = 1;
public const ADMIN_TYPE = 2;
public const SALESMAN_TYPE = 3;
public const PICKER_TYPE = 4;
protected function supports(string $attribute, $subject): bool
{
return (empty($subject));
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/** @var ?Utilisateur $user */
$user = $token->getUser();
if ($user === null) {
return self::ACCESS_ABSTAIN;
}
if($user->getType()->getId() === self::SUPER_ADMIN_TYPE){
return self::ACCESS_GRANTED;
}
$attributes = [$attribute];
foreach ($attributes as $attribute) {
if (is_numeric($attribute)) {
if ($attribute == $user->getType()->getId()) {
return self::ACCESS_GRANTED;
} else {
return self::ACCESS_DENIED;
}
}
if ($attribute === $user->getType()->getLibelle()) {
return self::ACCESS_GRANTED;
}
}
return self::ACCESS_ABSTAIN;
}
/*
public function vote(TokenInterface $token, $subject, array $attributes): int
{
}
*/
}