<?php
//------------------------------------------------------------------------------
// src/Security/IkeaVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use App\Entity\Access;
use App\Entity\APIRest\AccessAPI;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\Ikea\ServiceOrder;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Invoice\Invoice;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Entity\Webapp\Document;
use App\Services\Config\ModuleTools;
use App\Services\Config\OptionConfigTools;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
class IkeaVoter extends Voter
{
const IS_ACTIVE = "ikea_service_order_is_active";
const IS_ACTIVE_IKEA_BP = "ikea_bp_is_active";
const USE_IKEA_BP = "use_ikea_bp";
const LISTING = "list_ikea_service_orders";
const LISTING_SOCIETY = "list_ikea_service_orders_society";
const LISTING_ANY = "list_ikea_service_orders_any";
const VIEW = "view_ikea_service_order";
const EDIT = "edit_ikea_service_order";
const EDIT_SOCIETY = "edit_ikea_service_order_society";
const CONVERT_TO_MISSION = "convert_ikea_service_order_to_mission";
const LINK_TO_MISSION = "link_ikea_service_order_to_mission";
const CHANGE_MISSION = "change_ikea_service_mission";
const VIEW_ERRORS = "view_ikea_service_order_errors";
// Plan.io Task #3846
const UPDATE_STATUS_FORCED = "update_status_forced";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::IS_ACTIVE_IKEA_BP,
self::USE_IKEA_BP,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::EDIT_SOCIETY,
self::CONVERT_TO_MISSION,
self::LINK_TO_MISSION,
self::CHANGE_MISSION,
self::VIEW_ERRORS,
self::UPDATE_STATUS_FORCED, // Plan.io Task #3846
);
// Plan.io Task #3846
const UPDATE_STATUS_FORCED_ACCESSES = array(
'johann', 'alexandre', 'oanalivia', 'dylan'
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = 'ikea_service_order_add';
const ACL_PERM_LIST = 'ikea_service_order_list';
const ACL_PERM_LIST_SOCIETY = 'ikea_service_order_list_society';
const ACL_PERM_VIEW = 'ikea_service_order_view';
const ACL_PERM_VIEW_SOCIETY = 'ikea_service_order_view_society';
const ACL_PERM_EDIT = 'ikea_service_order_edit';
const ACL_PERM_EDIT_SOCIETY = 'ikea_service_order_edit_society';
const ACL_PERM_SOCIETY_EDIT = 'ikea_service_order_society_edit';
const ACL_PERM_SOCIETY_EDIT_SOCIETY = 'ikea_service_order_society_edit_society';
const ACL_PERM_VIEW_ERRORS = 'ikea_service_order_view_errors';
// Plan.io Task #4040
const ACL_PERM_UPDATE_STATUS_FORCED = 'ikea_service_order_edit_status_forced';
// Plan.io Task #4448
const ACL_PERM_USE_IKEA_BP = 'use_ikea_bp';
//--------------------------------------------------------------------------------
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, ManagerRegistry $doctrine,
ModuleTools $moduleTools, OptionConfigTools $optionConfigTools)
{
$this->accessDecisionManager = $accessDecisionManager;
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->optionConfigTools = $optionConfigTools;
$this->aclRepository = $this->em->getRepository(Acl::class);
$this->aclPermissionRepository = $this->em->getRepository(AclPermission::class);
}
// Plan.io Task #4453 [See AccessVoter for details]
public function supportsAttribute(string $attribute): bool
{
return in_array($attribute, self::IS_GRANTED_CONSTANTS, true);
}
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 ServiceOrder objects inside this voter
if ($subject !== null && !$subject instanceof ServiceOrder)
{
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
$originalUserIsAccess = true;
// Plan.io Task #3707
if ($user instanceof AccessAPI)
{
if ($user->getAccess() === null)
{
return false;
}
$user = $user->getAccess();
$originalUserIsAccess = false;
}
// Plan.io Task #3707
// At this point $user is an object of Access type
// even if the $token->getUser() is AccessAPI
if (!$user instanceof Access)
{
// the user must be logged in; if not, deny access
return false;
}
// The user must have a function; if not deny access
$function = $user->getFunction();
if ($function === null) return false;
// Plan.io Task #3710 : Get current group
$currentGroup = $user->getSocietyGroup();
if ($currentGroup === null)
return false;
$this->currentGroup = $currentGroup;
// Module activated ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_IKEA_SERVICE_ORDER))
{
return false;
}
// you know $subject is a ServiceOrder object, thanks to supports
/** @var ServiceOrder $serviceOrder */
$serviceOrder = $subject;
// Check current group affectation
if ($subject !== null)
{
$subjectSociety = $subject->getSociety();
if ($subjectSociety === null)
return false;
$subjectGroup = $subjectSociety->getSocietyGroup();
if ($subjectGroup === null)
return false;
if (!$currentGroup->equals($subjectGroup))
return false;
}
$this->currentGroup = $currentGroup;
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
case self::IS_ACTIVE_IKEA_BP:
{
// TODO_4448 : Remove this before test / prod
// if (!$this->accessDecisionManager->decide($token, ['open_sesame']))
// return false;
return $this->optionConfigTools->isActive_IkeaBP($currentGroup);
}
case self::USE_IKEA_BP:
{
// TODO_4448 : Remove this before test / prod
// if (!$this->accessDecisionManager->decide($token, ['open_sesame']))
// return false;
// This also checks OptionConfig :: isActive_IkeaBP
// So it can be used to check both if option is active and user can use option
return $this->canUseIkeaBp($user, $function);
}
case self::VIEW:
return $this->canView($serviceOrder, $user, $function);
case self::EDIT:
return $this->canEdit($serviceOrder, $user, $function);
case self::EDIT_SOCIETY:
return $this->canEditSociety($serviceOrder, $user, $function);
case self::LISTING:
return $this->canList($user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_ANY:
return $this->canListAny($user, $function);
case self::CONVERT_TO_MISSION:
return $this->canConvertToMission($serviceOrder, $user, $function, $token);
case self::LINK_TO_MISSION:
return $this->canLinkToMission($serviceOrder, $user, $function, $token);
case self::CHANGE_MISSION:
return $this->canChangeMission($serviceOrder, $user, $function, $token);
case self::VIEW_ERRORS:
return $this->canViewErrors($user, $function);
// Plan.io Task #3846, #4040
case self::UPDATE_STATUS_FORCED:
return $this->canUpdateStatusForced($user, $function, $token);
}
throw new \LogicException('This code should not be reached!');
}
// Check if the Society of the Demand
// belongs to the societies of the $access
private function checkSociety(ServiceOrder $serviceOrder, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Demand
$serviceOrderSociety = $serviceOrder->getSociety();
if ($serviceOrderSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $serviceOrderSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canView(ServiceOrder $serviceOrder, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_SOCIETY);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here it means that nothing good has been found
// Load second permission
if ($aclPermSociety !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
return $this->checkSociety($serviceOrder, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(ServiceOrder $serviceOrder, Access $user, AccessFunction $function)
{
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_SOCIETY);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here it means that nothing good has been found
// Load second permission
if ($aclPermSociety !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
return $this->checkSociety($serviceOrder, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditSociety(ServiceOrder $serviceOrder, Access $user, AccessFunction $function)
{
// Get Acl_Perdemands
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT_SOCIETY);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here it means that nothing good has been found
// Load second permission
if ($aclPermSociety !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
return $this->checkSociety($serviceOrder, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canList(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Perdemand
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
// Since only one acl type can exist
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canListSociety(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Perdemand
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_SOCIETY);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
// Since only one acl type can exist
// we can return the result of the acl_permission
return $acl->getValue();
}
private function canListAny(Access $user, AccessFunction $function)
{
// Three Acl_Perdemand may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_SOCIETY);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here it means that nothing good has been found
// Load second permission
if ($aclPermSociety !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here, all hope is lost
return false;
}
private function canConvertToMission(ServiceOrder $serviceOrder, Access $user, AccessFunction $function, $token)
{
if ($serviceOrder->getClient() === null)
{
// This should not happen
return false;
}
if ($serviceOrder->getMission() !== null)
{
// Already attached to a mission
return false;
}
// if ($serviceOrder->getResultingMission() !== null)
// {
// // Already converted to a mission
// return false;
// }
// Request permission to add missions
$canAddMissions = $this->accessDecisionManager->decide($token, ['add_mission']);
// Request permission to edit this demand
$canEditServiceOrder = $this->canEdit($serviceOrder, $user, $function);
if ($canAddMissions && $canEditServiceOrder)
{
return true;
}
return false;
}
private function canLinkToMission(ServiceOrder $serviceOrder, Access $user, AccessFunction $function, $token)
{
if ($serviceOrder->getClient() === null)
{
// This should not happen
return false;
}
if ($serviceOrder->getMission() !== null)
{
// Already attached to a mission
return false;
}
// if ($serviceOrder->getResultingMission() !== null)
// {
// // Already converted to a mission
// return false;
// }
// Request permission to edit this demand
$canEditServiceOrder = $this->canEdit($serviceOrder, $user, $function);
if ($canEditServiceOrder)
{
return true;
}
return false;
}
private function canChangeMission(ServiceOrder $serviceOrder, Access $user, AccessFunction $function, $token)
{
if ($serviceOrder->getClient() === null)
{
// This should not happen
return false;
}
if ($serviceOrder->getMission() === null)
{
// No changing if no mission at all
return false;
}
// TODO.3621
// Get only the distinct orderNumbers from the ServiceOrders attached to the mission
// // Get all service orders linked to the same mission
// // or with the potential to be linked to the same mission
// $twinServiceOrders = $this->em->getRepository(ServiceOrder::class)
// ->getTwinsForMission($serviceOrder, $serviceOrder->getMission());
// // Add the current one
// $twinServiceOrders[] = $serviceOrder;
// foreach ($twinServiceOrders as $so)
// {
// $object = $this->em->getRepository(Devis::class)->findOneByIkeaServiceOrder($so);
// if ($object !== null)
// {
// return false;
// }
// $object = $this->em->getRepository(Invoice::class)->findOneByIkeaServiceOrder($so);
// if ($object !== null)
// {
// return false;
// }
// $object = $this->em->getRepository(Document::class)->findOneByIkeaServiceOrder($so);
// if ($object !== null)
// {
// return false;
// }
// }
// Request permission to edit this demand
$canEditServiceOrder = $this->canEdit($serviceOrder, $user, $function);
if ($canEditServiceOrder)
{
return true;
}
return false;
}
private function canViewErrors(Access $user, AccessFunction $function)
{
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_ERRORS);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
// Since only one acl type can exist
// we can return the result of the acl_permission
return $acl->getValue();
}
// Plan.io Task #3846
private function canUpdateStatusForced(Access $user, AccessFunction $function, TokenInterface $token)
{
// Keep original code also
// I want the real user here, not the impersonated one
$originalUser = null;
if ($token instanceof SwitchUserToken)
{
$originalUser = $token->getOriginalToken()->getUser();
}
else
{
$originalUser = $user;
}
if (in_array($originalUser->getUserIdentifier(), self::UPDATE_STATUS_FORCED_ACCESSES))
{
return true;
}
// Plan.io Task #4040 : Add Acl based code
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_UPDATE_STATUS_FORCED);
// If all are null, exit
if ($aclPerm === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here, all hope is lost
return false;
}
private function canUseIkeaBp(Access $user, AccessFunction $function)
{
// Check Option Active first
if (!$this->optionConfigTools->isActive_IkeaBP($this->currentGroup))
{
return false;
}
$aclPerm = $this->aclPermissionRepository->findOneByName(self::USE_IKEA_BP);
if ($aclPerm === null) return false;
// Get Acl
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl === null) return false;
// Since only one acl type can exist
// we can return the result of the acl_permission
return $acl->getValue();
}
}