Skip to content
Open
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
7 changes: 7 additions & 0 deletions apps/encryption/lib/Listeners/UserEventsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Lockdown\ILockdownManager;
use OCP\User\Events\BeforePasswordUpdatedEvent;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserCreatedEvent;
Expand All @@ -43,6 +44,7 @@ public function __construct(
private IUserSession $userSession,
private SetupManager $setupManager,
private PassphraseService $passphraseService,
private ILockdownManager $lockdownManager,
) {
}

Expand Down Expand Up @@ -70,6 +72,11 @@ public function handle(Event $event): void {
* Startup encryption backend upon user login
*/
private function onUserLogin(IUser $user, ?string $password): void {
// Do not try to setup filesystem if the current request does not have permissions to access it
if (!$this->lockdownManager->canAccessFilesystem()) {
return;
}

// ensure filesystem is loaded
$this->setupManager->setupForUser($user);
if ($this->util->isMasterKeyEnabled() === false) {
Expand Down
40 changes: 40 additions & 0 deletions apps/encryption/tests/Listeners/UserEventsListenersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Lockdown\ILockdownManager;
use OCP\User\Events\BeforePasswordUpdatedEvent;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserCreatedEvent;
Expand All @@ -41,6 +42,7 @@ class UserEventsListenersTest extends TestCase {
protected IUserManager&MockObject $userManager;
protected IUserSession&MockObject $userSession;
protected SetupManager&MockObject $setupManager;
protected ILockdownManager&MockObject $lockdownManager;
protected PassphraseService&MockObject $passphraseService;

protected UserEventsListener $instance;
Expand All @@ -55,6 +57,7 @@ public function setUp(): void {
$this->userManager = $this->createMock(IUserManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->setupManager = $this->createMock(SetupManager::class);
$this->lockdownManager = $this->createMock(ILockdownManager::class);
$this->passphraseService = $this->createMock(PassphraseService::class);

$this->instance = new UserEventsListener(
Expand All @@ -66,10 +69,14 @@ public function setUp(): void {
$this->userSession,
$this->setupManager,
$this->passphraseService,
$this->lockdownManager,
);
}

public function testLogin(): void {
$this->lockdownManager->expects(self::once())
->method('canAccessFilesystem')
->willReturn(true);
$this->userSetup->expects(self::once())
->method('setupUser')
->willReturn(true);
Expand All @@ -96,6 +103,9 @@ public function testLogin(): void {
}

public function testLoginMasterKey(): void {
$this->lockdownManager->expects(self::once())
->method('canAccessFilesystem')
->willReturn(true);
$this->util->method('isMasterKeyEnabled')->willReturn(true);

$this->userSetup->expects(self::never())
Expand All @@ -121,6 +131,36 @@ public function testLoginMasterKey(): void {
$this->instance->handle($event);
}

public function testLoginNoFilesystemAccess(): void {
$this->lockdownManager->expects(self::once())
->method('canAccessFilesystem')
->willReturn(false);

$this->userSetup->expects(self::never())
->method('setupUser');

$this->setupManager->expects(self::never())
->method('setupForUser');

$this->keyManager->expects(self::never())
->method('init');

$user = $this->createMock(IUser::class);
$user->expects(self::any())
->method('getUID')
->willReturn('testUser');

$event = $this->createMock(UserLoggedInEvent::class);
$event->expects(self::atLeastOnce())
->method('getUser')
->willReturn($user);
$event->expects(self::atLeastOnce())
->method('getPassword')
->willReturn('password');

$this->instance->handle($event);
}

public function testLogout(): void {
$this->session->expects(self::once())
->method('clear');
Expand Down
Loading