Skip to content

Commit 11f8734

Browse files
authored
Merge pull request #4853 from LibreSign/fix/use-user-id-to-load-file
fix: user userId when validate file
2 parents 18f6689 + e37a5a2 commit 11f8734

11 files changed

Lines changed: 29 additions & 79 deletions

lib/Helper/ValidateHelper.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use OCA\Libresign\Service\IdentifyMethodService;
2828
use OCA\Libresign\Service\SignerElementsService;
2929
use OCP\AppFramework\Db\DoesNotExistException;
30-
use OCP\Files\Config\IUserMountCache;
3130
use OCP\Files\IMimeTypeDetector;
3231
use OCP\Files\IRootFolder;
3332
use OCP\Files\NotFoundException;
@@ -74,7 +73,6 @@ public function __construct(
7473
private IGroupManager $groupManager,
7574
private IUserManager $userManager,
7675
private IRootFolder $root,
77-
private IUserMountCache $userMountCache,
7876
) {
7977
}
8078
public function validateNewFile(array $data, int $type = self::TYPE_TO_SIGN, ?IUser $user = null): void {
@@ -116,8 +114,13 @@ public function validateFile(array $data, int $type = self::TYPE_TO_SIGN, ?IUser
116114
if (!is_numeric($data['file']['fileId'])) {
117115
throw new LibresignException($this->l10n->t('File type: %s. Invalid fileID.', [$this->getTypeOfFile($type)]));
118116
}
119-
$this->validateIfNodeIdExists((int)$data['file']['fileId'], $type);
120-
$this->validateMimeTypeAcceptedByNodeId((int)$data['file']['fileId'], $type);
117+
if (!is_a($user, IUser::class)) {
118+
if (!is_a($data['userManager'], IUser::class)) {
119+
throw new LibresignException($this->l10n->t('User not found.'));
120+
}
121+
}
122+
$this->validateIfNodeIdExists((int)$data['file']['fileId'], $data['userManager']->getUID(), $type);
123+
$this->validateMimeTypeAcceptedByNodeId((int)$data['file']['fileId'], $data['userManager']->getUID(), $type);
121124
} elseif (!empty($data['file']['base64'])) {
122125
$this->validateBase64($data['file']['base64'], $type);
123126
} elseif (!empty($data['file']['path'])) {
@@ -366,10 +369,13 @@ public function fileCanBeSigned(File $file): void {
366369
}
367370
}
368371

369-
public function validateIfNodeIdExists(int $nodeId, int $type = self::TYPE_TO_SIGN): void {
370-
try {
372+
public function validateIfNodeIdExists(int $nodeId, string $userId = '', int $type = self::TYPE_TO_SIGN): void {
373+
if (!$userId) {
371374
$libresignFile = $this->fileMapper->getByFileId($nodeId);
372-
$file = $this->root->getUserFolder($libresignFile->getUserId())->getById($nodeId);
375+
$userId = $libresignFile->getUserId();
376+
}
377+
try {
378+
$file = $this->root->getUserFolder($userId)->getById($nodeId);
373379
$file = $file[0] ?? null;
374380
} catch (\Throwable $th) {
375381
throw new LibresignException($this->l10n->t('File type: %s. Invalid fileID.', [$this->getTypeOfFile($type)]));
@@ -379,9 +385,12 @@ public function validateIfNodeIdExists(int $nodeId, int $type = self::TYPE_TO_SI
379385
}
380386
}
381387

382-
public function validateMimeTypeAcceptedByNodeId(int $nodeId, int $type = self::TYPE_TO_SIGN): void {
383-
$libresignFile = $this->fileMapper->getByFileId($nodeId);
384-
$file = $this->root->getUserFolder($libresignFile->getUserId())->getById($nodeId);
388+
public function validateMimeTypeAcceptedByNodeId(int $nodeId, string $userId = '', int $type = self::TYPE_TO_SIGN): void {
389+
if (!$userId) {
390+
$libresignFile = $this->fileMapper->getByFileId($nodeId);
391+
$userId = $libresignFile->getUserId();
392+
}
393+
$file = $this->root->getUserFolder($userId)->getById($nodeId);
385394
$file = $file[0];
386395
$this->validateMimeTypeAcceptedByMime($file->getMimeType(), $type);
387396
}

lib/Service/AccountService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use OCP\AppFramework\Db\DoesNotExistException;
3030
use OCP\AppFramework\Utility\ITimeFactory;
3131
use OCP\Files\Config\IMountProviderCollection;
32-
use OCP\Files\Config\IUserMountCache;
3332
use OCP\Files\File;
3433
use OCP\Files\Folder;
3534
use OCP\Files\IMimeTypeDetector;
@@ -57,7 +56,6 @@ public function __construct(
5756
private IUserManager $userManager,
5857
private IAccountManager $accountManager,
5958
private IRootFolder $root,
60-
private IUserMountCache $userMountCache,
6159
private IMimeTypeDetector $mimeTypeDetector,
6260
private FileMapper $fileMapper,
6361
private FileTypeMapper $fileTypeMapper,

lib/Service/FileService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
2828
use OCP\Accounts\IAccountManager;
2929
use OCP\AppFramework\Db\DoesNotExistException;
30-
use OCP\Files\Config\IUserMountCache;
3130
use OCP\Files\IMimeTypeDetector;
3231
use OCP\Files\IRootFolder;
3332
use OCP\Files\NotFoundException;
@@ -85,7 +84,6 @@ public function __construct(
8584
private IURLGenerator $urlGenerator,
8685
protected IMimeTypeDetector $mimeTypeDetector,
8786
protected Pkcs12Handler $pkcs12Handler,
88-
private IUserMountCache $userMountCache,
8987
private IRootFolder $root,
9088
protected LoggerInterface $logger,
9189
protected IL10N $l10n,

lib/Service/FolderService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use OCA\Libresign\Exception\LibresignException;
1212
use OCP\AppFramework\Services\IAppConfig;
1313
use OCP\Files\AppData\IAppDataFactory;
14-
use OCP\Files\Config\IUserMountCache;
1514
use OCP\Files\File;
1615
use OCP\Files\Folder;
1716
use OCP\Files\IAppData;
@@ -27,7 +26,6 @@ class FolderService {
2726
protected IAppData $appData;
2827
public function __construct(
2928
private IRootFolder $root,
30-
private IUserMountCache $userMountCache,
3129
protected IAppDataFactory $appDataFactory,
3230
protected IGroupManager $groupManager,
3331
private IAppConfig $appConfig,

lib/Service/IdentifyMethod/IdentifyService.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use OCA\Libresign\Service\SessionService;
1717
use OCP\AppFramework\Utility\ITimeFactory;
1818
use OCP\EventDispatcher\IEventDispatcher;
19-
use OCP\Files\Config\IUserMountCache;
2019
use OCP\Files\IRootFolder;
2120
use OCP\IAppConfig;
2221
use OCP\IL10N;
@@ -35,7 +34,6 @@ public function __construct(
3534
private IRootFolder $root,
3635
private IAppConfig $appConfig,
3736
private SignRequestMapper $signRequestMapper,
38-
private IUserMountCache $userMountCache,
3937
private IL10N $l10n,
4038
private FileMapper $fileMapper,
4139
private IHasher $hasher,
@@ -111,10 +109,6 @@ public function getSignRequestMapper(): SignRequestMapper {
111109
return $this->signRequestMapper;
112110
}
113111

114-
public function getUserMountCache(): IUserMountCache {
115-
return $this->userMountCache;
116-
}
117-
118112
public function getL10n(): IL10N {
119113
return $this->l10n;
120114
}

lib/Service/SignFileService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use OCP\AppFramework\Db\DoesNotExistException;
4040
use OCP\AppFramework\Utility\ITimeFactory;
4141
use OCP\EventDispatcher\IEventDispatcher;
42-
use OCP\Files\Config\IUserMountCache;
4342
use OCP\Files\File;
4443
use OCP\Files\IRootFolder;
4544
use OCP\Files\Node;
@@ -92,7 +91,6 @@ public function __construct(
9291
private IRootFolder $root,
9392
private IUserSession $userSession,
9493
private IDateTimeZone $dateTimeZone,
95-
private IUserMountCache $userMountCache,
9694
private FileElementMapper $fileElementMapper,
9795
private UserElementMapper $userElementMapper,
9896
private IEventDispatcher $eventDispatcher,

tests/Unit/Helper/ValidateHelperTest.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use OCA\Libresign\Helper\ValidateHelper;
2121
use OCA\Libresign\Service\IdentifyMethodService;
2222
use OCA\Libresign\Service\SignerElementsService;
23-
use OCP\Files\Config\IUserMountCache;
2423
use OCP\Files\IMimeTypeDetector;
2524
use OCP\Files\IRootFolder;
2625
use OCP\IAppConfig;
@@ -48,7 +47,6 @@ final class ValidateHelperTest extends \OCA\Libresign\Tests\Unit\TestCase {
4847
private IGroupManager&MockObject $groupManager;
4948
private IUserManager&MockObject $userManager;
5049
private IRootFolder&MockObject $root;
51-
private IUserMountCache&MockObject $userMountCache;
5250

5351
public function setUp(): void {
5452
$this->l10n = $this->createMock(IL10N::class);
@@ -70,7 +68,6 @@ public function setUp(): void {
7068
$this->groupManager = $this->createMock(IGroupManager::class);
7169
$this->userManager = $this->createMock(IUserManager::class);
7270
$this->root = $this->createMock(IRootFolder::class);
73-
$this->userMountCache = $this->createMock(IUserMountCache::class);
7471
}
7572

7673
private function getValidateHelper(): ValidateHelper {
@@ -91,7 +88,6 @@ private function getValidateHelper(): ValidateHelper {
9188
$this->groupManager,
9289
$this->userManager,
9390
$this->root,
94-
$this->userMountCache,
9591
);
9692
return $validateHelper;
9793
}
@@ -117,12 +113,12 @@ public function testValidateFileWhenFileIdDoesNotExist():void {
117113
$this->root->method('getById')->will($this->returnCallback(function ():void {
118114
throw new \Exception('not found');
119115
}));
120-
$this->userMountCache
121-
->method('getMountsForFileId')
122-
->willreturn([]);
116+
$user = $this->createMock(\OCP\IUser::class);
117+
$user->method('getUID')->willReturn('john.doe');
123118
$this->getValidateHelper()->validateFile([
124119
'file' => ['fileId' => 123],
125-
'name' => 'test'
120+
'name' => 'test',
121+
'userManager' => $user,
126122
]);
127123
}
128124

@@ -137,12 +133,13 @@ public function testValidateNewFileUsingFileIdWithSuccess():void {
137133
$this->root
138134
->method('getById')
139135
->willReturn([$file]);
140-
$this->userMountCache
141-
->method('getMountsForFileId')
142-
->willreturn([]);
136+
137+
$user = $this->createMock(\OCP\IUser::class);
138+
$user->method('getUID')->willReturn('john.doe');
143139
$actual = $this->getValidateHelper()->validateNewFile([
144140
'file' => ['fileId' => 123],
145-
'name' => 'test'
141+
'name' => 'test',
142+
'userManager' => $user,
146143
]);
147144
$this->assertNull($actual);
148145
}
@@ -186,10 +183,7 @@ public function testValidateMimeTypeAcceptedByNodeId(string $mimetype, int $dest
186183
if ($exception) {
187184
$this->expectExceptionMessage($exception);
188185
}
189-
$this->userMountCache
190-
->method('getMountsForFileId')
191-
->willreturn([]);
192-
$actual = $this->getValidateHelper()->validateMimeTypeAcceptedByNodeId(171, $destination);
186+
$actual = $this->getValidateHelper()->validateMimeTypeAcceptedByNodeId(171, '', $destination);
193187
if (!$exception) {
194188
$this->assertNull($actual);
195189
}
@@ -391,9 +385,6 @@ public function testValidateIfNodeIdExistsWhenGetFileThrowException():void {
391385
->will($this->returnCallback(function ():void {
392386
throw new \Exception('not found');
393387
}));
394-
$this->userMountCache
395-
->method('getMountsForFileId')
396-
->willreturn([]);
397388
$this->getValidateHelper()->validateIfNodeIdExists(171);
398389
}
399390

@@ -402,9 +393,6 @@ public function testValidateIfNodeIdExistsWithInvalidFile():void {
402393
$this->root
403394
->method('getById')
404395
->willReturn([0 => null]);
405-
$this->userMountCache
406-
->method('getMountsForFileId')
407-
->willreturn([]);
408396
$this->getValidateHelper()->validateIfNodeIdExists(171);
409397
}
410398

@@ -415,9 +403,6 @@ public function testValidateIfNodeIdExistsWithSuccess():void {
415403
$this->root
416404
->method('getById')
417405
->willReturn(['file']);
418-
$this->userMountCache
419-
->method('getMountsForFileId')
420-
->willreturn([]);
421406
$actual = $this->getValidateHelper()->validateIfNodeIdExists(171);
422407
$this->assertNull($actual);
423408
}

tests/Unit/Service/AccountServiceTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use OCA\Settings\Mailer\NewUserMailHelper;
3030
use OCP\Accounts\IAccountManager;
3131
use OCP\Files\Config\IMountProviderCollection;
32-
use OCP\Files\Config\IUserMountCache;
3332
use OCP\Files\IMimeTypeDetector;
3433
use OCP\Files\IRootFolder;
3534
use OCP\IAppConfig;
@@ -50,7 +49,6 @@ final class AccountServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
5049
private IUserManager&MockObject $userManager;
5150
private IAccountManager $accountManager;
5251
private IRootFolder&MockObject $root;
53-
private IUserMountCache&MockObject $userMountCache;
5452
private IMimeTypeDetector&MockObject $mimeTypeDetector;
5553
private FileMapper&MockObject $fileMapper;
5654
private FileTypeMapper&MockObject $fileTypeMapper;
@@ -84,7 +82,6 @@ public function setUp(): void {
8482
$this->userManager = $this->createMock(IUserManager::class);
8583
$this->accountManager = $this->createMock(IAccountManager::class);
8684
$this->root = $this->createMock(IRootFolder::class);
87-
$this->userMountCache = $this->createMock(IUserMountCache::class);
8885
$this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
8986
$this->fileMapper = $this->createMock(FileMapper::class);
9087
$this->fileTypeMapper = $this->createMock(FileTypeMapper::class);
@@ -116,7 +113,6 @@ private function getService(): AccountService {
116113
$this->userManager,
117114
$this->accountManager,
118115
$this->root,
119-
$this->userMountCache,
120116
$this->mimeTypeDetector,
121117
$this->fileMapper,
122118
$this->fileTypeMapper,
@@ -369,9 +365,6 @@ public function testGetPdfByUuidWithSuccessAndSignedFile():void {
369365
$this->fileMapper
370366
->method('getByUuid')
371367
->will($this->returnValue($libresignFile));
372-
$this->userMountCache
373-
->method('getMountsForFileId')
374-
->willreturn([]);
375368
$node = $this->createMock(\OCP\Files\File::class);
376369
$this->root
377370
->method('getUserFolder')
@@ -389,9 +382,6 @@ public function testGetPdfByUuidWithSuccessAndUnignedFile():void {
389382
$this->fileMapper
390383
->method('getByUuid')
391384
->will($this->returnValue($libresignFile));
392-
$this->userMountCache
393-
->method('getMountsForFileId')
394-
->willreturn([]);
395385
$node = $this->createMock(\OCP\Files\File::class);
396386
$this->root
397387
->method('getUserFolder')

tests/Unit/Service/FileServiceTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ function is_uploaded_file($filename) {
3535
use OCA\Libresign\Service\PdfParserService;
3636
use OCA\Libresign\Tests\lib\AppConfigOverwrite;
3737
use OCP\Accounts\IAccountManager;
38-
use OCP\Files\Config\IUserMountCache;
3938
use OCP\Files\IMimeTypeDetector;
4039
use OCP\Files\IRootFolder;
4140
use OCP\Http\Client\IClientService;
@@ -72,7 +71,6 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
7271
private IURLGenerator $urlGenerator;
7372
protected IMimeTypeDetector $mimeTypeDetector;
7473
protected Pkcs12Handler $pkcs12Handler;
75-
private IUserMountCache $userMountCache;
7674
private IRootFolder $root;
7775
protected LoggerInterface $logger;
7876
protected IL10N $l10n;
@@ -108,7 +106,6 @@ private function getService(): FileService {
108106
$this->urlGenerator = \OCP\Server::get(IURLGenerator::class);
109107
$this->mimeTypeDetector = \OCP\Server::get(IMimeTypeDetector::class);
110108
$this->pkcs12Handler = \OCP\Server::get(Pkcs12Handler::class);
111-
$this->userMountCache = \OCP\Server::get(IUserMountCache::class);
112109
$this->root = \OCP\Server::get(IRootFolder::class);
113110
$this->logger = \OCP\Server::get(LoggerInterface::class);
114111
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
@@ -131,7 +128,6 @@ private function getService(): FileService {
131128
$this->urlGenerator,
132129
$this->mimeTypeDetector,
133130
$this->pkcs12Handler,
134-
$this->userMountCache,
135131
$this->root,
136132
$this->logger,
137133
$this->l10n,

tests/Unit/Service/FolderServiceTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use OCA\Libresign\Service\FolderService;
1313
use OCP\AppFramework\Services\IAppConfig;
1414
use OCP\Files\AppData\IAppDataFactory;
15-
use OCP\Files\Config\IUserMountCache;
1615
use OCP\Files\Folder;
1716
use OCP\Files\IAppData;
1817
use OCP\Files\IRootFolder;
@@ -59,7 +58,6 @@ public function newFolder(string $path): ISimpleFolder {
5958

6059
final class FolderServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
6160
private IRootFolder&MockObject $root;
62-
private IUserMountCache&MockObject $userMountCache;
6361
private IAppDataFactory&MockObject $appDataFactory;
6462
private IGroupManager&MockObject $groupManager;
6563
private IAppConfig&MockObject $appConfig;
@@ -68,7 +66,6 @@ final class FolderServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
6866
public function setUp(): void {
6967
parent::setUp();
7068
$this->root = $this->createMock(IRootFolder::class);
71-
$this->userMountCache = $this->createMock(IUserMountCache::class);
7269
$this->appDataFactory = $this->createMock(IAppDataFactory::class);
7370
$this->groupManager = $this->createMock(IGroupManager::class);
7471
$this->appConfig = $this->createMock(IAppConfig::class);
@@ -78,7 +75,6 @@ public function setUp(): void {
7875
private function getFolderService(?string $userId = '171'): FolderSErvice {
7976
$service = new FolderService(
8077
$this->root,
81-
$this->userMountCache,
8278
$this->appDataFactory,
8379
$this->groupManager,
8480
$this->appConfig,
@@ -106,7 +102,6 @@ public function testGetFolderAsUnauthenticatedWhenUserIdIsInvalid():void {
106102
}
107103

108104
public function testGetFileWithInvalidNodeId():void {
109-
$this->userMountCache->method('getMountsForFileId')->wilLReturn([]);
110105
$folder = $this->createMock(\OCP\Files\Folder::class);
111106
$folder->method('isUpdateable')->willReturn(true);
112107
$this->root->method('getUserFolder')->willReturn($folder);
@@ -117,9 +112,6 @@ public function testGetFileWithInvalidNodeId():void {
117112
}
118113

119114
public function testGetFolderWithValidNodeId():void {
120-
$this->userMountCache
121-
->method('getMountsForFileId')
122-
->willreturn([]);
123115
$node = $this->createMock(\OCP\Files\File::class);
124116
$folder = $this->createMock(\OCP\Files\Folder::class);
125117
$node->method('getParent')

0 commit comments

Comments
 (0)