<?php
//----------------------------------------------------------------------
// src/Logging/Security/AclPlanningLog.php
//----------------------------------------------------------------------
namespace App\Logging\Security;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\Security\AclPlanning;
use App\Logging\Tools;
use App\Services\LogTools;
class AclPlanningLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, TranslatorInterface $translator, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->translator = $translator;
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(AclPlanning $acl)
{
if ($this->societyGroupIsNotLoggable($acl)) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($acl);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($acl, $loggingData);
//----------------------------------------------------------------------
$action = "acl_planning_change_permission";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(AclPlanning $acl, $changes)
{
if ($this->societyGroupIsNotLoggable($acl)) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($acl);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($acl, $loggingData);
//----------------------------------------------------------------------
$action = "acl_planning_change_permission";
$args["action"] = $action;
$access = $acl->getAccess()->getUsername();
$society = $acl->getSociety()->getRef();
foreach ($changes as $key => $change)
{
$before = $change[0];
$after = $change[1];
switch ($key)
{
case 'canAdd':
{
$type = $this->translator->trans('can_add');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
case 'canView':
{
$type = $this->translator->trans('can_view');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
case 'canViewOwn':
{
$type = $this->translator->trans('can_view_own');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
case 'canEdit':
{
$type = $this->translator->trans('can_edit');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
case 'canEditOwn':
{
$type = $this->translator->trans('can_edit_own');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
case 'canDelete':
{
$type = $this->translator->trans('can_delete');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
case 'canDeleteOwn':
{
$type = $this->translator->trans('can_delete_own');
$display = $access . " / " . $society . " / " . $type . " : ";
$args["object_display"] = $type;
$args["old_value"] = $display . ($before ? 'oui' : 'non');
$args["new_value"] = $display . ($after ? 'oui' : 'non');
break;
}
}
}
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logRemoval(AclPlanning $acl)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
// SocietyGroup creation => Objects are being created at the same time
// but the SocietyGroup itself has not been flushed
private function societyGroupIsNotLoggable(AclPlanning $acl)
{
$access = $acl->getAccess()->getUsername();
$society = $acl->getSociety();
$societyGroup = $society->getSocietyGroup();
if ($societyGroup === null || empty($societyGroup->getId()))
{
return true;
}
return false;
}
private function initArgs(AclPlanning $acl, $loggingData)
{
$access = $acl->getAccess()->getUsername();
$society = $acl->getSociety();
$societyGroup = $society->getSocietyGroup();
$args = array(
"object_id" => $acl->getId(),
"object_bundle" => "Security",
"object_entity" => "AclPlanning",
"object_display" => $loggingData['info'],
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}