<?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 BackendTeamVoter extends Voter
{
const TEAM_VIEW = 'TEAM_VIEW';
const TEAM_SHOW = 'TEAM_SHOW';
const TEAM_CREATE = 'TEAM_CREATE';
const TEAM_EDIT = 'TEAM_EDIT';
const TEAM_DELETE = 'TEAM_DELETE';
/**
* @var Security
*/
private $security;
/**
* @var Commons
*/
private $commons;
/**
* @var array
*/
private $permissions;
public function __construct(Security $security, Commons $commons)
{
$this->security = $security;
$this->commons = $commons;
$this->permissions = [];
}
/**
* @param string $attribute
* @param mixed $subject
* @return bool
*/
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, [self::TEAM_VIEW, self::TEAM_SHOW, self::TEAM_EDIT, self::TEAM_CREATE, self::TEAM_DELETE])) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
* @return bool
*/
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::TEAM_VIEW:
return $this->canView();
case self::TEAM_CREATE:
return $this->canCreate();
case self::TEAM_EDIT:
return $this->canEdit();
case self::TEAM_DELETE:
return $this->canDelete();
case self::TEAM_SHOW:
return $this->canShow();
}
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('teams', $this->permissions)) {
if (in_array('View', $this->permissions['teams'])) {
return true;
}
}
return false;
}
/**
* Return True if have Show Permission else return false
* @return bool
*/
private function canShow(): bool
{
if (array_key_exists('teams', $this->permissions)) {
if (in_array('Show', $this->permissions['teams'])) {
return true;
}
}
return false;
}
/**
* Return True if have Edit Permission else return false
* @return bool
*/
private function canEdit(): bool
{
if (array_key_exists('teams', $this->permissions)) {
if (in_array('Edit', $this->permissions['teams'])) {
return true;
}
}
return false;
}
/**
* Return True if have Create Permission else return false
* @return bool
*/
private function canCreate(): bool
{
if (array_key_exists('teams', $this->permissions)) {
if (in_array('Create', $this->permissions['teams'])) {
return true;
}
}
return false;
}
/**
* Return True if have Delete Permission else return false
* @return bool
*/
private function canDelete(): bool
{
if (array_key_exists('teams', $this->permissions)) {
if (in_array('Delete', $this->permissions['teams'])) {
return true;
}
}
return false;
}
}