src/Security/Voters/Olympiad/Profile.php line 15
<?php
namespace App\Security\Voters\Olympiad;
use App\Entity\Olympiad\Olympiad;
use App\Entity\Organisation\Organisation;
use App\Entity\Organisation\User;
use App\Entity\User\UserRights;
use App\Model\Common\GetOwnerInterface;
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_LINK = 'olymp_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 Olympiad) {
/* 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!');
}
private function profileLink(): bool
{
$user = $this->security->getUser();
$user_id = $user->getId();
if (empty($user_id)) {
return false;
}
$userRight = $this->em->getRepository(UserRights::class)->getUserRolesByType($user_id, [GetOwnerInterface::OWNER_OLYMPIAD, GetOwnerInterface::OWNER_OLYMPIAD_STAGE]);
if (!$userRight) {
return false;
}
return true;
}
/**
* @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 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;
}
}