Skip to content

Commit

Permalink
Add report to display logs of admin audit app
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Sep 11, 2023
1 parent 288314f commit a6159f3
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
114 changes: 114 additions & 0 deletions lib/Datasource/AdminAudit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Vitor Mattos <[email protected]>
*
* @author Vitor Mattos <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\MyCompany\Datasource;

use OCA\Analytics\Datasource\IDatasource;
use OCP\IConfig;
use OCP\IL10N;

class AdminAudit implements IDatasource

Check failure on line 33 in lib/Datasource/AdminAudit.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedClass

lib/Datasource/AdminAudit.php:33:29: UndefinedClass: Class, interface or enum named OCA\Analytics\Datasource\IDatasource does not exist (see https://psalm.dev/019)
{
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: "(?<file>.*)"/', $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,
];
}
}
42 changes: 42 additions & 0 deletions lib/Listener/AnalyticsDatasourceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Vitor Mattos <[email protected]>
*
* @author Vitor Mattos <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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 {

Check failure on line 34 in lib/Listener/AnalyticsDatasourceListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud

MissingTemplateParam

lib/Listener/AnalyticsDatasourceListener.php:34:46: MissingTemplateParam: OCA\MyCompany\Listener\AnalyticsDatasourceListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)
public function handle(Event $event): void {
if (!($event instanceof DatasourceEvent)) {

Check failure on line 36 in lib/Listener/AnalyticsDatasourceListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedClass

lib/Listener/AnalyticsDatasourceListener.php:36:27: UndefinedClass: Class, interface or enum named OCA\Analytics\Datasource\DatasourceEvent does not exist (see https://psalm.dev/019)
// Unrelated
return;
}
$event->registerDatasource(AdminAudit::class);
}
}

0 comments on commit a6159f3

Please sign in to comment.