<?php
namespace App\Security\Voter;
use App\Util\AccountTools;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Security\Core\Security;
class LoginMessageVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject) : bool
{
// NEW actions don't yet have objects
if(in_array($attribute, ['LOGIN_MESSAGES'])) {
return true;
}
if(in_array($attribute, ['NEW_LOGIN_MESSAGE'])) {
return true;
}
return in_array($attribute, ['EDIT', 'DELETE'])
&& $subject instanceof \App\Model\LoginMessage;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) : bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if(!isset($_SERVER['APP_QP_LOGIN_MSG_ALLOWED'])) {
return false;
}
if(isset($_SERVER['APP_QP_LOGIN_MSG_ALLOWED'])) {
if($_SERVER['APP_QP_LOGIN_MSG_ALLOWED'] == 'false') {
return false;
}
}
switch ($attribute) {
case 'EDIT':
if($this->security->isGranted('ROLE_POSTMASTER')) {
return true;
}
break;
case 'DELETE':
if($this->security->isGranted('ROLE_POSTMASTER')) {
return true;
}
break;
case 'NEW_LOGIN_MESSAGE':
if($this->security->isGranted('ROLE_POSTMASTER')) {
return true;
}
break;
case 'LOGIN_MESSAGES':
if($this->security->isGranted('ROLE_POSTMASTER')) {
return true;
}
break;
}
return false;
}
}