Skip to content

Commit

Permalink
Merge pull request #47 from LibreCodeCoop/backport/45/stable27
Browse files Browse the repository at this point in the history
[stable27] Add report to display logs of admin audit app
  • Loading branch information
vitormattos authored Sep 11, 2023
2 parents 230c605 + 56619d9 commit 4c521dd
Show file tree
Hide file tree
Showing 4 changed files with 172 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 @@ -26,6 +26,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 @@ -49,5 +51,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);
}
}
14 changes: 14 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<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 @@ -51,11 +52,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

0 comments on commit 4c521dd

Please sign in to comment.