src/Security/Voters/Organisation/Page.php line 12
<?php
namespace App\Security\Voters\Organisation;
use App\Entity\Organisation\Organisation;
use App\Entity\Organisation\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class Page extends Voter
{
public const PAGE_EDIT = 'page_edit';
/**
* @var Security
*/
private $security;
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(Security $security, EntityManagerInterface $em)
{
$this->security = $security;
$this->em = $em;
}
protected function supports(string $attribute, $subject): bool
{
if ($subject instanceof \App\Entity\Organisation\Page) {
if (in_array($attribute, [self::PAGE_EDIT])) {
return true;
}
}
return false;
}
/**
* @param string $attribute
*
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::PAGE_EDIT:
return $this->pageEdit($subject);
}
throw new \LogicException('This code should not be reached!');
}
/**
*
* @return bool
*/
protected function pageEdit(\App\Entity\Organisation\Page $subject): bool
{
/** @var \App\Entity\User\User $user */
$user = $this->security->getUser();
if ($subject->isDeleted() || !$user) {
return false;
}
$orgUser = $this->em->getRepository(User::class)->findByUser($user->getId());
if (!$orgUser) {
return false;
}
return $orgUser->getOrganisation()->getId() == $subject->getOrganisation()->getId();
}
}