Action.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * Log a single action with a log level of info
  35. *
  36. * @param string $text
  37. * @param array $params
  38. * @param array $elements
  39. * @param bool $obfuscateParameters
  40. */
  41. public function log(string $text,
  42. array $params,
  43. array $elements,
  44. bool $obfuscateParameters = false): void {
  45. foreach ($elements as $element) {
  46. if (!isset($params[$element])) {
  47. if ($obfuscateParameters) {
  48. $this->logger->critical(
  49. '$params["'.$element.'"] was missing.',
  50. ['app' => 'admin_audit']
  51. );
  52. } else {
  53. $this->logger->critical(
  54. sprintf(
  55. '$params["'.$element.'"] was missing. Transferred value: %s',
  56. print_r($params, true)
  57. ),
  58. ['app' => 'admin_audit']
  59. );
  60. }
  61. return;
  62. }
  63. }
  64. $replaceArray = [];
  65. foreach ($elements as $element) {
  66. if ($params[$element] instanceof \DateTime) {
  67. $params[$element] = $params[$element]->format('Y-m-d H:i:s');
  68. }
  69. $replaceArray[] = $params[$element];
  70. }
  71. $this->logger->info(
  72. vsprintf(
  73. $text,
  74. $replaceArray
  75. ),
  76. [
  77. 'app' => 'admin_audit'
  78. ]
  79. );
  80. }
  81. }