aboutsummaryrefslogtreecommitdiffstats
path: root/apps/admin_audit/lib/Actions/Action.php
blob: 5d06cf4fbf515516bc3cdd19af657f347e70396f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\AdminAudit\Actions;

use OCA\AdminAudit\IAuditLogger;

class Action {

	public function __construct(
		private IAuditLogger $logger,
	) {
	}

	/**
	 * Log a single action with a log level of info
	 *
	 * @param string $text
	 * @param array $params
	 * @param array $elements
	 * @param bool $obfuscateParameters
	 */
	public function log(string $text,
		array $params,
		array $elements,
		bool $obfuscateParameters = false): void {
		foreach ($elements as $element) {
			if (!isset($params[$element])) {
				if ($obfuscateParameters) {
					$this->logger->critical(
						'$params["'.$element.'"] was missing.',
						['app' => 'admin_audit']
					);
				} else {
					$this->logger->critical(
						sprintf(
							'$params["'.$element.'"] was missing. Transferred value: %s',
							print_r($params, true)
						),
						['app' => 'admin_audit']
					);
				}
				return;
			}
		}

		$replaceArray = [];
		foreach ($elements as $element) {
			if ($params[$element] instanceof \DateTime) {
				$params[$element] = $params[$element]->format('Y-m-d H:i:s');
			}
			$replaceArray[] = $params[$element];
		}

		$this->logger->info(
			vsprintf(
				$text,
				$replaceArray
			),
			[
				'app' => 'admin_audit'
			]
		);
	}
}