<?php
//------------------------------------------------------------------------------
// src/Security/MissionVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\APIRest\AccessAPI;
use App\Entity\Client\Client;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\Mission\Mission;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Entity\Webapp\Document;
use App\Services\LogTools;
use App\Services\Config\ModuleTools;
use App\Services\Mission\MissionSecurityTools;
class MissionVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "mission_is_active";
const IS_ACTIVE_SHARING = "mission_sharing_is_active";
const ADD = "add_mission";
const ADD_OR_CONVERT = "add_mission_or_convert_devis_simulation";
const LISTING = "list_missions";
const LISTING_SOCIETY = "list_missions_society";
const LISTING_AUTHOR = "list_missions_author";
const LISTING_ANY = "list_missions_any";
const VIEW = "view_mission";
const EDIT = "edit_mission";
const EDIT_SOCIETY = "edit_mission_society";
const EDIT_MANAGER = "edit_mission_manager";
const SHARE = "share_mission";
// Plan.io Task #3304
const PAY_OWNER = "pay_mission_owner";
// Plan.io Task #4071
const ADD_CHILD = "add_mission_child";
// Plan.Io Task #4247
const MERGE = "merge_mission";
const MERGE_TOOL = "merge_tool_mission";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::IS_ACTIVE_SHARING,
self::ADD,
self::ADD_OR_CONVERT,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_AUTHOR,
self::LISTING_ANY,
self::VIEW,
self::EDIT,
self::EDIT_SOCIETY,
self::EDIT_MANAGER,
self::SHARE,
self::PAY_OWNER,
self::ADD_CHILD,
self::MERGE,
self::MERGE_TOOL,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = 'mission_add';
const ACL_PERM_LIST = 'mission_list';
const ACL_PERM_LIST_SOCIETY = 'mission_list_society';
const ACL_PERM_LIST_AUTHOR = 'mission_list_author';
const ACL_PERM_VIEW = 'mission_view';
const ACL_PERM_VIEW_SOCIETY = 'mission_view_society';
const ACL_PERM_VIEW_AUTHOR = 'mission_view_author';
const ACL_PERM_VIEW_TASK = 'mission_view_task';
const ACL_PERM_EDIT = 'mission_edit';
const ACL_PERM_EDIT_SOCIETY = 'mission_edit_society';
const ACL_PERM_EDIT_AUTHOR = 'mission_edit_author';
const ACL_PERM_EDIT_TASK = 'mission_edit_task';
const ACL_PERM_SOCIETY_EDIT = 'mission_society_edit';
const ACL_PERM_SOCIETY_EDIT_SOCIETY = 'mission_society_edit_society';
const ACL_PERM_SOCIETY_EDIT_AUTHOR = 'mission_society_edit_author';
const ACL_PERM_SHARE = 'mission_share';
const ACL_PERM_SHARE_SOCIETY = 'mission_share_society';
const ACL_PERM_SHARE_AUTHOR = 'mission_share_author';
const ACL_PERM_MANAGER_EDIT = 'mission_edit_manager';
const ACL_PERM_MANAGER_EDIT_SOCIETY = 'mission_edit_manager_society';
const ACL_PERM_MANAGER_EDIT_AUTHOR = 'mission_edit_manager_author';
// Plan.io Task #4247
const ACL_PERM_MERGE = "mission_merge";
const ACL_PERM_MERGE_SOCIETY = "mission_merge_society";
//--------------------------------------------------------------------------------
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, ManagerRegistry $doctrine, ModuleTools $moduleTools, LogTools $logTools, MissionSecurityTools $missionSecurityTools)
{
$this->accessDecisionManager = $accessDecisionManager;
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->logTools = $logTools;
$this->missionSecurityTools = $missionSecurityTools;
$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
{
// $this->logTools->ploopLog("[MissionVoter][supports] attribute = $attribute");
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// Only vote on Mission and Client objects inside this voter
if ($subject !== null && !($subject instanceof Mission || $subject instanceof Client))
{
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// Plan.io Task #3707
if ($user instanceof AccessAPI)
{
if ($user->getAccess() === null)
{
return false;
}
$user = $user->getAccess();
}
// 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_MISSION) &&
$this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_MISSION_LIGHT) &&
$this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_MISSION_PLUS)
)
{
return false;
}
// This one also needs clients or clients light
if (
$this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_CLIENT) &&
$this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_CLIENT_LIGHT)
)
{
return false;
}
$client = null;
$mission = null;
if ($subject instanceof Mission)
{
// you know $subject is a Mission object, thanks to supports
/** @var Mission $mission */
$mission = $subject;
// Check current group affectation
// This also includes mission sharing
// mission.society.group = currentGroup
// OR
// mission.getSocietyGroupOwner = currentGroup
if ($mission !== null)
{
$missionSociety = $mission->getSociety();
if ($missionSociety === null)
{
return false;
}
$missionSocietyGroup = $missionSociety->getGroup();
if ($missionSocietyGroup === null)
{
return false;
}
$missionSocietyGroupOwner = $mission->getSocietyGroupOwner();
if ($missionSocietyGroupOwner === null)
{
return false;
}
// Checking ...
if (!$currentGroup->equals($missionSocietyGroup) && !$currentGroup->equals($missionSocietyGroupOwner))
{
return false;
}
// Special case of archived missions
// This should only be visible by owner, since the owner is the one who archived them
if ($mission->isArchivedRefused())
{
if (!$currentGroup->equals($missionSocietyGroupOwner))
{
return false;
}
}
}
}
else
{
if ($subject instanceof Client)
{
/** @var Client $client */
$client = $subject;
}
}
switch ($attribute)
{
case self::IS_ACTIVE:
{
return true;
}
case self::IS_ACTIVE_SHARING:
{
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_MISSION_PLUS))
{
return false;
}
return true;
}
// We can create a mission without a client
// (the client can be created at the same time as the mission)
// So the client parameter is optional
case self::ADD:
return $this->canAdd($user, $function, $client);
case self::ADD_OR_CONVERT:
return $this->canAddOrConvert($user, $function, $token, $client);
case self::ADD_CHILD:
{
if ($mission === null) return false;
return $this->canAddChild($mission, $user, $function);
}
case self::VIEW:
return $this->canView($mission, $user, $function);
case self::EDIT:
return $this->canEdit($mission, $user, $function);
case self::EDIT_SOCIETY:
return $this->canEditSociety($mission, $user, $function);
case self::EDIT_MANAGER:
return $this->canEditManager($mission, $user, $function);
case self::SHARE:
return $this->canShare($mission, $user, $function);
case self::PAY_OWNER:
return $this->canPayMissionOwner($mission);
case self::LISTING:
return $this->canList($user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_AUTHOR:
return $this->canListAuthor($user, $function);
case self::LISTING_ANY:
return $this->canListAny($user, $function);
case self::MERGE:
return $this->canMerge($mission, $user, $function);
case self::MERGE_TOOL:
return $this->canUseMergeTool($user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// $access is the user trying to load the resource
// $mission is the resource being loaded
// Check if the Author of the Mission is the Access
private function checkAuthor(Mission $mission, Access $access)
{
// Get the Society of the Mission
$missionAuthor = $mission->getAuthor();
if ($missionAuthor === null)
return false;
if ($access->getId() == $missionAuthor->getId())
return true;
return false;
}
// Check if the $access is connected to any task of the mission
private function checkTask(Mission $mission, Access $access)
{
if (empty($mission->getTasks()))
return false;
foreach ($mission->getTasks() as $task)
{
foreach ($task->getPlanningResources() as $resource)
{
if ($resource->getAccess() !== null)
{
if ($access->equals($resource->getAccess()))
{
return true;
}
}
}
}
return false;
}
// Check if the Society of the Mission
// belongs to the societies of the $access
private function checkSociety(Mission $mission, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Mission
// If the mission was created by the currentGroup, get the mission.society
// If the mission was shared with the currentGroup, get the mission.societyOwner
$missionSociety = $mission->getSociety();
if ($missionSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $missionSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
// Check if the Society of the Mission
// belongs to the societies of the $access
private function checkSocietyOwner(Mission $mission, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Mission
// If the mission was created by the currentGroup, get the mission.society
// If the mission was shared with the currentGroup, get the mission.societyOwner
$missionSociety = $mission->getSocietyOwner();
if ($missionSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $missionSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
private function canAdd(Access $user, AccessFunction $function, $client = null)
{
// We can create a mission without a client
// (the client can be created at the same time as the mission)
// So the client parameter is optional
if ($client !== null)
{
// Individuals only for now
$individual = $client->getIndividual();
if ($individual !== null)
{
// Only allow creating missions for the individuals who belong tp the current group
if (!$individual->getSocietyGroup()->equals($this->currentGroup))
{
return false;
}
}
else
{
// Plan.io Task #3582 : No Stores as Receivers for now
return false;
}
}
// Module activated ?
// MISSION_LIGHT cannot add Missions
if ($this->moduleTools->isInactiveByCode($this->currentGroup, Module::MODULE_MISSION) && $this->moduleTools->isInactiveByCode($this->currentGroup, Module::MODULE_MISSION_PLUS))
{
return false;
}
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD);
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 canAddOrConvert(Access $user, AccessFunction $function, $token, $client = null)
{
if ($this->canAdd($user, $function, $client))
{
return true;
}
if ($this->accessDecisionManager->decide($token, ['convert_devis_simulation']))
{
return true;
}
return false;
}
private function canAddChild(Mission $parent, Access $user, AccessFunction $function)
{
// Check object restrictions because they are simpler
$reason = $parent->whyCannotAddChildren();
if ($reason > 0)
{
// $this->logTools->errorLog("[MissionVoter][canAddChild] The reason we cannot add a child for ".$parent->displayForLog()." is ".$reason);
}
if (!$parent->canAddChild())
{
return false;
}
// Decide based on Add permissions
return $this->canAdd($user, $function, $parent->getReceiver());
}
private function canView(Mission $mission, Access $user, AccessFunction $function)
{
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_SOCIETY);
$aclPermAuthor = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_AUTHOR);
$aclPermTask = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_TASK);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermAuthor === null && $aclPermTask === 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())
{
// Check Society : also check author
// Les missions appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkAuthor($mission, $user))
{
return true;
}
// When a mission is shared, the author can still view it
if ($this->checkSociety($mission, $user) || $this->checkSocietyOwner($mission, $user))
{
return true;
}
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermAuthor !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAuthor
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkAuthor($mission, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermTask !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermTask,
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkTask($mission, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Mission $mission, Access $user, AccessFunction $function)
{
// Plan.io Task #4071 : Deny on children
// Not anymore ;)
// if ($mission->isChild())
// {
// return false;
// }
// Deny edit on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_SOCIETY);
$aclPermAuthor = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_AUTHOR);
$aclPermTask = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_TASK);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermAuthor === null && $aclPermTask === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// Both author and owner can edit
return true;
// // When a mission is shared, the author cannot edit it
// if (!$mission->isShared())
// {
// // Not shared
// return true;
// }
// else
// {
// // Shared
// // The author cannot edit / The owner can edit
// if ($mission->isSharedWithSocietyGroup($this->currentGroup))
// {
// 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())
{
// Check Society : also check author
// Les missions appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkAuthor($mission, $user))
{
return true;
}
// Both author and owner can edit
// However, if the mission is shared,
// we are filtering using societyOwner instead of society
// Only one of the two conditions below can be true at a time
if ($this->checkSociety($mission, $user) || $this->checkSocietyOwner($mission, $user))
{
return true;
}
// // When a mission is shared, the author cannot edit it
// if (!$mission->isShared())
// {
// // Not shared : check society
// if ($this->checkSociety($mission, $user))
// {
// return true;
// }
// }
// else
// {
// // Shared
// // The author cannot edit / The owner can edit
// if ($mission->isSharedWithSocietyGroup($this->currentGroup))
// {
// if ($this->checkSocietyOwner($mission, $user))
// {
// return true;
// }
// }
// }
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermAuthor !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAuthor
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkAuthor($mission, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermTask !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermTask,
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkTask($mission, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditSociety(Mission $mission, Access $user, AccessFunction $function)
{
// Deny edit on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT_SOCIETY);
$aclPermAuthor = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SOCIETY_EDIT_AUTHOR);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermAuthor === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// Both author and owner can edit
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())
{
// Check Society : also check author
// Les missions appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkAuthor($mission, $user))
{
return true;
}
// Both author and owner can edit
// However, if the mission is shared,
// we are filtering using societyOwner instead of society
// Only one of the two conditions below can be true at a time
if ($this->checkSociety($mission, $user) || $this->checkSocietyOwner($mission, $user))
{
return true;
}
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermAuthor !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAuthor
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkAuthor($mission, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditManager($mission, Access $user, AccessFunction $function)
{
// Deny edit on archivedRefused objects
if ($mission !== null && $mission->isArchivedRefused())
{
return false;
}
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MANAGER_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MANAGER_EDIT_SOCIETY);
$aclPermAuthor = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MANAGER_EDIT_AUTHOR);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermAuthor === null)
return false;
// Get First one
if ($aclPerm !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPerm
));
if ($acl !== null)
{
if ($acl->getValue())
{
// Both author and owner can edit
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())
{
if ($mission !== null)
{
// Check Society : also check author
// Les missions appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkAuthor($mission, $user))
{
return true;
}
// Both author and owner can edit
// However, if the mission is shared,
// we are filtering using societyOwner instead of society
// Only one of the two conditions below can be true at a time
if ($this->checkSociety($mission, $user) || $this->checkSocietyOwner($mission, $user))
{
return true;
}
}
else
{
return $acl->getValue();
}
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermAuthor !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAuthor
));
if ($acl !== null)
{
if ($acl->getValue())
{
if ($mission !== null)
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkAuthor($mission, $user);
}
else
{
return $acl->getValue();
}
}
}
}
// If we are here, all hope is lost
return false;
}
private function canShare(Mission $mission, Access $user, AccessFunction $function)
{
// Module activated ?
if ($this->moduleTools->isInactiveByCode($this->currentGroup, Module::MODULE_MISSION_PLUS))
{
return false;
}
// Plan.io Task #4031 #4024 #4071
if ($this->missionSecurityTools->cannotBeShared($mission))
{
return false;
}
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SHARE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SHARE_SOCIETY);
$aclPermAuthor = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_SHARE_AUTHOR);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermAuthor === 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())
{
// Check Society : also check author
// Les missions appartenant à ses sociétés (et celles dont il est l'auteur)
if ($this->checkSociety($mission, $user))
{
return true;
}
else
{
$this->checkAuthor($mission, $user);
}
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermAuthor !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAuthor
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkAuthor($mission, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canPayMissionOwner(Mission $mission)
{
// Plan.io Task #3304
// Not paid, obviously :)
if ($mission->ownerWasPaid())
{
return false;
}
// Restricted to JCAF society group
if ($this->currentGroup->isNotJcaf())
{
return false;
}
// Restricted to JCAF missions
if ($mission->authorIsNotJcaf())
{
return false;
}
if ($mission->isNotJcaf())
{
return false;
}
// Mission should be shared
if ($mission->isNotShared())
{
return false;
}
// The mission should have a finished RFI not requiring another intervention
$docs = $this->em->getRepository(Document::class)
->findByMission($mission);
if (count($docs) < 1)
{
return false;
}
$found = false;
foreach ($docs as $doc)
{
if ($doc->getRfi() !== null)
{
if ($doc->getRfi()->doesNotRequireReintervention())
{
$found = true;
break;
}
}
}
if (!$found)
{
return false;
}
// All looks good
return true;
}
private function canList(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Permission
$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_Permission
$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 canListAuthor(Access $user, AccessFunction $function)
{
// If canView, then canList ;)
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_AUTHOR);
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_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_SOCIETY);
$aclPermAuthor = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LIST_AUTHOR);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermAuthor === 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 it means that nothing good has been found
// Load third permission
if ($aclPermAuthor !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAuthor
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
return true;
}
}
}
// If we are here, all hope is lost
return false;
}
// Plan.io Task #4247
private function canMerge(Mission $mission, Access $user, AccessFunction $accessFunction)
{
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MERGE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MERGE_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' => $accessFunction,
'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' => $accessFunction,
'permission' => $aclPermSociety
));
if ($acl !== null)
{
if ($acl->getValue())
{
// A single positive answer is enough
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($mission, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
// Plan.io Task #4247
// We need to know if the user can access the merge tool
// Specific merge permissions will be checked later
private function canUseMergeTool(Access $user, AccessFunction $accessFunction)
{
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MERGE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_MERGE_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' => $accessFunction,
'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' => $accessFunction,
'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;
}
}