src/Logging/Security/AclPlanningLog.php line 20

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/Security/AclPlanningLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Security;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use App\Entity\Security\AclPlanning;
  9. use App\Logging\Tools;
  10. use App\Services\LogTools;
  11. class AclPlanningLog
  12. {
  13.     private array $pendingLogArgs = [];
  14.     public function __construct(ManagerRegistry $doctrineTranslatorInterface $translatorLogTools $logToolsTools $tools)
  15.     {
  16.         $this->em $doctrine->getManager();
  17.         $this->translator $translator;
  18.         $this->logTools $logTools;
  19.         $this->tools $tools;
  20.     }
  21.     public function logCreation(AclPlanning $acl)
  22.     {
  23.         if ($this->societyGroupIsNotLoggable($acl)) return [];
  24.         $pendingLogArgs = [];
  25.         //----------------------------------------------------------------------
  26.         // Fetch eventual info stored in the object
  27.         $loggingData $this->logTools->handleLoggingData($acl);
  28.         $info $loggingData['info'];
  29.         $specialAuthor $loggingData['special_author'];
  30.         $ignore $loggingData['ignore'];
  31.         if ($ignore) return $pendingLogArgs;
  32.         //----------------------------------------------------------------------
  33.         // Init base log args
  34.         // This does not contain action
  35.         $args $this->initArgs($acl$loggingData);
  36.         //----------------------------------------------------------------------
  37.         $action "acl_planning_change_permission";
  38.         $args["action"] = $action;
  39.         $pendingLogArgs[] = $args;
  40.         return $pendingLogArgs;
  41.     }
  42.     public function logChanges(AclPlanning $acl$changes)
  43.     {
  44.         if ($this->societyGroupIsNotLoggable($acl)) return [];
  45.         
  46.         $pendingLogArgs = [];
  47.         //----------------------------------------------------------------------
  48.         // Fetch eventual info skeletond in the object
  49.         $loggingData $this->logTools->handleLoggingData($acl);
  50.         $info $loggingData['info'];
  51.         $specialAuthor $loggingData['special_author'];
  52.         $ignore $loggingData['ignore'];
  53.         if ($ignore) return $pendingLogArgs;
  54.         //----------------------------------------------------------------------
  55.         // Init base log args
  56.         // This does not contain action
  57.         $args $this->initArgs($acl$loggingData);
  58.         //----------------------------------------------------------------------
  59.         $action "acl_planning_change_permission";
  60.         $args["action"] = $action;
  61.         $access $acl->getAccess()->getUsername();
  62.         $society $acl->getSociety()->getRef();
  63.         foreach ($changes as $key => $change)
  64.         {
  65.             $before $change[0];
  66.             $after $change[1];
  67.             switch ($key)
  68.             {
  69.                 case 'canAdd':
  70.                 {
  71.                     $type $this->translator->trans('can_add');
  72.                     $display $access " / " $society " / " $type " : ";
  73.                     $args["object_display"] = $type;
  74.                     $args["old_value"] = $display . ($before 'oui' 'non');
  75.                     $args["new_value"] = $display . ($after 'oui' 'non');
  76.                     break;
  77.                 }
  78.                 case 'canView':
  79.                 {
  80.                     $type $this->translator->trans('can_view');
  81.                     $display $access " / " $society " / " $type " : ";
  82.                     $args["object_display"] = $type;
  83.                     $args["old_value"] = $display . ($before 'oui' 'non');
  84.                     $args["new_value"] = $display . ($after 'oui' 'non');
  85.                     break;
  86.                 }
  87.                 case 'canViewOwn':
  88.                 {
  89.                     $type $this->translator->trans('can_view_own');
  90.                     $display $access " / " $society " / " $type " : ";
  91.                     $args["object_display"] = $type;
  92.                     $args["old_value"] = $display . ($before 'oui' 'non');
  93.                     $args["new_value"] = $display . ($after 'oui' 'non');
  94.                     break;
  95.                 }
  96.                 case 'canEdit':
  97.                 {
  98.                     $type $this->translator->trans('can_edit');
  99.                     $display $access " / " $society " / " $type " : ";
  100.                     $args["object_display"] = $type;
  101.                     $args["old_value"] = $display . ($before 'oui' 'non');
  102.                     $args["new_value"] = $display . ($after 'oui' 'non');
  103.                     break;
  104.                 }
  105.                 case 'canEditOwn':
  106.                 {
  107.                     $type $this->translator->trans('can_edit_own');
  108.                     $display $access " / " $society " / " $type " : ";
  109.                     $args["object_display"] = $type;
  110.                     $args["old_value"] = $display . ($before 'oui' 'non');
  111.                     $args["new_value"] = $display . ($after 'oui' 'non');
  112.                     break;
  113.                 }
  114.                 case 'canDelete':
  115.                 {
  116.                     $type $this->translator->trans('can_delete');
  117.                     $display $access " / " $society " / " $type " : ";
  118.                     $args["object_display"] = $type;
  119.                     $args["old_value"] = $display . ($before 'oui' 'non');
  120.                     $args["new_value"] = $display . ($after 'oui' 'non');
  121.                     break;
  122.                 }
  123.                 case 'canDeleteOwn':
  124.                 {
  125.                     $type $this->translator->trans('can_delete_own');
  126.                     $display $access " / " $society " / " $type " : ";
  127.                     $args["object_display"] = $type;
  128.                     $args["old_value"] = $display . ($before 'oui' 'non');
  129.                     $args["new_value"] = $display . ($after 'oui' 'non');
  130.                     break;
  131.                 }
  132.             }
  133.         }
  134.         $pendingLogArgs[] = $args;
  135.         return $pendingLogArgs;
  136.     }
  137.     public function logRemoval(AclPlanning $acl)
  138.     {
  139.         $pendingLogArgs = [];
  140.         return $pendingLogArgs;
  141.     }
  142.     // SocietyGroup creation => Objects are being created at the same time
  143.     // but the SocietyGroup itself has not been flushed
  144.     private function societyGroupIsNotLoggable(AclPlanning $acl)
  145.     {
  146.         $access $acl->getAccess()->getUsername();
  147.         $society $acl->getSociety();
  148.         $societyGroup $society->getSocietyGroup();
  149.         if ($societyGroup === null || empty($societyGroup->getId()))
  150.         {
  151.             return true;
  152.         }
  153.         return false;
  154.     }
  155.     private function initArgs(AclPlanning $acl$loggingData)
  156.     {
  157.         $access $acl->getAccess()->getUsername();
  158.         $society $acl->getSociety();
  159.         $societyGroup $society->getSocietyGroup();
  160.         $args = array(
  161.             "object_id"                    =>    $acl->getId(),
  162.             "object_bundle"                =>    "Security",
  163.             "object_entity"                =>    "AclPlanning",
  164.             "object_display"            =>    $loggingData['info'],
  165.             "society_group"                =>    $societyGroup,
  166.             "society"                    =>    $society,
  167.             "info"                        =>    $loggingData['info'],
  168.             "special_author"            =>    $loggingData['special_author'],
  169.         );
  170.         return $args;
  171.     }
  172. }