<?php
namespace App\Security\Voter;
use App\Entity\User;
use App\Utils\Commons;
use LogicException;
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 BackendPopUpMessageVoter extends Voter
{
const BACKEND_POP_UP_VIEW = 'BACKEND_POP_UP_VIEW';
const BACKEND_POP_UP_CREATE = 'BACKEND_POP_UP_CREATE';
const BACKEND_POP_UP_EDIT = 'BACKEND_POP_UP_EDIT';
const BACKEND_POP_UP_DELETE = 'BACKEND_POP_UP_DELETE';
const BACKEND_POP_UP_STATUS_CHANGE = 'BACKEND_POP_UP_STATUS_CHANGE';
/**
* @var Security
*/
private $security;
/**
* @var Commons
*/
private $commons;
/**
* @var array
*/
private $permissions;
/**
* BackendPlanVoter constructor.
* @param Security $security
* @param Commons $commons
*/
public function __construct(Security $security, Commons $commons)
{
$this->security = $security;
$this->commons = $commons;
$this->permissions = [];
}
/**
* @param mixed $attribute
* @param mixed $subject
* @return bool
*/
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, [self::BACKEND_POP_UP_VIEW, self::BACKEND_POP_UP_STATUS_CHANGE, self::BACKEND_POP_UP_EDIT, self::BACKEND_POP_UP_CREATE, self::BACKEND_POP_UP_DELETE])) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
$this->permissions = $this->commons->getBackendPermissions($user);
if ($this->hasSuperUser() === true) {
return true;
}
switch ($attribute) {
case self::BACKEND_POP_UP_VIEW:
return $this->canView();
case self::BACKEND_POP_UP_CREATE:
return $this->canCreate();
case self::BACKEND_POP_UP_EDIT:
return $this->canEdit();
case self::BACKEND_POP_UP_DELETE:
return $this->canDelete();
case self::BACKEND_POP_UP_STATUS_CHANGE:
return $this->canChangeStatus();
}
throw new LogicException('Invalid attribute: ' . $attribute);
}
/**
* Return True if have Super View Permission else return false
* @return bool
*/
private function hasSuperUser(): bool
{
if (array_key_exists('superUser', $this->permissions)) {
if (in_array('Yes', $this->permissions['superUser'])) {
return true;
}
}
return false;
}
/**
* Return True if have View Permission else return false
* @return bool
*/
private function canView(): bool
{
if (array_key_exists('backendPopUp', $this->permissions)) {
if (in_array('View', $this->permissions['backendPopUp'])) {
return true;
}
}
return false;
}
/**
* Return True if have Edit Permission else return false
* @return bool
*/
private function canEdit(): bool
{
if (array_key_exists('backendPopUp', $this->permissions)) {
if (in_array('Edit', $this->permissions['backendPopUp'])) {
return true;
}
}
return false;
}
/**
* Return True if have Create Permission else return false
* @return bool
*/
private function canCreate(): bool
{
if (array_key_exists('backendPopUp', $this->permissions)) {
if (in_array('Create', $this->permissions['backendPopUp'])) {
return true;
}
}
return false;
}
/**
* Return True if have Delete Permission else return false
* @return bool
*/
private function canDelete(): bool
{
if (array_key_exists('backendPopUp', $this->permissions)) {
if (in_array('Delete', $this->permissions['backendPopUp'])) {
return true;
}
}
return false;
}
/**
* Return True if have Change Status Permission else return false
* @return bool
*/
private function canChangeStatus(): bool
{
if (array_key_exists('backendPopUp', $this->permissions)) {
if (in_array('Change', $this->permissions['backendPopUp'])) {
return true;
}
}
return false;
}
}