Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add report to display logs of admin audit app #45

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
113 changes: 113 additions & 0 deletions lib/Datasource/AdminAudit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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 {
public function __construct(
private IL10N $l10n,
private IConfig $config,
) {
}

/**
* @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');
}

/**
* @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\Analytics\Datasource\DatasourceEvent;
use OCA\MyCompany\Datasource\AdminAudit;
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);
}
}
16 changes: 15 additions & 1 deletion tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.14.1@b9d355e0829c397b9b3b47d0c0ed042a8a70284d">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="lib/AppInfo/Application.php">
<UndefinedClass>
<code>DatasourceEvent</code>
<code>PublicShareTemplateProvider</code>
</UndefinedClass>
</file>
Expand Down Expand Up @@ -59,11 +60,24 @@
<code>private</code>
</UndefinedClass>
</file>
<file src="lib/Datasource/AdminAudit.php">
<UndefinedClass>
<code>IDatasource</code>
</UndefinedClass>
</file>
<file src="lib/Db/FormSubmissionMapper.php">
<UndefinedInterfaceMethod>
<code>quoteAlias</code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/Listener/AnalyticsDatasourceListener.php">
<MissingTemplateParam>
<code>IEventListener</code>
</MissingTemplateParam>
<UndefinedClass>
<code>DatasourceEvent</code>
</UndefinedClass>
</file>
<file src="lib/Middleware/InjectionMiddleware.php">
<UndefinedClass>
<code>ApiController</code>
Expand Down
Loading