<?php
//----------------------------------------------------------------------
// src/Logging/Security/AclLog.php
//----------------------------------------------------------------------
namespace App\Logging\Security;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Security\Acl;
use App\Logging\Tools;
use App\Services\LogTools;
class AclLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(Acl $acl)
{
if ($this->societyGroupIsNotLoggable($acl)) return [];
$pendingLogArgs = [];
$function = $acl->getFunction();
if ($function === null) 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_change_permission";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Acl $acl, $changes)
{
if ($this->societyGroupIsNotLoggable($acl)) return [];
$pendingLogArgs = [];
$function = $acl->getFunction();
if ($function === null) 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_change_permission";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logRemoval(Acl $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(Acl $acl)
{
$user = $this->logTools->getCurrentUser();
$societyGroup = $user->getSocietyGroup();
if ($societyGroup === null || empty($societyGroup->getId()))
{
return true;
}
return false;
}
private function initArgs(Acl $acl, $loggingData)
{
$user = $this->logTools->getCurrentUser();
$societyGroup = $user->getSocietyGroup();
$function = $acl->getFunction()->getName();
$functionDisplay = $function . " : ";
if ($acl->getValue())
{
$oldValue = "non";
$newValue = "oui";
}
else
{
$oldValue = "oui";
$newValue = "non";
}
$args = array(
"object_id" => $acl->getId(),
"object_bundle" => "Security",
"object_entity" => "Acl",
"object_display" => $acl->getPermission()->getTranslation(),
"old_value" => $functionDisplay . $oldValue,
"new_value" => $functionDisplay . $newValue,
"society_group" => $societyGroup,
"society" => null,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}