src/Logging/Activity/AccessLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/AccessLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class AccessLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.         $this->tools $tools;
  18.     }
  19.     public function logCreation(Access $access)
  20.     {
  21.         $pendingLogArgs = [];
  22.         // Skip anything without ROLE_USER
  23.         if (!$access->isUser()) return [];
  24.         //----------------------------------------------------------------------
  25.         // Fetch eventual info stored in the object
  26.         $loggingData $this->logTools->handleLoggingData($access);
  27.         $info $loggingData['info'];
  28.         $specialAuthor $loggingData['special_author'];
  29.         $ignore $loggingData['ignore'];
  30.         if ($ignore) return $pendingLogArgs;
  31.         //----------------------------------------------------------------------
  32.         // Init base log args
  33.         // This does not contain action
  34.         $args $this->initArgs($access$loggingData);
  35.         //----------------------------------------------------------------------
  36.         $action "access_add";
  37.         $args["action"] = $action;
  38.         $pendingLogArgs[] = $args;
  39.         return $pendingLogArgs;
  40.     }
  41.     public function logChanges(Access $access$changes)
  42.     {
  43.         // Skip anything without ROLE_USER
  44.         if (!$access->isUser()) return [];
  45.         $pendingLogArgs = [];
  46.         //----------------------------------------------------------------------
  47.         // Fetch eventual info skeletond in the object
  48.         $loggingData $this->logTools->handleLoggingData($access);
  49.         $info $loggingData['info'];
  50.         $specialAuthor $loggingData['special_author'];
  51.         $ignore $loggingData['ignore'];
  52.         if ($ignore) return $pendingLogArgs;
  53.         //----------------------------------------------------------------------
  54.         // Init base log args
  55.         // This does not contain action
  56.         $args $this->initArgs($access$loggingData);
  57.         //----------------------------------------------------------------------
  58.         $action "access_edit";
  59.         // if ($info == 'lost_pwd')
  60.         // {
  61.         //     $args['action'] = "access_request_lost_pwd_code";
  62.         //     $args['info'] = null;
  63.         //     $pendingLogArgs = $args;
  64.         //     // return $pendingLogArgs;
  65.         // }
  66.         // This is triggered when we are actually changing the connected Address object
  67.         // The changes in the actual object are handled in AddressLog
  68.         $addressChanges = array(
  69.         );
  70.         $basicChanges = array(
  71.             "password",
  72.             "lostPwdCode",
  73.         );
  74.         $basicBoolChanges = array(
  75.             "isActive",
  76.         );
  77.         $dateChanges = array(
  78.             "activationDate",
  79.         );
  80.         $labelChanges = array(
  81.         );
  82.         // Method getLogLabel() should be defined for these objects/entities
  83.         $objectChanges = array(
  84.             "function",
  85.         );
  86.         // See what changed and log (members first)
  87.         foreach ($changes as $key => $change)
  88.         {
  89.             // Something to skip ?
  90.             if (false)
  91.             {
  92.                 continue;
  93.             }
  94.             $name $this->logTools->camelToSnakeCase($key);
  95.             $args["action"]    = $action."_".$name;
  96.             $before $change[0];
  97.             $after $change[1];
  98.             if (in_array($key$addressChanges))
  99.             {
  100.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  101.                 continue;
  102.             }
  103.             if (in_array($key$basicChanges))
  104.             {
  105.                 $args $this->tools->handleBasicChanges($args$before$after);
  106.                 if ($key == "password")
  107.                 {
  108.                     $args['old_value'] = null;
  109.                     $args['new_value'] = null;
  110.                 }
  111.                 $pendingLogArgs[] = $args;
  112.                 continue;
  113.             }
  114.             if (in_array($key$basicBoolChanges))
  115.             {
  116.                 $args $this->tools->handleBasicBoolChanges($args$before$after);
  117.                 if ($key == "isActive")
  118.                 {
  119.                     if (!$before && $after$args['action'] = "access_activate";
  120.                     if ($before && !$after$args['action'] = "access_deactivate";
  121.                     $args['old_value'] = null;
  122.                     $args['new_value'] = null;
  123.                 }
  124.                 $pendingLogArgs[] = $args;
  125.                 continue;
  126.             }
  127.             if (in_array($key$dateChanges))
  128.             {
  129.                 // dateChanges can be null
  130.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  131.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  132.                 continue;
  133.             }
  134.             if (in_array($key$labelChanges))
  135.             {
  136.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  137.                 continue;
  138.             }
  139.             if (in_array($key$objectChanges))
  140.             {
  141.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  142.                 continue;
  143.             }
  144.         }
  145.         // Handle ManyToMany
  146.         if ($access->getSocieties()->isDirty())
  147.         {
  148.             $oldData "";
  149.             $newData "";
  150.             foreach ($access->getSocieties()->getSnapshot() as $society)
  151.             {
  152.                 $oldData .= $society->display();
  153.                 $oldData .= ", ";
  154.             }
  155.             foreach ($access->getSocieties() as $society)
  156.             {
  157.                 $newData .= $society->display();
  158.                 $newData .= ", ";
  159.             }
  160.             if ($oldData != "")
  161.             {
  162.                 $oldData substr($oldData0, -1);
  163.                 $oldData substr($oldData0, -1);
  164.             }
  165.             if ($newData != "")
  166.             {
  167.                 $newData substr($newData0, -1);
  168.                 $newData substr($newData0, -1);
  169.             }
  170.             $args["action"] = "access_edit_societies";
  171.             $args["old_value"] = $oldData;
  172.             $args["new_value"] = $newData;
  173.             $pendingLogArgs[] = $args;
  174.         }
  175.         if ($access->getPlanningResources()->isDirty())
  176.         {
  177.             $oldData "";
  178.             $newData "";
  179.             foreach ($access->getPlanningResources()->getSnapshot() as $planningResource)
  180.             {
  181.                 $oldData .= $planningResource->display();
  182.                 $oldData .= ", ";
  183.             }
  184.             foreach ($access->getPlanningResources() as $planningResource)
  185.             {
  186.                 $newData .= $planningResource->display();
  187.                 $newData .= ", ";
  188.             }
  189.             if ($oldData != "")
  190.             {
  191.                 $oldData substr($oldData0, -1);
  192.                 $oldData substr($oldData0, -1);
  193.             }
  194.             if ($newData != "")
  195.             {
  196.                 $newData substr($newData0, -1);
  197.                 $newData substr($newData0, -1);
  198.             }
  199.             $args["action"] = "access_edit_planning_resources";
  200.             $args["old_value"] = $oldData;
  201.             $args["new_value"] = $newData;
  202.             $pendingLogArgs[] = $args;
  203.         }
  204.         return $pendingLogArgs;
  205.     }
  206.     public function logRemoval(Access $access)
  207.     {
  208.         // Skip anything without ROLE_USER
  209.         if (!$access->isUser()) return [];
  210.         $pendingLogArgs = [];
  211.         //----------------------------------------------------------------------
  212.         // Fetch eventual info skeletond in the object
  213.         $loggingData $this->logTools->handleLoggingData($access);
  214.         $info $loggingData['info'];
  215.         $specialAuthor $loggingData['special_author'];
  216.         $ignore $loggingData['ignore'];
  217.         if ($ignore) return $pendingLogArgs;
  218.         //----------------------------------------------------------------------
  219.         // Init base log args
  220.         // This does not contain action
  221.         $args $this->initArgs($access$loggingData);
  222.         //----------------------------------------------------------------------
  223.         $args["action"] = "access_delete";
  224.         $pendingLogArgs[] = $args;
  225.         return $pendingLogArgs;
  226.     }
  227.     private function initArgs(Access $access$loggingData)
  228.     {
  229.         // Returns the Society of the Access.humanResource
  230.         $society $access->getSociety();
  231.         $societyGroup $access->getSocietyGroup();
  232.         // Add HumanResource logging
  233.         $humanResource $access->getHumanResource();
  234.         $args = array(
  235.             "object_id"                    =>    $access->getId(),
  236.             "object_bundle"                =>    "HR",
  237.             "object_entity"                =>    "Access",
  238.             "object_display"            =>    $access->display(),
  239.             "object_human_resource_id"        =>    $humanResource !== null $humanResource->getId() : null,
  240.             "object_human_resource_display"    =>    $humanResource !== null $humanResource->display() : null,
  241.             
  242.             "society_group"                =>    $societyGroup,
  243.             "society"                    =>    $society,
  244.             "info"                        =>    $loggingData['info'],
  245.             "special_author"            =>    $loggingData['special_author'],
  246.         );
  247.         return $args;
  248.     }
  249. }