<?php
namespace App\Security\Voters;
use App\Entity\User;
use App\Security\SecurityTrait;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class UserVoter extends Voter
{
use SecurityTrait;
const LIST = "list_user";
const ADD = "add_user";
const EDIT = "edit_user";
const READ = "read_user";
const REMOVE = "remove_user";
const PASSWORD_EMAIL = "password_email_user";
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
return in_array($attribute, [self::LIST, self::ADD, self::PASSWORD_EMAIL]) ||
(in_array($attribute, [
self::EDIT,
self::READ,
self::REMOVE
]) && $subject instanceof User);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::LIST:
return $this->isAdmin();
case self::ADD:
return $this->isAdmin();
case self::EDIT:
return $this->isAdmin();
case self::READ:
return $this->isAdmin();
case self::REMOVE:
return $this->isAdmin();
case self::PASSWORD_EMAIL:
return $this->isAdmin();
}
throw new \LogicException('This code should not be reached!');
}
}