<?php
//------------------------------------------------------------------------------
// src/Security/RHFormVoter.php
//------------------------------------------------------------------------------
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\HR\AccessFunction;
use App\Entity\HR\RHForm\RHForm;
use App\Entity\Platform\Society;
use App\Entity\Security\Acl;
use App\Entity\Security\AclPermission;
use App\Services\Config\ModuleTools;
class RHFormVoter extends Voter
{
//--------------------------------------------------------------------------------
// is_granted special constant
const IS_ACTIVE = "rhform_is_active";
//--------------------------------------------------------------------------------
// is_granted constants
const ADD = "add_rh_form";
const ADD_FOR_OTHERS = "add_rh_form_for_others";
const ADD_ANY = "add_rh_form_any";
const ADD_ALL = "add_rh_form_all"; // Plan.io Task #462
const LISTING = "list_rh_forms";
const LISTING_SOCIETY = "list_rh_forms_society";
const LISTING_OWN = "list_rh_forms_own";
const LISTING_ANY = "list_rh_forms_any";
const LISTING_OTHER_THAN_OWN = "list_rh_forms_other_than_own";
const VIEW = "view_rh_form";
const EDIT = "edit_rh_form";
const EDIT_STATUS = "edit_rh_form_status";
const EDIT_DATES = "edit_rh_form_dates";
const EDIT_HOURS = "edit_rh_form_hours";
const DELETE = "delete_rh_form";
const IS_GRANTED_CONSTANTS = array(
self::IS_ACTIVE,
self::ADD,
self::ADD_FOR_OTHERS,
self::ADD_ANY,
self::ADD_ALL,
self::LISTING,
self::LISTING_SOCIETY,
self::LISTING_OWN,
self::LISTING_ANY,
self::LISTING_OTHER_THAN_OWN,
self::VIEW,
self::EDIT,
self::EDIT_STATUS,
self::EDIT_DATES,
self::EDIT_HOURS,
self::DELETE,
);
//--------------------------------------------------------------------------------
// acl constants
const ACL_PERM_ADD = "rh_form_add";
const ACL_PERM_ADD_FOR_OTHERS = "rh_form_add_for_others";
const ACL_PERM_ADD_ALL = "rh_form_add_all";
const ACL_PERM_LISTING = "rh_form_list";
const ACL_PERM_LISTING_SOCIETY = "rh_form_list_society";
const ACL_PERM_LISTING_OWN = "rh_form_list_own";
const ACL_PERM_VIEW = "rh_form_view";
const ACL_PERM_VIEW_SOCIETY = "rh_form_view_society";
const ACL_PERM_VIEW_OWN = "rh_form_view_own";
const ACL_PERM_EDIT = "rh_form_edit";
const ACL_PERM_EDIT_SOCIETY = "rh_form_edit_society";
const ACL_PERM_EDIT_OWN = "rh_form_edit_own";
const ACL_PERM_EDIT_STATUS = "rh_form_edit_status";
const ACL_PERM_EDIT_STATUS_SOCIETY = "rh_form_edit_status_society";
const ACL_PERM_EDIT_DATES = "rh_form_edit_dates";
const ACL_PERM_EDIT_DATES_SOCIETY = "rh_form_edit_dates_society";
const ACL_PERM_EDIT_HOURS = "rh_form_hours_edit";
const ACL_PERM_EDIT_HOURS_SOCIETY = "rh_form_hours_edit_society";
//--------------------------------------------------------------------------------
public function __construct(ManagerRegistry $doctrine, ModuleTools $moduleTools)
{
$this->em = $doctrine->getManager();
$this->moduleTools = $moduleTools;
$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 = null): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
{
return false;
}
// only vote on RHForm objects inside this voter
if ($subject !== null && !$subject instanceof RHForm)
{
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
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;
// Module activated ?
if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_RHFORM))
{
return false;
}
// you know $subject is a RHForm object, thanks to supports
/** @var RHForm $rhForm */
$rhForm = $subject;
// Check current group affectation
// Exception : own forms
if ($subject !== null && $subject->getAccess() !== null && !$subject->getAccess()->equals($user))
{
$subjectSociety = $subject->getSociety();
if ($subjectSociety === null)
return false;
$subjectGroup = $subjectSociety->getGroup();
if ($subjectGroup === null)
return false;
if (!$currentGroup->equals($subjectGroup))
return false;
}
switch ($attribute)
{
// This is handled in the voteOnAttribute function,
// if we are here it means that the rhforms option is active
case self::IS_ACTIVE:
return true;
case self::ADD:
return $this->canAdd($user, $function);
case self::ADD_FOR_OTHERS:
return $this->canAddForOthers($user, $function);
case self::ADD_ANY:
return $this->canAddAny($user, $function);
case self::ADD_ALL:
return $this->canAddAll($user, $function);
case self::LISTING:
return $this->canList($user, $function);
case self::LISTING_SOCIETY:
return $this->canListSociety($user, $function);
case self::LISTING_OWN:
return $this->canListOwn($user, $function);
case self::LISTING_ANY:
return $this->canListAny($user, $function);
case self::LISTING_OTHER_THAN_OWN:
return $this->canListOtherThanOwn($user, $function);
case self::VIEW:
return $this->canView($rhForm, $user, $function);
case self::EDIT:
return $this->canEdit($rhForm, $user, $function);
case self::EDIT_STATUS:
return $this->canEditStatus($rhForm, $user, $function);
case self::EDIT_DATES:
return $this->canEditDates($rhForm, $user, $function);
case self::EDIT_HOURS:
return $this->canEditHours($rhForm, $user, $function);
case self::DELETE:
return $this->canDelete($rhForm, $user, $function);
}
throw new \LogicException('This code should not be reached!');
}
// $access is the user trying to load the resource
// $rhForm is the resource being loaded
// Check if the Society of the resource
// belongs to the societies of the $access
private function checkSociety(RHForm $rhForm, Access $access)
{
// Get all the societies of the access
$societies = $access->getSocieties();
// Get the Society of the RHForm
$rhFormSociety = $rhForm->getSociety();
if ($rhFormSociety === null)
return false;
$found = false;
foreach ($societies as $society)
{
if ($society->getId() == $rhFormSociety->getId())
{
$found = true;
break;
}
}
return $found;
}
// Check if the $access is the author / access of the $rhForm
private function checkOwn(RHForm $rhForm, Access $access)
{
// Get author
$author = $rhForm->getAuthor();
if ($author === null)
return false;
if ($author->getId() === $access->getId())
return true;
// Get access
$theOne = $rhForm->getAccess();
if ($theOne === null)
return false;
if ($theOne->getId() === $access->getId())
return true;
return false;
}
private function canAdd(Access $user, AccessFunction $function)
{
// 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 canAddForOthers(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_FOR_OTHERS);
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 canAddAll(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_ALL);
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 canAddAny(Access $user, AccessFunction $function)
{
// Three Acl_Permission may exist
// The third one was added for Plan.io Task #4239
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD);
$aclPermOthers = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_FOR_OTHERS);
$aclPermAll = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ADD_ALL);
// If all are null, exit
if ($aclPerm === null && $aclPermOthers === null && $aclPermAll === 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 ($aclPermOthers !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermOthers
));
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 ($aclPermAll !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermAll
));
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 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 canListOwn(Access $user, AccessFunction $function)
{
// Get Acl_Permission
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_OWN);
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)
{
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_SOCIETY);
$aclPermOwn = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_OWN);
// If both are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermOwn === 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 ($aclPermOwn !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermOwn
));
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 canListOtherThanOwn(Access $user, AccessFunction $function)
{
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_LISTING_SOCIETY);
// If both 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 canView(RHForm $rhForm, Access $user, AccessFunction $function)
{
// Three Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_SOCIETY);
$aclPermOwn = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_OWN);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermOwn === 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($rhForm, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermOwn !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermOwn
));
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->checkOwn($rhForm, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEdit(RHForm $rhForm, Access $user, AccessFunction $function)
{
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_SOCIETY);
$aclPermOwn = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_OWN);
// If all are null, exit
if ($aclPerm === null && $aclPermSociety === null && $aclPermOwn === 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($rhForm, $user);
}
}
}
// If we are here it means that nothing good has been found
// Load third permission
if ($aclPermOwn !== null)
{
$acl = $this->aclRepository->findOneBy(array(
'function' => $function,
'permission' => $aclPermOwn
));
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->checkOwn($rhForm, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditStatus(RHForm $rhForm, Access $user, AccessFunction $function)
{
// If annuled no one can edit sttaus
if ($rhForm->isAnnulled())
return false;
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_STATUS);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_STATUS_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($rhForm, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditDates(RHForm $rhForm, Access $user, AccessFunction $function)
{
// If waiting validation, everyone can edit dates
if ($rhForm->isWaitingValidation())
return true;
// If closed or annuled no one can edit dates
if ($rhForm->isPartiallyClosed() || $rhForm->isClosed() || $rhForm->isAnnulled())
return false;
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_DATES);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_DATES_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($rhForm, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canEditHours(RHForm $rhForm, Access $user, AccessFunction $function)
{
// Only available for RHForm_Hours
if ($rhForm->isHours() === false)
return false;
// Two Acl_Permission may exist
$aclPerm = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_HOURS);
$aclPermSociety = $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT_HOURS_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($rhForm, $user);
}
}
}
// If we are here, all hope is lost
return false;
}
private function canDelete(RHForm $rhForm, Access $user, AccessFunction $function)
{
return false;
}
}