<?php
//----------------------------------------------------------------------
// src/Logging/AccessLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Logging\Tools;
use App\Services\LogTools;
class AccessLog
{
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(Access $access)
{
$pendingLogArgs = [];
// Skip anything without ROLE_USER
if (!$access->isUser()) return [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($access);
$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($access, $loggingData);
//----------------------------------------------------------------------
$action = "access_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Access $access, $changes)
{
// Skip anything without ROLE_USER
if (!$access->isUser()) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($access);
$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($access, $loggingData);
//----------------------------------------------------------------------
$action = "access_edit";
// if ($info == 'lost_pwd')
// {
// $args['action'] = "access_request_lost_pwd_code";
// $args['info'] = null;
// $pendingLogArgs = $args;
// // return $pendingLogArgs;
// }
// This is triggered when we are actually changing the connected Address object
// The changes in the actual object are handled in AddressLog
$addressChanges = array(
);
$basicChanges = array(
"password",
"lostPwdCode",
);
$basicBoolChanges = array(
"isActive",
);
$dateChanges = array(
"activationDate",
);
$labelChanges = array(
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"function",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
// Something to skip ?
if (false)
{
continue;
}
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
if (in_array($key, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicChanges))
{
$args = $this->tools->handleBasicChanges($args, $before, $after);
if ($key == "password")
{
$args['old_value'] = null;
$args['new_value'] = null;
}
$pendingLogArgs[] = $args;
continue;
}
if (in_array($key, $basicBoolChanges))
{
$args = $this->tools->handleBasicBoolChanges($args, $before, $after);
if ($key == "isActive")
{
if (!$before && $after) $args['action'] = "access_activate";
if ($before && !$after) $args['action'] = "access_deactivate";
$args['old_value'] = null;
$args['new_value'] = null;
}
$pendingLogArgs[] = $args;
continue;
}
if (in_array($key, $dateChanges))
{
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
continue;
}
if (in_array($key, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
}
// Handle ManyToMany
if ($access->getSocieties()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($access->getSocieties()->getSnapshot() as $society)
{
$oldData .= $society->display();
$oldData .= ", ";
}
foreach ($access->getSocieties() as $society)
{
$newData .= $society->display();
$newData .= ", ";
}
if ($oldData != "")
{
$oldData = substr($oldData, 0, -1);
$oldData = substr($oldData, 0, -1);
}
if ($newData != "")
{
$newData = substr($newData, 0, -1);
$newData = substr($newData, 0, -1);
}
$args["action"] = "access_edit_societies";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
if ($access->getPlanningResources()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($access->getPlanningResources()->getSnapshot() as $planningResource)
{
$oldData .= $planningResource->display();
$oldData .= ", ";
}
foreach ($access->getPlanningResources() as $planningResource)
{
$newData .= $planningResource->display();
$newData .= ", ";
}
if ($oldData != "")
{
$oldData = substr($oldData, 0, -1);
$oldData = substr($oldData, 0, -1);
}
if ($newData != "")
{
$newData = substr($newData, 0, -1);
$newData = substr($newData, 0, -1);
}
$args["action"] = "access_edit_planning_resources";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Access $access)
{
// Skip anything without ROLE_USER
if (!$access->isUser()) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($access);
$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($access, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "access_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Access $access, $loggingData)
{
// Returns the Society of the Access.humanResource
$society = $access->getSociety();
$societyGroup = $access->getSocietyGroup();
// Add HumanResource logging
$humanResource = $access->getHumanResource();
$args = array(
"object_id" => $access->getId(),
"object_bundle" => "HR",
"object_entity" => "Access",
"object_display" => $access->display(),
"object_human_resource_id" => $humanResource !== null ? $humanResource->getId() : null,
"object_human_resource_display" => $humanResource !== null ? $humanResource->display() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}