Action.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\AdminAudit\Actions;
  28. use OCA\AdminAudit\IAuditLogger;
  29. class Action {
  30. public function __construct(
  31. private IAuditLogger $logger,
  32. ) {
  33. }
  34. /**
  35. * Log a single action with a log level of info
  36. *
  37. * @param string $text
  38. * @param array $params
  39. * @param array $elements
  40. * @param bool $obfuscateParameters
  41. */
  42. public function log(string $text,
  43. array $params,
  44. array $elements,
  45. bool $obfuscateParameters = false): void {
  46. foreach ($elements as $element) {
  47. if (!isset($params[$element])) {
  48. if ($obfuscateParameters) {
  49. $this->logger->critical(
  50. '$params["'.$element.'"] was missing.',
  51. ['app' => 'admin_audit']
  52. );
  53. } else {
  54. $this->logger->critical(
  55. sprintf(
  56. '$params["'.$element.'"] was missing. Transferred value: %s',
  57. print_r($params, true)
  58. ),
  59. ['app' => 'admin_audit']
  60. );
  61. }
  62. return;
  63. }
  64. }
  65. $replaceArray = [];
  66. foreach ($elements as $element) {
  67. if ($params[$element] instanceof \DateTime) {
  68. $params[$element] = $params[$element]->format('Y-m-d H:i:s');
  69. }
  70. $replaceArray[] = $params[$element];
  71. }
  72. $this->logger->info(
  73. vsprintf(
  74. $text,
  75. $replaceArray
  76. ),
  77. [
  78. 'app' => 'admin_audit'
  79. ]
  80. );
  81. }
  82. }