Skip to content

Commit 7e251bc

Browse files
committed
chore(refactor): hand the user to the fileContextFactory
Signed-off-by: Max <max@nextcloud.com>
1 parent 4965d20 commit 7e251bc

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

lib/Context/FileContextFactory.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
use OCP\DirectEditing\IToken;
1313
use OCP\Files\File;
1414
use OCP\Files\NotFoundException;
15-
use OCP\Files\NotPermittedException;
1615
use OCP\IL10N;
17-
use OCP\IUserSession;
16+
use OCP\IUser;
1817
use Psr\Log\LoggerInterface;
1918

2019
class FileContextFactory {
@@ -24,7 +23,6 @@ public function __construct(
2423
private readonly IL10N $l10n,
2524
private readonly LockService $lockService,
2625
private readonly LoggerInterface $logger,
27-
private readonly IUserSession $userSession,
2826
) {
2927
}
3028

@@ -43,24 +41,20 @@ private function build(
4341
}
4442

4543
/**
46-
* @throws NotPermittedException if not logged in
4744
* @throws NotFoundException if the file cannot be found
4845
*/
49-
public function buildForId(
46+
public function buildForUser(
47+
IUser $user,
5048
int $id,
5149
): FileContext {
52-
$userId = $this->userSession->getUser()?->getUID();
53-
if ($userId === null) {
54-
throw new NotPermittedException();
55-
}
56-
$file = $this->fileService->getFileById($id, $userId);
50+
$file = $this->fileService->getFileById($id, $user->getUID());
5751
return $this->build($file);
5852
}
5953

6054
/**
6155
* @throws NotFoundException if the file cannot be found
6256
*/
63-
public function buildForShareWithId(
57+
public function buildForShare(
6458
string $token,
6559
int $id,
6660
): FileContext {

lib/Listeners/RegisterContextEventListener.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use OCA\Text\Event\RegisterContextEvent;
1313
use OCP\EventDispatcher\Event;
1414
use OCP\EventDispatcher\IEventListener;
15+
use OCP\Files\NotPermittedException;
16+
use OCP\IUserSession;
1517
use Override;
1618

1719
/** @implements IEventListener<Event|RegisterContextEvent> */
1820
class RegisterContextEventListener implements IEventListener {
1921

2022
public function __construct(
2123
private readonly FileContextFactory $fileContextFactory,
24+
private readonly IUserSession $userSession,
2225
) {
2326
}
2427

@@ -32,9 +35,13 @@ public function handle(Event $event): void {
3235
'file',
3336
function (int $id, string $type, ?string $shareToken) {
3437
if ($shareToken === null) {
35-
return $this->fileContextFactory->buildForId($id);
38+
$user = $this->userSession->getUser();
39+
if ($user === null) {
40+
throw new NotPermittedException();
41+
}
42+
return $this->fileContextFactory->buildForUser($user, $id);
3643
} else {
37-
return $this->fileContextFactory->buildForShareWithId($shareToken, $id);
44+
return $this->fileContextFactory->buildForShare($shareToken, $id);
3845
}
3946
}
4047
);

0 commit comments

Comments
 (0)