<?php
namespace App\Security\Voter;
use App\Entity\User;
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 DashboardVoter extends Voter
{
// group must be separated by single underline, multiple words could be separated by double underline
// for example DASHBOARD_MEMO_VISUALIZZA__ASSEGNATARIO -> DASHBOARD -> MEMO -> VISUALIZZA ASSEGNATARIO
// Don't use double underline in constant names
// translate each expression separately at messages.it.yml
// Add __disabled suffix to make permission disabled and checked by default
const CDU_VISUALIZZA = 'DASHBOARD_CDU_VISUALIZZA';
const MEMO_VISUALIZZA_ASSEGNATARIO = 'DASHBOARD_MEMO_VISUALIZZA__ASSEGNATARIO__DISABLED';
const APPUNTAMENTI_VISUALIZZA_ASSEGNATARIO = 'DASHBOARD_APPUNTAMENTI_ASSEGNATARIO__DISABLED';
const SCADENZA_VISUALIZZA = 'DASHBOARD_SCADENZA_VISUALIZZA__DISABLED';
const MYSELF_VISUALIZZA = 'DASHBOARD_MYSELF_VISUALIZZA__DISABLED';
const MEMO_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO = 'DASHBOARD_MEMO_PRENDI__I__PERMESSI__DELL__ASSEGNATARIO';
const APPUNTAMENTI_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO = 'DASHBOARD_APPUNTAMENTI_PRENDI__I__PERMESSI__DELL__ASSEGNATARIO';
const ORDER = 5;
const PERMISSIONS_LIST = [
self::MEMO_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO,
self::APPUNTAMENTI_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO,
self::MEMO_VISUALIZZA_ASSEGNATARIO,
self::CDU_VISUALIZZA,
self::APPUNTAMENTI_VISUALIZZA_ASSEGNATARIO,
self::SCADENZA_VISUALIZZA,
self::MYSELF_VISUALIZZA,
];
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::PERMISSIONS_LIST)) {
return false;
}
return true;
}
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(User::ROLE_SUPER_ADMIN)) return true;
return in_array($attribute, $user->getPermissions());
}
}