Skip to content

Commit

Permalink
more share permission logic to storage wrapper
Browse files Browse the repository at this point in the history
this way we only have to determine the share permissions once

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Aug 14, 2023
1 parent dfbc0d8 commit 3f5d00c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
namespace OCA\Files_Sharing\Tests;

use OC\Files\Cache\Scanner;
use OC\Files\Filesystem;
use OC\Files\SetupManager;
use OCA\Files_Sharing\Controller\ShareAPIController;
use OCP\App\IAppManager;
use OCP\AppFramework\OCS\OCSBadRequestException;
Expand Down Expand Up @@ -74,6 +76,8 @@ protected function setUp(): void {
\OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
\OC::$server->getConfig()->setAppValue('core', 'shareapi_expire_after_n_days', '7');

Filesystem::getLoader()->removeStorageWrapper('sharing_mask');

$this->folder = self::TEST_FOLDER_NAME;
$this->subfolder = '/subfolder_share_api_test';
$this->subsubfolder = '/subsubfolder_share_api_test';
Expand Down
8 changes: 2 additions & 6 deletions lib/private/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function isEncrypted() {
}

/**
* Return the currently version used for the HMAC in the encryption app
* Return the current version used for the HMAC in the encryption app
*/
public function getEncryptedVersion(): int {
return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
Expand All @@ -241,11 +241,7 @@ public function getEncryptedVersion(): int {
* @return int
*/
public function getPermissions() {
$perms = (int) $this->data['permissions'];
if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) {
$perms = $perms & ~\OCP\Constants::PERMISSION_SHARE;
}
return $perms;
return (int) $this->data['permissions'];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/ObjectStore/HomeObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OC\Files\ObjectStore;

use OC\User\User;
use OCP\IUser;

class HomeObjectStoreStorage extends ObjectStoreStorage implements \OCP\Files\IHomeStorage {
/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public function getOwner($path) {
* @param string $path, optional
* @return \OC\User\User
*/
public function getUser($path = null) {
public function getUser($path = null): IUser {
return $this->user;
}
}
17 changes: 15 additions & 2 deletions lib/private/Files/SetupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
use OC\Files\Storage\Wrapper\PermissionsMask;
use OC\Files\Storage\Wrapper\Quota;
use OC\Lockdown\Filesystem\NullStorage;
use OC\Share\Share;
use OC_App;
use OC_Hook;
use OC_Util;
use OCA\Files_Sharing\ISharedStorage;
use OCP\Constants;
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -46,6 +48,7 @@
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Events\InvalidateMountCacheEvent;
use OCP\Files\Events\Node\FilesystemTornDownEvent;
use OCP\Files\IHomeStorage;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
Expand All @@ -60,6 +63,7 @@
use OCP\IUserSession;
use OCP\Lockdown\ILockdownManager;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

class SetupManager {
Expand Down Expand Up @@ -139,8 +143,17 @@ private function setupBuiltinWrappers() {
return $storage;
});

Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
if (!$mount->getOption('enable_sharing', true)) {
Filesystem::addStorageWrapper('sharing_mask', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
$reSharingEnabled = Share::isResharingAllowed();
$sharingEnabledForMount = $mount->getOption('enable_sharing', true);
/** @var IUserSession $userSession */
$userSession = \OC::$server->get(IUserSession::class);
$user = $userSession->getUser();
/** @var IManager $shareManager */
$shareManager = \OC::$server->get(IManager::class);
$sharingEnabledForUser = $user && !$shareManager->sharingDisabledForUser($user->getUID());
$isShared = $storage->instanceOfStorage(ISharedStorage::class);
if (!$sharingEnabledForMount || !$sharingEnabledForUser || (!$reSharingEnabled && $isShared)) {
return new PermissionsMask([
'storage' => $storage,
'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Storage/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OC\Files\Storage;

use OC\Files\Cache\HomePropagator;
use OCP\IUser;

/**
* Specialized version of Local storage for home directory usage
Expand Down Expand Up @@ -94,7 +95,7 @@ public function getPropagator($storage = null) {
*
* @return \OC\User\User owner of this home storage
*/
public function getUser() {
public function getUser(): IUser {
return $this->user;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/public/Files/IHomeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@
namespace OCP\Files;

use OCP\Files\Storage\IStorage;
use OCP\IUser;

/**
* Interface IHomeStorage
*
* @since 7.0.0
*/
interface IHomeStorage extends IStorage {
/**
* Get the user for this home storage
*
* @return IUser
* @since 28.0.0
*/
public function getUser(): IUser;
}

0 comments on commit 3f5d00c

Please sign in to comment.