-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use event listener instead of user changed hook
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
- Loading branch information
Showing
6 changed files
with
142 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
apps/admin_audit/lib/Listener/UserChangedEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\AdminAudit\Listener; | ||
|
||
use OCA\AdminAudit\Actions\Action; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\User\Events\UserChangedEvent; | ||
|
||
/** @template-implements IEventListener<UserChangedEvent> */ | ||
class UserChangedEventListener extends Action implements IEventListener { | ||
public function handle(Event $event): void { | ||
if (!($event instanceof UserChangedEvent)) { | ||
return; | ||
} | ||
|
||
switch ($event->getFeature()) { | ||
case 'enabled': | ||
$this->log( | ||
$event->getValue() | ||
? 'User enabled: "%s"' | ||
: 'User disabled: "%s"', | ||
['user' => $event->getUser()->getUID()], | ||
[ | ||
'user', | ||
] | ||
); | ||
break; | ||
case 'eMailAddress': | ||
$this->log( | ||
'Email address changed for user %s', | ||
['user' => $event->getUser()->getUID()], | ||
[ | ||
'user', | ||
] | ||
); | ||
break; | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
apps/admin_audit/tests/Listener/UserChangedEventListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\AdminAudit\Tests\Actions; | ||
|
||
use OCA\AdminAudit\AuditLogger; | ||
use OCA\AdminAudit\Listener\UserChangedEventListener; | ||
use OCP\IUser; | ||
use OCP\User\Events\UserChangedEvent; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Test\TestCase; | ||
|
||
class UserChangedEventListenerTest extends TestCase { | ||
private AuditLogger|MockObject $logger; | ||
|
||
private UserChangedEventListener $listener; | ||
|
||
private MockObject|IUser $user; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->logger = $this->createMock(AuditLogger::class); | ||
$this->listener = new UserChangedEventListener($this->logger); | ||
|
||
$this->user = $this->createMock(IUser::class); | ||
$this->user->method('getUID')->willReturn('alice'); | ||
$this->user->method('getDisplayName')->willReturn('Alice'); | ||
} | ||
|
||
public function testSkipUnsupported(): void { | ||
$this->logger->expects($this->never()) | ||
->method('info'); | ||
|
||
$event = new UserChangedEvent( | ||
$this->user, | ||
'unsupported', | ||
'value', | ||
); | ||
|
||
$this->listener->handle($event); | ||
} | ||
|
||
public function testUserEnabled(): void { | ||
$this->logger->expects($this->once()) | ||
->method('info') | ||
->with('User enabled: "alice"', ['app' => 'admin_audit']); | ||
|
||
$event = new UserChangedEvent( | ||
$this->user, | ||
'enabled', | ||
true, | ||
false, | ||
); | ||
|
||
$this->listener->handle($event); | ||
} | ||
|
||
public function testUserDisabled(): void { | ||
$this->logger->expects($this->once()) | ||
->method('info') | ||
->with('User disabled: "alice"', ['app' => 'admin_audit']); | ||
|
||
$event = new UserChangedEvent( | ||
$this->user, | ||
'enabled', | ||
false, | ||
true, | ||
); | ||
|
||
$this->listener->handle($event); | ||
} | ||
|
||
public function testEmailChanged(): void { | ||
$this->logger->expects($this->once()) | ||
->method('info') | ||
->with('Email address changed for user alice', ['app' => 'admin_audit']); | ||
|
||
$event = new UserChangedEvent( | ||
$this->user, | ||
'eMailAddress', | ||
'alice@alice.com', | ||
'', | ||
); | ||
|
||
$this->listener->handle($event); | ||
} | ||
} |