src/Services/ActionLoggerService.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Utils\SecurityUtils;
  4. use Psr\Log\LoggerInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. use Throwable;
  8. class ActionLoggerService
  9. {
  10.     const TYPE_ALERT "ALERT";
  11.     const TYPE_UPDATE "UPDATE";
  12.     const TYPE_TEAM "TEAM";
  13.     const TYPE_EMAIL "EMAIL";
  14.     const CREATE_EVENT_APPLICATION "CREATE_EVENT_APPLICATION";
  15.     /**
  16.      * @var TokenStorageInterface $tokenStorageInterface
  17.      */
  18.     protected $tokenStorageInterface;
  19.     /**
  20.      * @var LoggerInterface $logger
  21.      */
  22.     protected $logger;
  23.     /**
  24.      * @var Security $security
  25.      */
  26.     protected $security;
  27.     public function __construct(
  28.         TokenStorageInterface         $tokenStorageInterface,
  29.         Security                      $security,
  30.         LoggerInterface               $logger
  31.     )
  32.     {
  33.         $this->tokenStorageInterface $tokenStorageInterface;
  34.         $this->logger $logger;
  35.         $this->security $security;
  36.     }
  37.     public function logException(Throwable $exceptionstring $message)
  38.     {
  39.         $this->getLogger()->error($message ' : ' $exception->getMessage(),
  40.             ['exception' => $exception]);
  41.     }
  42.     /**
  43.      * @return LoggerInterface
  44.      */
  45.     public function getLogger(): LoggerInterface
  46.     {
  47.         return $this->logger;
  48.     }
  49.     /**
  50.      * To be used by unit test only...
  51.      *
  52.      * @param LoggerInterface $logger
  53.      */
  54.     public function setLogger(LoggerInterface $logger)
  55.     {
  56.         $this->logger $logger;
  57.     }
  58.     /**
  59.      * @param $action string the action done
  60.      * @param $type
  61.      * @param $objectId
  62.      * @param $message
  63.      * @param $targetObject
  64.      */
  65.     public function logActionInfo(
  66.         string $action,
  67.         string $type,
  68.         string $objectId null,
  69.         string $targetObject null,
  70.         string $message null
  71.     )
  72.     {
  73.         $this->logger->info("ACTION", [
  74.             'user_uid' => SecurityUtils::getUid($this->tokenStorageInterface),
  75.             'action' => $action,
  76.             'type' => $type,
  77.             'objectId' => $objectId,
  78.             'target' => $targetObject,
  79.             'message' => $message,
  80.         ]);
  81.     }
  82.     public function logUserRoles(array $rolesstring $urlFrom null)
  83.     {
  84.         $asJson SecurityUtils::getAuthorizationAsJson($roles$this->security);
  85.         $this->logAction('LOG_USER_ROLES''USER_ROLES'$urlFromnull$asJson);
  86.     }
  87.     /**
  88.      * @param $action string the action done
  89.      * @param string $type
  90.      * @param string $objectId
  91.      * @param string $targetObject
  92.      * @param string $message
  93.      * @param string|null $parentId
  94.      * @param array $message
  95.      */
  96.     public function logAction(
  97.         string $action,
  98.         string $type,
  99.         string $objectId null,
  100.         string $targetObject null,
  101.         string $message null,
  102.         string $parentId null,
  103.         array  $object null
  104.     )
  105.     {
  106.         $this->logger->info("ACTION", [
  107.             'user_uid' => SecurityUtils::getUid($this->tokenStorageInterface),
  108.             'action' => $action,
  109.             'type' => $type,
  110.             'objectId' => $objectId,
  111.             'parentId' => $parentId,
  112.             'target' => $targetObject,
  113.             'message' => $message,
  114.             'object' => $object,
  115.         ]);
  116.     }
  117.     /**
  118.      * @param string $type
  119.      * @param string $objectId
  120.      * @param string $targetObject
  121.      * @param string $message
  122.      * @param string|null $parentId
  123.      * @param array $message
  124.      */
  125.     public function logActionV2(
  126.         string $prefix,
  127.         string $type,
  128.         string $objectId null,
  129.         string $url null,
  130.         string $message null,
  131.         string $parentId null,
  132.         array  $object null
  133.     )
  134.     {
  135.         $action $this->buildAction($prefix$url);
  136.         $this->logAction($action$type$objectId$url,
  137.             $message$parentId$object);
  138.     }
  139.     /**
  140.      * @param string $type
  141.      * @param string $objectId
  142.      * @param string $targetObject
  143.      * @param string $message
  144.      * @param string|null $parentId
  145.      * @param array $message
  146.      */
  147.     public function logActionErrorV2(
  148.         string $prefix,
  149.         string $type,
  150.         string $objectId null,
  151.         string $url null,
  152.         string $message null,
  153.         string $parentId null,
  154.         array  $object null
  155.     )
  156.     {
  157.         $action $this->buildAction($prefix$url);
  158.         $this->logActionError($action$type$objectId$url,
  159.             $message$parentId$object);
  160.     }
  161.     /**
  162.      * @param string $type
  163.      * @param string $objectId
  164.      * @param string $targetObject
  165.      * @param string $message
  166.      * @param string|null $parentId
  167.      * @param array $message
  168.      */
  169.     public function logActionWarnV2(
  170.         string $prefix,
  171.         string $type,
  172.         string $objectId null,
  173.         string $url null,
  174.         string $message null,
  175.         string $parentId null,
  176.         array  $object null
  177.     )
  178.     {
  179.         $action $this->buildAction($prefix$url);
  180.         $this->logActionWarn($action$type$objectId$url,
  181.             $message$parentId$object);
  182.     }
  183.     private function buildAction(String $prefixString $url){
  184.         $action $prefix;
  185.         $fragments parse_url($url);
  186.         foreach ($fragments as $fragment) {
  187.             if (!is_numeric($fragment)) {
  188.                 $action $action strtoupper($fragment);
  189.             }
  190.             if (next($fragments) == true) {
  191.                 $action $action "_";
  192.             }
  193.         }
  194.         return $action;
  195.     }
  196.     /**
  197.      * @param $action string the action done
  198.      * @param string $type
  199.      * @param string $objectId
  200.      * @param string $targetObject
  201.      * @param string $message
  202.      * @param string|null $parentId
  203.      */
  204.     public function logActionError(
  205.         string $action,
  206.         string $type,
  207.         string $objectId null,
  208.         string $targetObject null,
  209.         string $message null,
  210.         string $parentId null
  211.     )
  212.     {
  213.         $this->logger->error("ACTION", [
  214.             'user_uid' => SecurityUtils::getUid($this->tokenStorageInterface),
  215.             'action' => $action,
  216.             'type' => $type,
  217.             'objectId' => $objectId,
  218.             'parentId' => $parentId,
  219.             'target' => $targetObject,
  220.             'message' => $message,
  221.         ]);
  222.     }
  223.     /**
  224.      * @param $action string the action done
  225.      * @param string $type
  226.      * @param string $objectId
  227.      * @param string $targetObject
  228.      * @param string $message
  229.      * @param string|null $parentId
  230.      */
  231.     public function logActionWarn(
  232.         string $action,
  233.         string $type,
  234.         string $objectId null,
  235.         string $targetObject null,
  236.         string $message null,
  237.         string $parentId null
  238.     )
  239.     {
  240.         $this->logger->warning("ACTION", [
  241.             'user_uid' => SecurityUtils::getUid($this->tokenStorageInterface),
  242.             'action' => $action,
  243.             'type' => $type,
  244.             'objectId' => $objectId,
  245.             'parentId' => $parentId,
  246.             'target' => $targetObject,
  247.             'message' => $message,
  248.         ]);
  249.     }
  250. }