From a6159f37e5af1d25ca4ae5ba5fd4b45e98901f56 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 11 Sep 2023 18:04:31 -0300 Subject: [PATCH 1/3] Add report to display logs of admin audit app Signed-off-by: Vitor Mattos --- lib/AppInfo/Application.php | 3 + lib/Datasource/AdminAudit.php | 114 +++++++++++++++++++ lib/Listener/AnalyticsDatasourceListener.php | 42 +++++++ 3 files changed, 159 insertions(+) create mode 100644 lib/Datasource/AdminAudit.php create mode 100644 lib/Listener/AnalyticsDatasourceListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4ab6cce..f088197 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -2,6 +2,8 @@ namespace OCA\MyCompany\AppInfo; +use OCA\Analytics\Datasource\DatasourceEvent; +use OCA\MyCompany\Listener\AnalyticsDatasourceListener; use OCA\MyCompany\Middleware\InjectionMiddleware; use OCA\MyCompany\Provider\PublicShareTemplateProvider; use OCP\AppFramework\App; @@ -25,5 +27,6 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { $context->registerMiddleWare(InjectionMiddleware::class, true); $context->registerPublicShareTemplateProvider(PublicShareTemplateProvider::class); + $context->registerEventListener(DatasourceEvent::class, AnalyticsDatasourceListener::class); } } diff --git a/lib/Datasource/AdminAudit.php b/lib/Datasource/AdminAudit.php new file mode 100644 index 0000000..c615682 --- /dev/null +++ b/lib/Datasource/AdminAudit.php @@ -0,0 +1,114 @@ + + * + * @author Vitor Mattos + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\MyCompany\Datasource; + +use OCA\Analytics\Datasource\IDatasource; +use OCP\IConfig; +use OCP\IL10N; + +class AdminAudit implements IDatasource +{ + public function __construct( + private IL10N $l10n, + private IConfig $config, + ) { + } + + /** + * @return string Display Name of the datasource + */ + public function getName(): string { + return $this->l10n->t('App: Admin Audit'); + } + + /** + * @return int digit unique datasource id + */ + public function getId(): int { + return 6; + } + + /** + * @return array available options of the datasoure + */ + public function getTemplate(): array { + return []; + } + + /** + * Read the Data + * @param $option + * @return array available options of the datasoure + */ + public function readData($option): array + { + $default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log'; + $logFile = $this->config->getAppValue('admin_audit', 'logfile', $default); + + $fp = fopen($logFile, 'r'); + $i = 1; + while (($buffer = fgets($fp, 4096)) !== false) { + $json = json_decode($buffer, true); + if (!str_contains($json['message'], 'File accessed')) { + continue; + } + preg_match('/File accessed: "(?.*)"/', $json['message'], $matches); + $data[] = [ + $json['time'], + $matches['file'], + $json['user'], + $json['remoteAddr'], + $json['userAgent'], + $i, + ]; + $i++; + } + fclose($fp); + + $header = [ + // TRANSLATORS Column name of report that list log entries of app Audit Report. This column will display the date of log. + $this->l10n->t('Date'), + // TRANSLATORS Column name of report that list log entries of app Audit Report. This column will display the acessed file. + $this->l10n->t('File'), + // TRANSLATORS Column name of report that list log entries of app Audit Report. This column will display the user that trigged the log. + $this->l10n->t('User'), + // TRANSLATORS Column name of report that list log entries of app Audit Report. This column will display the IP of user that trigged the log. + $this->l10n->t('IP'), + // TRANSLATORS Column name of report that list log entries of app Audit Report. This column will display the user agent of user that trigged the log. + $this->l10n->t('userAgent'), + // TRANSLATORS Column name of report that list log entries of app Audit Report. This column will display a sequencial number of rows to be displayed in report. + $this->l10n->t('row'), + ]; + + return [ + 'header' => $header, + 'dimensions' => array_slice($header, 0, count($header) - 1), + 'data' => $data, + 'error' => 0, + ]; + } +} diff --git a/lib/Listener/AnalyticsDatasourceListener.php b/lib/Listener/AnalyticsDatasourceListener.php new file mode 100644 index 0000000..cc25a52 --- /dev/null +++ b/lib/Listener/AnalyticsDatasourceListener.php @@ -0,0 +1,42 @@ + + * + * @author Vitor Mattos + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\MyCompany\Listener; + +use OCA\MyCompany\Datasource\AdminAudit; +use OCA\Analytics\Datasource\DatasourceEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +class AnalyticsDatasourceListener implements IEventListener { + public function handle(Event $event): void { + if (!($event instanceof DatasourceEvent)) { + // Unrelated + return; + } + $event->registerDatasource(AdminAudit::class); + } +} From 96e71369d12bc285ded726f9ba87b18187c83e90 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 11 Sep 2023 18:07:48 -0300 Subject: [PATCH 2/3] cs:fix and translators tooltip Signed-off-by: Vitor Mattos --- lib/Datasource/AdminAudit.php | 7 +++---- lib/Listener/AnalyticsDatasourceListener.php | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Datasource/AdminAudit.php b/lib/Datasource/AdminAudit.php index c615682..fd14f68 100644 --- a/lib/Datasource/AdminAudit.php +++ b/lib/Datasource/AdminAudit.php @@ -30,8 +30,7 @@ use OCP\IConfig; use OCP\IL10N; -class AdminAudit implements IDatasource -{ +class AdminAudit implements IDatasource { public function __construct( private IL10N $l10n, private IConfig $config, @@ -42,6 +41,7 @@ public function __construct( * @return string Display Name of the datasource */ public function getName(): string { + // TRANSLATORS The report name to display file access logs in the app Analytics return $this->l10n->t('App: Admin Audit'); } @@ -64,8 +64,7 @@ public function getTemplate(): array { * @param $option * @return array available options of the datasoure */ - public function readData($option): array - { + public function readData($option): array { $default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log'; $logFile = $this->config->getAppValue('admin_audit', 'logfile', $default); diff --git a/lib/Listener/AnalyticsDatasourceListener.php b/lib/Listener/AnalyticsDatasourceListener.php index cc25a52..28d1723 100644 --- a/lib/Listener/AnalyticsDatasourceListener.php +++ b/lib/Listener/AnalyticsDatasourceListener.php @@ -26,8 +26,8 @@ namespace OCA\MyCompany\Listener; -use OCA\MyCompany\Datasource\AdminAudit; use OCA\Analytics\Datasource\DatasourceEvent; +use OCA\MyCompany\Datasource\AdminAudit; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; From 0079c095cc613db2a5ae068da320919f7413cd94 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 11 Sep 2023 18:09:54 -0300 Subject: [PATCH 3/3] Update psalm baseline Signed-off-by: Vitor Mattos --- tests/psalm-baseline.xml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 487e315..3a5e8b0 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,7 +1,8 @@ - + + DatasourceEvent PublicShareTemplateProvider @@ -59,11 +60,24 @@ private + + + IDatasource + + quoteAlias + + + IEventListener + + + DatasourceEvent + + ApiController