<?php
namespace App\Security\Voter\Administracion;
use App\Entity\Administracion\Propuesta;
use App\Entity\Ajustes\Usuario;
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 PropuestaVoter extends Voter
{
const VIEW = 'PRO_VIEW';
const EDIT = 'PRO_EDIT';
const DELETE = 'PRO_DELETE';
const DUPLICATE = 'PRO_DUPLICATE';
const CONFIG = 'PRO_CONFIG';
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, self::DUPLICATE, self::CONFIG])
&& $subject instanceof Propuesta;
}
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);
case self::DUPLICATE:
// logic to determine if the user can DUPLICATE
// return true or false
return $this->canDuplicate($subject, $user);
case self::CONFIG:
// logic to determine if the user can DUPLICATE
// return true or false
return $this->canConfig($subject, $user);
}
return false;
}
private function canView(Propuesta $subject, Usuario $user){
if ($this->security->isGranted('ROLE_PRO_VIEW'))
return true;
return false;
}
private function canEdit(Propuesta $subject, Usuario $user){
if ($this->security->isGranted('ROLE_PRO_EDIT'))
return true;
return false;
}
private function canDelete(Propuesta $subject, Usuario $user){
if ($this->security->isGranted('ROLE_PRO_DELETE'))
return true;
return false;
}
private function canDuplicate(Propuesta $subject, Usuario $user){
if ($this->security->isGranted('ROLE_PRO_DUPLICATE'))
return true;
return false;
}
private function canConfig(Propuesta $subject, Usuario $user){
if ($this->security->isGranted('ROLE_PRO_CONFIG'))
return true;
return false;
}
}