<?php
namespace App\Security\Voter\Almacen;
use App\Entity\Ajustes\Usuario;
use App\Entity\Almacen\Modelo;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class ModeloVoter extends Voter
{
const VIEW = 'MOD_VIEW';
const EDIT = 'MOD_EDIT';
const DELETE = 'MOD_DELETE';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::EDIT, self::VIEW, self::DELETE])
&& $subject instanceof Modelo;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::EDIT:
// logic to determine if the user can EDIT
// return true or false
return $this->canEdit($subject, $user);
case self::VIEW:
// logic to determine if the user can VIEW
// return true or false
return $this->canView($subject, $user);
case self::DELETE:
// logic to determine if the user can DELETE
// return true or false
return $this->canDelete($subject, $user);
}
return false;
}
private function canView(Modelo $subject,Usuario $user){
if ($this->security->isGranted('ROLE_MOD_VIEW'))
return true;
return false;
}
private function canEdit(Modelo $subject,Usuario $user){
if ($this->security->isGranted('ROLE_MOD_EDIT'))
return true;
return false;
}
private function canDelete(Modelo $subject,Usuario $user){
if ($this->security->isGranted('ROLE_MOD_DELETE'))
return true;
return false;
}
}