<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Security;
use App\Entity\User;
class AccesoAlmacenVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['VER'])
&& ($subject !== NULL);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
//** $user User */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
switch ($attribute) {
case 'VER':
if ($this->security->isGranted('ROLE_ADMIN')){return TRUE;};
if ($user->getAlmacen() == $subject){return TRUE;};
}
return false;
}
}