-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Filter getStorage to make sure the user has access to it #21468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,12 +23,9 @@ | |
|
|
||
| namespace OCA\Files_external\Service; | ||
|
|
||
| use \OCP\IUserSession; | ||
| use \OC\Files\Filesystem; | ||
|
|
||
| use \OCA\Files_external\Lib\StorageConfig; | ||
| use \OCA\Files_external\NotFoundException; | ||
| use \OCA\Files_External\Service\BackendService; | ||
| use \OCA\Files_External\Lib\Backend\Backend; | ||
| use \OCA\Files_External\Lib\Auth\AuthMechanism; | ||
| use \OCP\Files\StorageNotAvailableException; | ||
|
|
@@ -85,6 +82,7 @@ protected function getStorageConfigFromDBMount(array $mount) { | |
| array_values($applicableGroups), | ||
| $mount['priority'] | ||
| ); | ||
| $config->setType($mount['type']); | ||
| $config->setId((int)$mount['mount_id']); | ||
| return $config; | ||
| } catch (\UnexpectedValueException $e) { | ||
|
|
@@ -132,9 +130,22 @@ public function getStorage($id) { | |
| throw new NotFoundException('Storage with id "' . $id . '" not found'); | ||
| } | ||
|
|
||
| return $this->getStorageConfigFromDBMount($mount); | ||
| $config = $this->getStorageConfigFromDBMount($mount); | ||
| if ($this->isApplicable($config)) { | ||
| return $config; | ||
| } else { | ||
| throw new NotFoundException('Storage with id "' . $id . '" not found'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check whether this storage service should provide access to a storage | ||
| * | ||
| * @param StorageConfig $config | ||
| * @return bool | ||
| */ | ||
| abstract protected function isApplicable(StorageConfig $config); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPDoc 😄 |
||
|
|
||
| /** | ||
| * Gets all storages, valid or not | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,4 +152,22 @@ protected function getPriorityType(StorageConfig $storage) { | |
| return 0; | ||
| } | ||
|
|
||
| protected function isApplicable(StorageConfig $config) { | ||
| $applicableUsers = $config->getApplicableUsers(); | ||
| $applicableGroups = $config->getApplicableGroups(); | ||
|
|
||
| if (count($applicableUsers) === 0 && count($applicableGroups) === 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have used |
||
| return true; | ||
| } | ||
| if (in_array($this->getUser()->getUID(), $applicableUsers, true)) { | ||
| return true; | ||
| } | ||
| $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); | ||
| foreach ($groupIds as $groupId) { | ||
| if (in_array($groupId, $applicableGroups, true)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| */ | ||
| namespace OCA\Files_External\Tests\Service; | ||
|
|
||
| use OCA\Files_external\NotFoundException; | ||
| use OCA\Files_external\Service\StoragesService; | ||
| use \OCA\Files_External\Service\UserGlobalStoragesService; | ||
| use \OCP\IGroupManager; | ||
|
|
@@ -140,6 +141,13 @@ public function testGetStorageWithApplicable($applicableUsers, $applicableGroups | |
| $this->assertEquals('/mountpoint', $retrievedStorage->getMountPoint()); | ||
| } else { | ||
| $this->assertEquals(0, count($storages)); | ||
|
|
||
| try { | ||
| $this->service->getStorage($newStorage->getId()); | ||
| $this->fail('Failed asserting that storage can\'t be accessed by id'); | ||
| } catch (NotFoundException $e) { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make it less ugly I usually create a variable |
||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not throw some sort of
AccessDeniedException, rather than lying about the existence of a storage?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NotFoundException is consistent with 8.2 behavior