src/Security/Voters/Organisation/Profile.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 Profile extends Voter
{
public const PROFILE_EDIT = 'org_profile_edit';
public const PROFILE_PRIVILEGES = 'org_privileges';
public const PROFILE_SUCCESS_HISTORY = 'org_success_history';
public const PROFILE_LINK = 'org_profile_link';
/**
* @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::PROFILE_EDIT, self::PROFILE_PRIVILEGES, self::PROFILE_SUCCESS_HISTORY,])) {
return true;
}
}
// if ($subject instanceof \App\Entity\User\User) {
if ($this->security->getUser()) {
if (in_array($attribute, [self::PROFILE_LINK])) {
return true;
}
}
// }
return false;
}
/**
* @param string $attribute
* @param Organisation $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::PROFILE_EDIT:
return $this->profileEdit($subject);
case self::PROFILE_LINK:
return $this->profileLink();
case self::PROFILE_PRIVILEGES:
return $this->havePrivileges($subject);
case self::PROFILE_SUCCESS_HISTORY:
return $this->haveSuccessHistory($subject);
}
throw new \LogicException('This code should not be reached!');
}
/**
* @param Organisation $subject
* @return bool
*/
protected function profileEdit(Organisation $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->getId();
}
private function profileLink()
{
$subject = $this->security->getUser();
$orgUser = $this->em->getRepository(User::class)->findByUser($subject->getId());
if (!$orgUser) {
return false;
}
if ($orgUser->getOrganisation()->isDeleted()) {
return false;
}
return true;
}
private function havePrivileges(Organisation $subject)
{
if (in_array($subject->getTypeString(), [$subject::TYPE_VUZ])) {
return true;
}
return false;
}
private function haveSuccessHistory(Organisation $subject)
{
if (in_array($subject->getTypeString(), [$subject::TYPE_VUZ])) {
return true;
}
return false;
}
}