Action.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\AdminAudit\Actions;
  8. use OCA\AdminAudit\IAuditLogger;
  9. class Action {
  10. public function __construct(
  11. private IAuditLogger $logger,
  12. ) {
  13. }
  14. /**
  15. * Log a single action with a log level of info
  16. *
  17. * @param string $text
  18. * @param array $params
  19. * @param array $elements
  20. * @param bool $obfuscateParameters
  21. */
  22. public function log(string $text,
  23. array $params,
  24. array $elements,
  25. bool $obfuscateParameters = false): void {
  26. foreach ($elements as $element) {
  27. if (!isset($params[$element])) {
  28. if ($obfuscateParameters) {
  29. $this->logger->critical(
  30. '$params["' . $element . '"] was missing.',
  31. ['app' => 'admin_audit']
  32. );
  33. } else {
  34. $this->logger->critical(
  35. sprintf(
  36. '$params["' . $element . '"] was missing. Transferred value: %s',
  37. print_r($params, true)
  38. ),
  39. ['app' => 'admin_audit']
  40. );
  41. }
  42. return;
  43. }
  44. }
  45. $replaceArray = [];
  46. foreach ($elements as $element) {
  47. if ($params[$element] instanceof \DateTime) {
  48. $params[$element] = $params[$element]->format('Y-m-d H:i:s');
  49. }
  50. $replaceArray[] = $params[$element];
  51. }
  52. $this->logger->info(
  53. vsprintf(
  54. $text,
  55. $replaceArray
  56. ),
  57. [
  58. 'app' => 'admin_audit'
  59. ]
  60. );
  61. }
  62. }