<?php
//------------------------------------------------------------------------------
// src/Security/DevisVoter.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\Config\Config;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\Planning\Task;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Mission\Mission;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\LogTools;
use App\Services\Config\ModuleTools;
use App\Services\Config\OptionConfigTools;
class DevisVoter extends Voter
{
// For now manager = author (both)
//--------------------------------------------------------------------------------
// is_granted constants
const IS_ACTIVE = "devis_is_active";
const ADD = "add_devis";
// Plan.io Task #3605
const ADD_GHOST = "add_ghost_devis";
const EDIT_GHOST = "edit_ghost_devis";
// Plan.io Task #3633
// This is to allow users to manually update the products of a ghost devis
// without actually modifying the original devis
const UPDATE_GHOST = "update_ghost_devis";
const LISTING = "list_devis";
const LISTING_SOCIETY = "list_devis_society";
const LISTING_MANAGER = "list_devis_manager";
const LISTING_ANY = "list_devis_any";
const VIEW = "view_devis";
const VIEW_PDF_HT_INTERNAL = "view_pdf_devis_ht_internal";
const VIEW_PDF_HT_PUBLIC = "view_pdf_devis_ht_public";
const VIEW_PDF_TTC_PUBLIC = "view_pdf_devis_ttc_public";
const VIEW_PDF_NO_PRICE = "view_pdf_devis_no_price";
const VIEW_PDF_WITH_PRICE = "view_pdf_devis_with_price";
const EDIT = "edit_devis";
const DELETE = "delete_devis";
const ANNUL = "annul_devis";
const REVIVE = "revive_devis";
// Plan.io Task #3621
const EDIT_IKEA_OS_FROM_TASK = "edit_devis_ikea_order_number_from_task";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_MANAGER,
self::LISTING_ANY,
self::VIEW,
self::VIEW_PDF_HT_INTERNAL,
self::VIEW_PDF_HT_PUBLIC,
self::VIEW_PDF_TTC_PUBLIC,
self::VIEW_PDF_NO_PRICE,
self::VIEW_PDF_WITH_PRICE,
self::EDIT,
self::DELETE,
self::ANNUL,
self::REVIVE,
// Plan.io Task #3605
self::ADD_GHOST,
self::EDIT_GHOST,
// Plan.io Task #3633
self::UPDATE_GHOST,
// Plan.io Task #3621
self::EDIT_IKEA_OS_FROM_TASK,
);
const IS_GRANTED_CONSTANTS_SHARING_EXCEPTION = array(
self::VIEW,
self::VIEW_PDF_HT_INTERNAL,
self::VIEW_PDF_HT_PUBLIC,
self::VIEW_PDF_TTC_PUBLIC,
self::VIEW_PDF_NO_PRICE,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "devis_add";
// Plan.io Task #3633
const ACL_PERM_UPDATE_GHOST = "ghost_devis_update";
const ACL_PERM_LISTING = "devis_list";
const ACL_PERM_LISTING_SOCIETY = "devis_list_society";
const ACL_PERM_LISTING_MANAGER = "devis_list_manager";
const ACL_PERM_VIEW = "devis_view";
const ACL_PERM_VIEW_SOCIETY = "devis_view_society";
const ACL_PERM_VIEW_MANAGER = "devis_view_manager";
const ACL_PERM_VIEW_CLIENT_MANAGER = "devis_view_ind_manager";
const ACL_PERM_VIEW_IF_TASK = "devis_view_if_task";
const ACL_PERM_EDIT = "devis_edit";
const ACL_PERM_EDIT_SOCIETY = "devis_edit_society";
const ACL_PERM_EDIT_MANAGER = "devis_edit_manager";
const ACL_PERM_EDIT_CLIENT_MANAGER = "devis_edit_ind_manager";
const ACL_PERM_EDIT_IF_TASK = "devis_edit_if_task";
const ACL_PERM_ANNUL = "devis_annul";
const ACL_PERM_ANNUL_SOCIETY = "devis_annul_society";
const ACL_PERM_REVIVE = "devis_revive";
const ACL_PERM_REVIVE_SOCIETY = "devis_revive_society";
const ACL_PERM_VIEW_PDF_HT_INTERNAL = "devis_view_pdf_ht_internal";
const ACL_PERM_VIEW_PDF_HT_INTERNAL_SOCIETY = "devis_view_pdf_ht_internal_society";
const ACL_PERM_VIEW_PDF_HT_INTERNAL_MANAGER = "devis_view_pdf_ht_internal_manager";
const ACL_PERM_VIEW_PDF_HT_INTERNAL_CLIENT_MANAGER = "devis_view_pdf_ht_internal_ind_manager";
const ACL_PERM_VIEW_PDF_HT_PUBLIC = "devis_view_pdf_ht_public";
const ACL_PERM_VIEW_PDF_HT_PUBLIC_SOCIETY = "devis_view_pdf_ht_public_society";
const ACL_PERM_VIEW_PDF_HT_PUBLIC_MANAGER = "devis_view_pdf_ht_public_manager";
const ACL_PERM_VIEW_PDF_HT_PUBLIC_CLIENT_MANAGER = "devis_view_pdf_ht_public_ind_manager";
const ACL_PERM_VIEW_PDF_TTC_PUBLIC = "devis_view_pdf_ttc_public";
const ACL_PERM_VIEW_PDF_TTC_PUBLIC_SOCIETY = "devis_view_pdf_ttc_public_society";
const ACL_PERM_VIEW_PDF_TTC_PUBLIC_MANAGER = "devis_view_pdf_ttc_public_manager";
const ACL_PERM_VIEW_PDF_TTC_PUBLIC_CLIENT_MANAGER = "devis_view_pdf_ttc_public_ind_manager";
const ACL_PERM_VIEW_PDF_NO_PRICE = "devis_view_pdf_no_price_public";
const ACL_PERM_VIEW_PDF_NO_PRICE_SOCIETY = "devis_view_pdf_no_price_public_society";
const ACL_PERM_VIEW_PDF_NO_PRICE_MANAGER = "devis_view_pdf_no_price_public_manager";
const ACL_PERM_VIEW_PDF_NO_PRICE_CLIENT_MANAGER = "devis_view_pdf_no_price_public_ind_manager";
//--------------------------------------------------------------------------------
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, ManagerRegistry $doctrine, ModuleTools $moduleTools, OptionConfigTools $optionConfigTools, LogTools $logTools)
{
$this->accessDecisionManager = $accessDecisionManager;
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$this->optionConfigTools = $optionConfigTools;
$this->logTools = $logTools;
$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 Devis and Mission objects inside this voter
if ($subject !== null && !($subject instanceof Devis || $subject instanceof Mission))
{
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_DEVIS))
{
return false;
}
// Required modules : Misison, Product
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;
}
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_PRODUCT))
{
return false;
}
$devis = null;
$mission = null;
if ($subject instanceof Devis)
{
/** @var Devis $devis */
$devis = $subject;
// Check current group affectation
// Make an exception for "view/pdf" : take sharing into account
if ($devis !== null)
{
$devisSociety = $devis->getSociety();
if ($devisSociety === null)
return false;
$devisSocietyGroup = $devisSociety->getGroup();
if ($devisSocietyGroup === null)
return false;
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS_SHARING_EXCEPTION))
{
// Just check currentGroup
if (!$currentGroup->equals($devisSocietyGroup))
return false;
}
else
{
// Take sharing into account
// If the devis belongs to a mission that was created by the currentGroup
// (devis.mission.societyGroupAuthor == currentGroup)
// Then it can see the devis
if (!$currentGroup->equals($devisSocietyGroup))
{
if ($devis->getMission() !== null)
{
$missionSocietyGroupAuthor = $devis->getMission()->getSocietyGroupAuthor();
if (!$currentGroup->equals($missionSocietyGroupAuthor))
{
return false;
}
}
}
}
}
}
else
{
if ($subject instanceof Mission)
{
/** @var Mission $mission */
$mission = $subject;
// Plan.io Task #3517
// Adding a devis counts as editing a mission
// So check if the current user has access to this mission
// If this is too restrictive, change 'edit_mission' with 'view_mission'
// 'view_mission' is better ;)
if (!$this->accessDecisionManager->decide($token, ['view_mission'], $mission))
{
return false;
}
}
}
switch ($attribute)
{
case self::IS_ACTIVE:
return true;
// We are only adding devis for missions, so this makes sense
case self::ADD:
return $this->canAdd($mission, $user, $function);
// Plan.io Task #3605
case self::ADD_GHOST:
return $this->canAddGhost($devis, $user, $function);
case self::EDIT_GHOST:
return $this->canEditGhost($devis, $user, $function);
// Plan.io Task #3633
case self::UPDATE_GHOST:
return $this->canUpdateGhost($devis, $user, $function);
case self::LISTING:
return $this->canList($user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_MANAGER:
return $this->canListManager($user, $function);
case self::LISTING_ANY:
return $this->canListAny($user, $function);
case self::VIEW:
return $this->canView($devis, $user, $function);
case self::VIEW_PDF_HT_INTERNAL:
return $this->canViewPdfHtInternal($devis, $user, $function);
case self::VIEW_PDF_HT_PUBLIC:
return $this->canViewPdfHtPublic($devis, $user, $function);
case self::VIEW_PDF_TTC_PUBLIC:
return $this->canViewPdfTtcPublic($devis, $user, $function);
case self::VIEW_PDF_NO_PRICE:
return $this->canViewPdfNoPrice($devis, $user, $function);
case self::VIEW_PDF_WITH_PRICE:
return $this->canViewPdfWithPrice($devis, $user, $function);
case self::EDIT:
return $this->canEdit($devis, $user, $function, $currentGroup);
case self::DELETE:
return $this->canDelete($devis, $user, $function);
case self::ANNUL:
return $this->canAnnul($devis, $user, $function, $currentGroup);
case self::REVIVE:
return $this->canRevive($devis, $user, $function, $currentGroup);
// Plan.io Task #3621
case self::EDIT_IKEA_OS_FROM_TASK:
return $this->canEditIkeaDataFromTask($devis, $user, $function, $currentGroup);
}
throw new \LogicException('This code should not be reached!');
}
// $access is the user trying to load the resource
// $devis is the resource being loaded
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkSociety(Devis $devis, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the Devis
$devisSociety = $devis->getSociety();
if ($devisSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $devisSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
// Check if the $access is the manager / author of the $devis
private function checkManager(Devis $devis, Access $access)
{
// Get manager
$manager = $devis->getManager();
$author = $devis->getAuthor();
if ($manager === null && $author === null)
return false;
if ($manager !== null)
if ($manager->getId() === $access->getId())
return true;
if ($author !== null)
if ($author->getId() === $access->getId())
return true;
return false;
}
// Check if the $access is the manager of the $devis
private function checkClientManager(Devis $devis, Access $access)
{
// Get Client
$client = $devis->getReceiver();
if ($client === null)
return false;
if ($client->getIndividual() !== null)
{
// Only Individuals have managers
// Get manager
$manager = $devis->getReceiver()->getIndividual()->getManager();
if ($manager === null)
return false;
if ($manager->getId() === $access->getId())
return true;
}
return false;
}
// Check if there is at least one task
// having this access as resource and this devis
private function checkTask(Devis $devis, Access $access)
{
$taskRep = $this->em->getRepository(Task::class);
$resources = $access->getPlanningResources();
foreach ($resources as $r)
{
$tasks = $taskRep->findForDevisAndResource($devis, $r);
if (count($tasks) > 0)
return true;
}
return false;
}
private function canAdd(Mission $mission, Access $user, AccessFunction $function)
{
// Deny actions on archivedRefused objects
if ($mission->isArchivedRefused())
{
return false;
}
// If the mission is shared and the author is the current group, deny adding devis
// When a mission is shared, the author cannot edit it
if ($mission->isShared())
{
if ($mission->isSharedBySocietyGroup($this->currentGroup))
{
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();
}
// Plan.io Task #3605
private function canAddGhost(Devis $devis, Access $user, AccessFunction $function)
{
if (!$this->optionConfigTools->isActive_GhostInvoicing($this->currentGroup))
{
return false;
}
if ($devis->getGhost() !== null)
{
return false;
}
if (!$this->canAdd($devis->getMission(), $user, $function))
{
return false;
}
$template = $devis->getTemplate();
if ($template === null)
{
return false;
}
if ($template->hasProductWithInvoicingProduct())
{
return true;
}
return false;
}
// Plan.io Task #3605
private function canEditGhost(Devis $devis, Access $user, AccessFunction $function)
{
if ($devis->getGhost() === null)
{
return false;
}
if (!$this->canEdit($devis, $user, $function, $this->currentGroup))
{
return false;
}
$template = $devis->getTemplate();
if ($template === null)
{
return false;
}
if ($template->hasProductWithInvoicingProduct())
{
return true;
}
return false;
}
// Plan.io Task #3633
private function canUpdateGhost(Devis $devis, Access $user, AccessFunction $function)
{
if ($devis->getGhost() === null)
{
return false;
}
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_UPDATE_GHOST);
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 canList(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
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)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_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
// Further filtering is done in the Controller
return $acl->getValue();
}
private function canListManager(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_MANAGER);
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
// Further filtering is done in the Controller
return $acl->getValue();
}
private function canListAny(Access $user, AccessFunction $function)
{
// Three Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === 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 ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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 canView(Devis $devis = null, Access $user, AccessFunction $function)
{
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_MANAGER);
$aclPermClientManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_CLIENT_MANAGER);
$aclPermIfTask = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_IF_TASK);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === null && $aclPermIfTask === null)
return false;
// The aclPermIfTask should be checked first, and if it is false, the others should be cheked
// ($aclPerm ^ $aclPermSociety ^ $aclPermManager ^ $aclPermClientManager) = empty
// ($aclPerm ^ $aclPermSociety ^ $aclPermManager ^ $aclPermClientManager) ^ $aclPermIfTask = not empty
// This means that an user can have access to the devis of his society
// and also have access to the devis realted to his tasks
if ($aclPermIfTask !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermIfTask
));
if ($acl !== null)
{
if ($acl->getValue())
{
if ($this->checkTask($devis, $user))
return true;
}
}
}
// If we are here it means that the user doesn't have access to the Devis
// by the bias of one of his tasks
// So check regular permissions next
// 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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->checkManager($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermClientManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermClientManager
));
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->checkClientManager($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canViewPdfHtInternal(Devis $devis = null, Access $user, AccessFunction $function)
{
// Unfinished Simulations
if ($devis->getReceiver() === null)
return false;
// Four Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_INTERNAL);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_INTERNAL_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_INTERNAL_MANAGER);
$aclPermClientManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_INTERNAL_CLIENT_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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->checkManager($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermClientManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermClientManager
));
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->checkClientManager($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canViewPdfHtPublic(Devis $devis = null, Access $user, AccessFunction $function)
{
// Unfinished Simulations
if ($devis->getReceiver() === null)
return false;
// Four Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_PUBLIC);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_PUBLIC_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_PUBLIC_MANAGER);
$aclPermClientManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_HT_PUBLIC_CLIENT_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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->checkManager($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermClientManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermClientManager
));
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->checkClientManager($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canViewPdfTtcPublic(Devis $devis = null, Access $user, AccessFunction $function)
{
// Unfinished Simulations
// if ($devis->getReceiver() === null)
// return false;
// Four Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_TTC_PUBLIC);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_TTC_PUBLIC_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_TTC_PUBLIC_MANAGER);
$aclPermClientManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_TTC_PUBLIC_CLIENT_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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->checkManager($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermClientManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermClientManager
));
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->checkClientManager($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canViewPdfNoPrice(Devis $devis = null, Access $user, AccessFunction $function)
{
// Unfinished Simulations
// if ($devis->getReceiver() === null)
// return false;
// Four Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_NO_PRICE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_NO_PRICE_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_NO_PRICE_MANAGER);
$aclPermClientManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_NO_PRICE_CLIENT_MANAGER);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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->checkManager($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermClientManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermClientManager
));
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->checkClientManager($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
// Plan.io Task #3621
private function canEditConditions(Devis $devis)
{
// Plan.io Task #4329
if ($devis->hasDrafts())
{
return false;
}
// Plan.io Task #3793
if ($devis->hasSignature())
{
return false;
}
// Deny edit on archivedRefused objects
if ($devis->isArchivedRefused())
{
return false;
}
// Deny for an annulled devis
if ($devis->isAnnulled())
{
return false;
}
// Deny for a devis that is partially or totally invoiced
if ($devis->isInvoiced() || $devis->isPartiallyInvoiced())
{
return false;
}
// If the Devis has been sent to the Rekto Platform, deny edit
if ($devis->getRemoteId() !== null)
{
return false;
}
// Unfinished Simulations
if ($devis->getReceiver() === null)
{
return false;
}
return true;
}
private function canViewPdfWithPrice(Devis $devis = null, Access $user, AccessFunction $function)
{
if ($this->canViewPdfHtPublic($devis, $user, $function))
{
return true;
}
if ($this->canViewPdfHtPublic($devis, $user, $function))
{
return true;
}
if ($this->canViewPdfTtcPublic($devis, $user, $function))
{
return true;
}
// If we are here, all hope is lost
return false;
}
private function canEdit(Devis $devis = null, Access $user, AccessFunction $function, $currentGroup, $withConditions = true)
{
// Plan.io Task #3621
if ($withConditions)
{
if (!$this->canEditConditions($devis))
{
return false;
}
}
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_SOCIETY);
$aclPermManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_MANAGER);
$aclPermClientManager = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_CLIENT_MANAGER);
$aclPermIfTask = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_IF_TASK);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === null && $aclPermIfTask === null)
return false;
// The aclPermIfTask should be checked first, and if it is false, the others should be cheked
// ($aclPerm ^ $aclPermSociety ^ $aclPermManager ^ $aclPermClientManager) = empty
// ($aclPerm ^ $aclPermSociety ^ $aclPermManager ^ $aclPermClientManager) ^ $aclPermIfTask = not empty
// This means that an user can have access to the devis of his society
// and also have access to the devis realted to his tasks
if ($aclPermIfTask !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermIfTask
));
if ($acl !== null)
{
if ($acl->getValue())
{
if ($this->checkTask($devis, $user))
return true;
}
}
}
// If we are here it means that the user doesn't have access to the Devis
// by the bias of one of his tasks
// So check regular permissions next
// 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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermManager
));
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->checkManager($devis, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load fourth permission
if ($aclPermClientManager !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermClientManager
));
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->checkClientManager($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDelete(Devis $devis = null, Access $user, AccessFunction $function)
{
return false;
}
private function canAnnul(Devis $devis = null, Access $user, AccessFunction $function, $currentGroup)
{
// Plan.io Task #4329
if ($devis->hasDrafts())
{
return false;
}
// Deny for an annulled devis
if ($devis->isAnnulled())
return false;
// Deny for a devis that is partially or totally invoiced
if ($devis->isInvoiced() || $devis->isPartiallyInvoiced())
return false;
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ANNUL);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ANNUL_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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canRevive(Devis $devis = null, Access $user, AccessFunction $function, $currentGroup)
{
// Deny reviving a devis that is not annulled
if ($devis->isAnnulled() == false)
return false;
// Get Acl_Permissions
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_REVIVE);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_REVIVE_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
// In this case the good answer will be provided by the checkSociety
return $this->checkSociety($devis, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
// Plan.io Task #3621
private function canEditIkeaDataFromTask(Devis $devis, $user, $function, $currentGroup)
{
return $this->canEdit($devis, $user, $function, $currentGroup, false);
}
}