-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFileAccessService.php
More file actions
62 lines (50 loc) · 1.45 KB
/
FileAccessService.php
File metadata and controls
62 lines (50 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\FileMapper;
use OCP\IUser;
use OCP\IUserSession;
class FileAccessService {
public function __construct(
private FileMapper $fileMapper,
private SignFileService $signFileService,
private IUserSession $userSession,
) {
}
public function userCanAccessFileById(int $fileId, ?IUser $user = null): bool {
$user = $this->resolveUser($user);
if (!$user) {
return false;
}
return $this->userCanAccessFile($this->fileMapper->getById($fileId), $user);
}
public function userCanAccessFileByNodeId(int $nodeId, ?IUser $user = null): bool {
$user = $this->resolveUser($user);
if (!$user) {
return false;
}
return $this->userCanAccessFile($this->fileMapper->getByNodeId($nodeId), $user);
}
private function resolveUser(?IUser $user): ?IUser {
return $user ?? $this->userSession->getUser();
}
private function userCanAccessFile(FileEntity $file, IUser $user): bool {
if ($file->getUserId() === $user->getUID()) {
return true;
}
return $this->userCanSignFile($file, $user);
}
private function userCanSignFile(FileEntity $file, IUser $user): bool {
try {
$this->signFileService->getSignRequestToSign($file, null, $user);
return true;
} catch (\Exception) {
return false;
}
}
}