Skip to content

Commit a954412

Browse files
committed
feat(admin-log): improved admin logging (#3833)
1 parent 61d5d9d commit a954412

File tree

7 files changed

+65
-18
lines changed

7 files changed

+65
-18
lines changed

phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace phpMyFAQ\Controller\Administration;
2121

2222
use Exception;
23+
use phpMyFAQ\Administration\AdminLog;
2324
use phpMyFAQ\Administration\Helper;
2425
use phpMyFAQ\Controller\AbstractController;
2526
use phpMyFAQ\Controller\Exception\ForbiddenException;
@@ -36,6 +37,15 @@
3637

3738
abstract class AbstractAdministrationController extends AbstractController
3839
{
40+
protected ?AdminLog $adminLog = null;
41+
42+
public function __construct()
43+
{
44+
parent::__construct();
45+
46+
$this->adminLog = $this->container->get(id: 'phpmyfaq.admin.admin-log');
47+
}
48+
3949
/**
4050
* @return string[]
4151
* @throws Exception

phpmyfaq/src/phpMyFAQ/Controller/Administration/AdminLogController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,19 @@ public function index(Request $request): Response
4545
{
4646
$this->userHasPermission(PermissionType::STATISTICS_ADMINLOG);
4747

48-
$adminLog = $this->container->get(id: 'phpmyfaq.admin.admin-log');
49-
5048
$itemsPerPage = 15;
5149
$page = Filter::filterVar($request->attributes->get('page'), FILTER_VALIDATE_INT, 1);
5250

5351
// Pagination options
5452
$options = [
5553
'baseUrl' => $request->getUri(),
56-
'total' => $adminLog->getNumberOfEntries(),
54+
'total' => $this->adminLog->getNumberOfEntries(),
5755
'perPage' => $itemsPerPage,
5856
'pageParamName' => 'page',
5957
];
6058
$pagination = new Pagination($options);
6159

62-
$loggingData = $adminLog->getAll();
60+
$loggingData = $this->adminLog->getAll();
6361

6462
$offset = ($page - 1) * $itemsPerPage;
6563
$currentItems = array_slice($loggingData, $offset, $itemsPerPage);

phpmyfaq/src/phpMyFAQ/Controller/Administration/AuthenticationController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public function authenticate(Request $request): Response
3939
return new RedirectResponse(url: './');
4040
}
4141

42-
$logging = $this->container->get(id: 'phpmyfaq.admin.admin-log');
43-
4442
$username = Filter::filterVar($request->request->get(key: 'faqusername'), FILTER_SANITIZE_SPECIAL_CHARS);
4543
$password = Filter::filterVar(
4644
$request->request->get(key: 'faqpassword'),
@@ -68,7 +66,7 @@ public function authenticate(Request $request): Response
6866
return new RedirectResponse(url: './token?user-id=' . $this->currentUser->getUserId());
6967
}
7068
} catch (Exception) {
71-
$logging->log(
69+
$this->adminLog->log(
7270
$this->currentUser,
7371
'Login-error\nLogin: ' . $username . '\nErrors: '
7472
. implode(separator: ', ', array: $this->currentUser->errors),

phpmyfaq/src/phpMyFAQ/Controller/Administration/BackupController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace phpMyFAQ\Controller\Administration;
2121

2222
use phpMyFAQ\Core\Exception;
23+
use phpMyFAQ\Enums\AdminLogType;
2324
use phpMyFAQ\Enums\BackupType;
2425
use phpMyFAQ\Enums\PermissionType;
2526
use phpMyFAQ\Session\Token;
@@ -70,10 +71,12 @@ public function export(Request $request): Response
7071
$this->userHasPermission(PermissionType::BACKUP);
7172

7273
$type = $request->attributes->get(key: 'type');
73-
if (!\in_array($type, ['content', 'logs'], true)) {
74+
if (!in_array($type, ['content', 'logs'], true)) {
7475
return new Response(status: Response::HTTP_BAD_REQUEST);
7576
}
7677

78+
$this->adminLog->log($this->currentUser, AdminLogType::BACKUP_EXPORT->value);
79+
7780
$backup = $this->container->get(id: 'phpmyfaq.backup');
7881

7982
$backupType = $type === 'content' ? BackupType::BACKUP_TYPE_DATA : BackupType::BACKUP_TYPE_LOGS;
@@ -107,6 +110,8 @@ public function restore(Request $request): Response
107110
throw new UnauthorizedHttpException(challenge: 'Invalid CSRF token');
108111
}
109112

113+
$this->adminLog->log($this->currentUser, AdminLogType::BACKUP_RESTORE->value);
114+
110115
$file = $request->files->get(key: 'userfile');
111116

112117
if (!$file) {

phpmyfaq/src/phpMyFAQ/Controller/Administration/FaqController.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use phpMyFAQ\Core\Exception;
2727
use phpMyFAQ\Database;
2828
use phpMyFAQ\Entity\SeoEntity;
29+
use phpMyFAQ\Enums\AdminLogType;
2930
use phpMyFAQ\Enums\PermissionType;
3031
use phpMyFAQ\Enums\SeoType;
3132
use phpMyFAQ\Faq\Permission;
@@ -108,7 +109,7 @@ public function add(Request $request): Response
108109
$faq = $this->container->get(id: 'phpmyfaq.faq');
109110
$userHelper = $this->container->get(id: 'phpmyfaq.helper.user-helper');
110111

111-
$this->container->get(id: 'phpmyfaq.admin.admin-log')->log($this->currentUser, 'admin-add-faq');
112+
$this->adminLog->log($this->currentUser, AdminLogType::FAQ_ADD->value);
112113
$categories = [];
113114

114115
$faqData = [
@@ -181,7 +182,7 @@ public function addInCategory(Request $request): Response
181182
$faq = $this->container->get(id: 'phpmyfaq.faq');
182183
$userHelper = $this->container->get(id: 'phpmyfaq.helper.user-helper');
183184

184-
$this->container->get(id: 'phpmyfaq.admin.admin-log')->log($this->currentUser, 'admin-add-faq');
185+
$this->adminLog->log($this->currentUser, AdminLogType::FAQ_ADD->value);
185186

186187
$faqData = [
187188
'id' => 0,
@@ -258,7 +259,7 @@ public function edit(Request $request): Response
258259
$faqLanguage = Filter::filterVar($request->attributes->get('faqLanguage'), FILTER_SANITIZE_SPECIAL_CHARS);
259260
$selectedRevisionId = Filter::filterVar($request->attributes->get('selectedRevisionId'), FILTER_VALIDATE_INT);
260261

261-
$this->container->get(id: 'phpmyfaq.admin.admin-log')->log($this->currentUser, 'admin-edit-faq ' . $faqId);
262+
$this->adminLog->log($this->currentUser, AdminLogType::FAQ_EDIT->value . ':' . $faqId);
262263

263264
$categories = $categoryRelation->getCategories($faqId, $faqLanguage);
264265

@@ -293,7 +294,7 @@ public function edit(Request $request): Response
293294

294295
// User permissions
295296
$userPermission = $this->container->get(id: 'phpmyfaq.faq.permission')->get(Permission::USER, $faqId);
296-
if (count($userPermission) == 0 || $userPermission[0] == -1) {
297+
if (count($userPermission) === 0 || $userPermission[0] === -1) {
297298
$allUsers = true;
298299
$restrictedUsers = false;
299300
$userPermission[0] = -1;
@@ -385,7 +386,7 @@ public function copy(Request $request): Response
385386
$faqId = (int) Filter::filterVar($request->attributes->get('faqId'), FILTER_VALIDATE_INT);
386387
$faqLanguage = Filter::filterVar($request->attributes->get('faqLanguage'), FILTER_SANITIZE_SPECIAL_CHARS);
387388

388-
$this->container->get(id: 'phpmyfaq.admin.admin-log')->log($this->currentUser, 'admin-copy-faq ' . $faqId);
389+
$this->adminLog->log($this->currentUser, AdminLogType::FAQ_COPY->value . ':' . $faqId);
389390

390391
$categories = [];
391392

@@ -459,7 +460,7 @@ public function translate(Request $request): Response
459460
$faqId = (int) Filter::filterVar($request->attributes->get('faqId'), FILTER_VALIDATE_INT);
460461
$faqLanguage = Filter::filterVar($request->attributes->get('faqLanguage'), FILTER_SANITIZE_SPECIAL_CHARS);
461462

462-
$this->container->get(id: 'phpmyfaq.admin.admin-log')->log($this->currentUser, 'admin-translate-faq ' . $faqId);
463+
$this->adminLog->log($this->currentUser, AdminLogType::FAQ_TRANSLATE->value . ':' . $faqId);
463464

464465
$categories = [];
465466

@@ -533,10 +534,7 @@ public function answer(Request $request): Response
533534
$questionId = (int) Filter::filterVar($request->attributes->get('questionId'), FILTER_VALIDATE_INT);
534535
$faqLanguage = Filter::filterVar($request->attributes->get('faqLanguage'), FILTER_SANITIZE_SPECIAL_CHARS);
535536

536-
$this->container->get(id: 'phpmyfaq.admin.admin-log')->log(
537-
$this->currentUser,
538-
'admin-answer-question ' . $questionId,
539-
);
537+
$this->adminLog->log($this->currentUser, AdminLogType::FAQ_ANSWER_ADD->value . ':' . $questionId);
540538

541539
/** @var Question $question */
542540
$question = $this->container->get(id: 'phpmyfaq.question');
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* Admin log type enum
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2026 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2026-01-04
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace phpMyFAQ\Enums;
21+
22+
enum AdminLogType: string
23+
{
24+
case BACKUP_EXPORT = 'backup-export';
25+
case BACKUP_RESTORE = 'backup-restore';
26+
case FAQ_ADD = 'faq-add';
27+
case FAQ_EDIT = 'faq-edit';
28+
case FAQ_COPY = 'faq-copy';
29+
case FAQ_TRANSLATE = 'faq-translate';
30+
case FAQ_ANSWER_ADD = 'faq-answer-add';
31+
}

tests/phpMyFAQ/Controller/BackupControllerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace phpMyFAQ\Controller;
66

7+
use phpMyFAQ\Administration\AdminLog;
78
use phpMyFAQ\Administration\Backup;
89
use phpMyFAQ\Administration\Backup\BackupExportResult;
910
use phpMyFAQ\Configuration;
@@ -28,6 +29,7 @@ class BackupControllerTest extends TestCase
2829
private CurrentUser $currentUserMock;
2930
private BasicPermission $permissionMock;
3031
private Backup $backupServiceMock;
32+
private AdminLog $adminLogMock;
3133

3234
private Session $session;
3335

@@ -37,6 +39,7 @@ protected function setUp(): void
3739
$this->currentUserMock = $this->createStub(CurrentUser::class);
3840
$this->permissionMock = $this->createStub(BasicPermission::class);
3941
$this->backupServiceMock = $this->createMock(Backup::class);
42+
$this->adminLogMock = $this->createStub(AdminLog::class);
4043
$this->session = new Session(new MockArraySessionStorage());
4144

4245
$this->currentUserMock->perm = $this->permissionMock;
@@ -48,19 +51,23 @@ private function createController(): BackupController
4851
$this->configurationMock,
4952
$this->currentUserMock,
5053
$this->backupServiceMock,
54+
$this->adminLogMock,
5155
$this->session,
5256
) extends BackupController {
5357
public function __construct(
5458
Configuration $configuration,
5559
CurrentUser $currentUser,
5660
Backup $backupService,
61+
AdminLog $adminLog,
5762
Session $session,
5863
) {
5964
$this->configuration = $configuration;
6065
$this->currentUser = $currentUser;
6166
$this->session = $session;
67+
$this->adminLog = $adminLog;
6268
$this->container = new ContainerBuilder();
6369
$this->container->set('phpmyfaq.backup', $backupService);
70+
$this->container->set('phpmyfaq.admin.admin-log', $adminLog);
6471
$this->container->set('session', $session);
6572
}
6673
};

0 commit comments

Comments
 (0)