<?php
namespace App\Security\Voter\Configuracion;
use App\Entity\Ajustes\Usuario;
use App\Entity\Configuracion\Abrasivo;
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 AbrasivoVoter extends Voter
{
const VIEW = 'ABRASIVO_VIEW';
const EDIT = 'ABRASIVO_EDIT';
const DELETE = 'ABRASIVO_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 Abrasivo;
}
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(Abrasivo $subject,Usuario $user){
if ($this->security->isGranted('ROLE_ABRASIVO_VIEW'))
return true;
return false;
}
private function canEdit(Abrasivo $subject,Usuario $user){
if ($this->security->isGranted('ROLE_ABRASIVO_EDIT'))
return true;
return false;
}
private function canDelete(Abrasivo $subject,Usuario $user){
if ($this->security->isGranted('ROLE_ABRASIVO_DELETE'))
return true;
return false;
}
}