<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Security;
class GeneralVoter extends Voter {
private $security;
public function __construct(Security $security) {
$this->security = $security;
}
protected function supports($attribute, $subject) {
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['_EDIT', '_VIEW', '_CREATE', '_DELETE']);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
return true;
}
if (in_array($subject . '_ALL', $user->getRoles())) {
return true;
}
if (in_array(strtoupper($subject . $attribute), $user->getRoles())) {
return true;
}
return false;
}
}