src/Security/Voters/Organisation/View.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 View extends Voter
{
public const UNIVERSITY_VIEW = 'university_view';
public const OLYMP_CENTER_VIEW = 'olymp_center_view';
/**
* @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 Organisation) {
if (in_array($attribute, [self::UNIVERSITY_VIEW, self::OLYMP_CENTER_VIEW])) {
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::UNIVERSITY_VIEW:
return $this->universityView($subject);
case self::OLYMP_CENTER_VIEW:
return $this->ocView($subject);
}
throw new \LogicException('This code should not be reached!');
}
/**
*
* @return bool
*/
protected function universityView(Organisation $subject): bool
{
// dump($subject);
if (false == $subject->isTypeVuz()) {
return false;
}
return $this->haveAccess($subject);
}
private function haveAccess(Organisation $subject): bool
{
if ($subject->isDeleted()) {
return false;
}
if ($subject->isDeleted() == false && $subject->isActive() == true) {
return true;
}
/** @var \App\Entity\User\User $user */
$user = $this->security->getUser();
if (!$user && !$subject->isActive()) {
return false;
}
if ($subject->isActive() == false) {
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
$orgUser = $this->em->getRepository(User::class)->findByUser($user->getId());
if (!$orgUser) {
return false;
}
return $orgUser->getOrganisation()->getId() == $subject->getId();
}
return false;
}
/**
*
* @return bool
*/
protected function ocView(Organisation $subject): bool
{
// dump($subject);
if (false == $subject->isTypeOc()) {
return false;
}
return $this->haveAccess($subject);
}
}