<?php
//------------------------------------------------------------------------------
// src/Security/ClientPlatform/MissionVoter.php
//------------------------------------------------------------------------------
namespace App\Security\ClientPlatform;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use App\Entity\AccessClient\AccessClient;
use App\Entity\Mission\Mission;
use App\Services\AccessClient\AccessClientTools;
use App\Services\Config\OptionConfigTools;
use App\Services\LogTools;
class MissionVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const VIEW = "client_view_mission";
const EDIT = "client_edit_mission";
const CAN_BOOK_SLOT = "can_book_slot_mission";
const IS_GRANTED_CONSTANTS = array(
self::VIEW,
self::EDIT,
self::CAN_BOOK_SLOT,
);
//--------------------------------------------------------------------------------
public function __construct(Security $security, ManagerRegistry $doctrine, AccessClientTools $accessClientTools, LogTools $logTools, OptionConfigTools $optionConfigTools)
{
$this->security = $security;
$this->em = $doctrine->getManager();
$this->accessClientTools = $accessClientTools;
$this->logTools = $logTools;
$this->optionConfigTools = $optionConfigTools;
}
protected function supports(string $attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// only vote on Mission objects inside this voter
if ($subject !== null && !$subject instanceof Mission)
{
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
// Only access ROLE_CLIENT
if (!$this->security->isGranted('ROLE_CLIENT'))
{
return false;
}
$user = $token->getUser();
if (!$user instanceof AccessClient)
{
// the user must be logged in; if not, deny access
return false;
}
switch ($attribute)
{
case self::VIEW:
return $this->canView($subject, $user);
case self::EDIT:
return $this->canEdit($subject, $user);
case self::CAN_BOOK_SLOT:
return $this->canBookSlot($subject, $user);
}
throw new \LogicException('This code should not be reached!');
}
// Id the $object related to $accessClient ?
private function checkOwnership(Mission $object, AccessClient $accessClient)
{
$clientObjects = $this->accessClientTools->getMissionsForAccessClient($accessClient);
foreach ($clientObjects as $clientObject)
{
if ($clientObject->equals($object))
{
return true;
}
}
return false;
}
private function canView(Mission $mission, AccessClient $user)
{
// Check Ownership
if ($this->checkOwnership($mission, $user))
{
return true;
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Mission $mission, AccessClient $user)
{
// Check Ownership
if ($this->checkOwnership($mission, $user))
{
return true;
}
// If we are here, all hope is lost
return false;
}
private function canBookSlot(Mission $mission, AccessClient $user)
{
if (!$this->canView($mission, $user))
{
return false;
}
// NOTE: Use SocietyGroupOwner, we want the permission from the current mission manager (SocietyGroup)
$societyGroupOwner = $mission->getSocietyGroupOwner();
if (!$this->optionConfigTools->isActive_OnlineBooking($societyGroupOwner))
{
return false;
}
if (!$this->optionConfigTools->isActive_planningOptimisation($societyGroupOwner))
{
return false;
}
return true;
}
}