<?php
namespace App\Security\Voter;
use Symfony\Component\HttpFoundation\RequestStack;
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 PermissionVoter extends Voter
{
public const EDIT = 'POST_EDIT';
public const VIEW = 'POST_VIEW';
private $session;
private $security;
private $requestStack;
public function __construct(RequestStack $sessionInterface, Security $security)
{
$this->security = $security;
// $this->session = $sessionInterface;
$this->requestStack =$sessionInterface;
$this->session = $this->requestStack->getSession();
}
protected function supports(string $attribute, $subject): bool
{
if (!$this->security->getUser() )
return false;
$permission = $this->session->get("PERMISSION");
if ($this->security->getUser() && in_array("rlspad", $this->security->getUser()->getRoles()))
return true;
if (!$permission)
$permission = array();
return in_array($attribute, $permission);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return true;
}
}