<?php
namespace App\Security;
use App\Entity\OperationsCommerciales;
use App\Entity\Utilisateurs;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class OperationVoter extends Voter
{
// these strings are just invented: you can use anything
const VIEW = 'view';
protected function supports(string $attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::VIEW])) {
return false;
}
// only vote on `Post` objects
if (!$subject instanceof OperationsCommerciales) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Utilisateurs) {
// the user must be logged in; if not, deny access
return false;
}
/** @var OperationsCommerciales $operationCommerciale */
$operationCommerciale = $subject;
switch ($attribute) {
case self::VIEW:
return $this->canView($operationCommerciale, $user);
}
throw new \LogicException('This code should not be reached!');
}
private function canView(OperationsCommerciales $operationCommerciale, Utilisateurs $user): bool
{
return $operationCommerciale->getMagasinsAutorises()->contains($user->getIdSite()) or $operationCommerciale->getTypesMagasins()->contains($user->getIdSite()->getIdTypeMagasin());
}
}