Action.php 2.3 KB

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