Skip to content

Commit 64f62f7

Browse files
authored
Merge pull request #39863 from nextcloud/sharing-mask-wrapper
move share permission logic to storage wrapper
2 parents 489a57e + e73889a commit 64f62f7

15 files changed

Lines changed: 168 additions & 127 deletions

File tree

apps/files_sharing/tests/ApiTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
namespace OCA\Files_Sharing\Tests;
3737

3838
use OC\Files\Cache\Scanner;
39+
use OC\Files\Filesystem;
40+
use OC\Files\SetupManager;
3941
use OCA\Files_Sharing\Controller\ShareAPIController;
4042
use OCP\App\IAppManager;
4143
use OCP\AppFramework\OCS\OCSBadRequestException;
@@ -74,6 +76,8 @@ protected function setUp(): void {
7476
\OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no');
7577
\OC::$server->getConfig()->setAppValue('core', 'shareapi_expire_after_n_days', '7');
7678

79+
Filesystem::getLoader()->removeStorageWrapper('sharing_mask');
80+
7781
$this->folder = self::TEST_FOLDER_NAME;
7882
$this->subfolder = '/subfolder_share_api_test';
7983
$this->subsubfolder = '/subsubfolder_share_api_test';

apps/files_sharing/tests/CapabilitiesTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
use OC\KnownUser\KnownUserService;
3232
use OC\Share20\Manager;
33+
use OC\Share20\ShareDisableChecker;
3334
use OCA\Files_Sharing\Capabilities;
3435
use OCP\EventDispatcher\IEventDispatcher;
3536
use OCP\Files\IRootFolder;
@@ -94,7 +95,8 @@ private function getResults(array $map) {
9495
$this->createMock(\OC_Defaults::class),
9596
$this->createMock(IEventDispatcher::class),
9697
$this->createMock(IUserSession::class),
97-
$this->createMock(KnownUserService::class)
98+
$this->createMock(KnownUserService::class),
99+
$this->createMock(ShareDisableChecker::class)
98100
);
99101
$cap = new Capabilities($config, $shareManager);
100102
$result = $this->getFilesSharingPart($cap->getCapabilities());

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,7 @@
16551655
'OC\\Share20\\PublicShareTemplateFactory' => $baseDir . '/lib/private/Share20/PublicShareTemplateFactory.php',
16561656
'OC\\Share20\\Share' => $baseDir . '/lib/private/Share20/Share.php',
16571657
'OC\\Share20\\ShareAttributes' => $baseDir . '/lib/private/Share20/ShareAttributes.php',
1658+
'OC\\Share20\\ShareDisableChecker' => $baseDir . '/lib/private/Share20/ShareDisableChecker.php',
16581659
'OC\\Share20\\ShareHelper' => $baseDir . '/lib/private/Share20/ShareHelper.php',
16591660
'OC\\Share20\\UserRemovedListener' => $baseDir . '/lib/private/Share20/UserRemovedListener.php',
16601661
'OC\\Share\\Constants' => $baseDir . '/lib/private/Share/Constants.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
16881688
'OC\\Share20\\PublicShareTemplateFactory' => __DIR__ . '/../../..' . '/lib/private/Share20/PublicShareTemplateFactory.php',
16891689
'OC\\Share20\\Share' => __DIR__ . '/../../..' . '/lib/private/Share20/Share.php',
16901690
'OC\\Share20\\ShareAttributes' => __DIR__ . '/../../..' . '/lib/private/Share20/ShareAttributes.php',
1691+
'OC\\Share20\\ShareDisableChecker' => __DIR__ . '/../../..' . '/lib/private/Share20/ShareDisableChecker.php',
16911692
'OC\\Share20\\ShareHelper' => __DIR__ . '/../../..' . '/lib/private/Share20/ShareHelper.php',
16921693
'OC\\Share20\\UserRemovedListener' => __DIR__ . '/../../..' . '/lib/private/Share20/UserRemovedListener.php',
16931694
'OC\\Share\\Constants' => __DIR__ . '/../../..' . '/lib/private/Share/Constants.php',

lib/private/Files/FileInfo.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public function isEncrypted() {
233233
}
234234

235235
/**
236-
* Return the currently version used for the HMAC in the encryption app
236+
* Return the current version used for the HMAC in the encryption app
237237
*/
238238
public function getEncryptedVersion(): int {
239239
return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
@@ -243,11 +243,7 @@ public function getEncryptedVersion(): int {
243243
* @return int
244244
*/
245245
public function getPermissions() {
246-
$perms = (int) $this->data['permissions'];
247-
if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) {
248-
$perms = $perms & ~\OCP\Constants::PERMISSION_SHARE;
249-
}
250-
return $perms;
246+
return (int) $this->data['permissions'];
251247
}
252248

253249
/**

lib/private/Files/ObjectStore/HomeObjectStoreStorage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OC\Files\ObjectStore;
2727

2828
use OC\User\User;
29+
use OCP\IUser;
2930

3031
class HomeObjectStoreStorage extends ObjectStoreStorage implements \OCP\Files\IHomeStorage {
3132
/**
@@ -61,7 +62,7 @@ public function getOwner($path) {
6162
* @param string $path, optional
6263
* @return \OC\User\User
6364
*/
64-
public function getUser($path = null) {
65+
public function getUser($path = null): IUser {
6566
return $this->user;
6667
}
6768
}

lib/private/Files/SetupManager.php

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
use OC\Files\Storage\Wrapper\PermissionsMask;
3535
use OC\Files\Storage\Wrapper\Quota;
3636
use OC\Lockdown\Filesystem\NullStorage;
37+
use OC\Share\Share;
38+
use OC\Share20\ShareDisableChecker;
3739
use OC_App;
3840
use OC_Hook;
3941
use OC_Util;
42+
use OCA\Files_Sharing\ISharedStorage;
4043
use OCP\Constants;
4144
use OCP\Diagnostics\IEventLogger;
4245
use OCP\EventDispatcher\IEventDispatcher;
@@ -64,52 +67,33 @@
6467

6568
class SetupManager {
6669
private bool $rootSetup = false;
67-
private IEventLogger $eventLogger;
68-
private MountProviderCollection $mountProviderCollection;
69-
private IMountManager $mountManager;
70-
private IUserManager $userManager;
7170
// List of users for which at least one mount is setup
7271
private array $setupUsers = [];
7372
// List of users for which all mounts are setup
7473
private array $setupUsersComplete = [];
7574
/** @var array<string, string[]> */
7675
private array $setupUserMountProviders = [];
77-
private IEventDispatcher $eventDispatcher;
78-
private IUserMountCache $userMountCache;
79-
private ILockdownManager $lockdownManager;
80-
private IUserSession $userSession;
8176
private ICache $cache;
82-
private LoggerInterface $logger;
83-
private IConfig $config;
8477
private bool $listeningForProviders;
8578
private array $fullSetupRequired = [];
8679
private bool $setupBuiltinWrappersDone = false;
8780

8881
public function __construct(
89-
IEventLogger $eventLogger,
90-
MountProviderCollection $mountProviderCollection,
91-
IMountManager $mountManager,
92-
IUserManager $userManager,
93-
IEventDispatcher $eventDispatcher,
94-
IUserMountCache $userMountCache,
95-
ILockdownManager $lockdownManager,
96-
IUserSession $userSession,
82+
private IEventLogger $eventLogger,
83+
private MountProviderCollection $mountProviderCollection,
84+
private IMountManager $mountManager,
85+
private IUserManager $userManager,
86+
private IEventDispatcher $eventDispatcher,
87+
private IUserMountCache $userMountCache,
88+
private ILockdownManager $lockdownManager,
89+
private IUserSession $userSession,
9790
ICacheFactory $cacheFactory,
98-
LoggerInterface $logger,
99-
IConfig $config
91+
private LoggerInterface $logger,
92+
private IConfig $config,
93+
private ShareDisableChecker $shareDisableChecker,
10094
) {
101-
$this->eventLogger = $eventLogger;
102-
$this->mountProviderCollection = $mountProviderCollection;
103-
$this->mountManager = $mountManager;
104-
$this->userManager = $userManager;
105-
$this->eventDispatcher = $eventDispatcher;
106-
$this->userMountCache = $userMountCache;
107-
$this->lockdownManager = $lockdownManager;
108-
$this->logger = $logger;
109-
$this->userSession = $userSession;
11095
$this->cache = $cacheFactory->createDistributed('setupmanager::');
11196
$this->listeningForProviders = false;
112-
$this->config = $config;
11397

11498
$this->setupListeners();
11599
}
@@ -139,15 +123,23 @@ private function setupBuiltinWrappers() {
139123
return $storage;
140124
});
141125

142-
Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
143-
if (!$mount->getOption('enable_sharing', true)) {
144-
return new PermissionsMask([
145-
'storage' => $storage,
146-
'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
147-
]);
126+
$reSharingEnabled = Share::isResharingAllowed();
127+
$user = $this->userSession->getUser();
128+
$sharingEnabledForUser = $user ? !$this->shareDisableChecker->sharingDisabledForUser($user->getUID()) : true;
129+
Filesystem::addStorageWrapper(
130+
'sharing_mask',
131+
function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) {
132+
$sharingEnabledForMount = $mount->getOption('enable_sharing', true);
133+
$isShared = $storage->instanceOfStorage(ISharedStorage::class);
134+
if (!$sharingEnabledForMount || !$sharingEnabledForUser || (!$reSharingEnabled && $isShared)) {
135+
return new PermissionsMask([
136+
'storage' => $storage,
137+
'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
138+
]);
139+
}
140+
return $storage;
148141
}
149-
return $storage;
150-
});
142+
);
151143

152144
// install storage availability wrapper, before most other wrappers
153145
Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, IStorage $storage) {

lib/private/Files/SetupManagerFactory.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace OC\Files;
2525

26+
use OC\Share20\ShareDisableChecker;
2627
use OCP\Diagnostics\IEventLogger;
2728
use OCP\EventDispatcher\IEventDispatcher;
2829
use OCP\Files\Config\IMountProviderCollection;
@@ -36,40 +37,21 @@
3637
use Psr\Log\LoggerInterface;
3738

3839
class SetupManagerFactory {
39-
private IEventLogger $eventLogger;
40-
private IMountProviderCollection $mountProviderCollection;
41-
private IUserManager $userManager;
42-
private IEventDispatcher $eventDispatcher;
43-
private IUserMountCache $userMountCache;
44-
private ILockdownManager $lockdownManager;
45-
private IUserSession $userSession;
4640
private ?SetupManager $setupManager;
47-
private ICacheFactory $cacheFactory;
48-
private LoggerInterface $logger;
49-
private IConfig $config;
5041

5142
public function __construct(
52-
IEventLogger $eventLogger,
53-
IMountProviderCollection $mountProviderCollection,
54-
IUserManager $userManager,
55-
IEventDispatcher $eventDispatcher,
56-
IUserMountCache $userMountCache,
57-
ILockdownManager $lockdownManager,
58-
IUserSession $userSession,
59-
ICacheFactory $cacheFactory,
60-
LoggerInterface $logger,
61-
IConfig $config
43+
private IEventLogger $eventLogger,
44+
private IMountProviderCollection $mountProviderCollection,
45+
private IUserManager $userManager,
46+
private IEventDispatcher $eventDispatcher,
47+
private IUserMountCache $userMountCache,
48+
private ILockdownManager $lockdownManager,
49+
private IUserSession $userSession,
50+
private ICacheFactory $cacheFactory,
51+
private LoggerInterface $logger,
52+
private IConfig $config,
53+
private ShareDisableChecker $shareDisableChecker,
6254
) {
63-
$this->eventLogger = $eventLogger;
64-
$this->mountProviderCollection = $mountProviderCollection;
65-
$this->userManager = $userManager;
66-
$this->eventDispatcher = $eventDispatcher;
67-
$this->userMountCache = $userMountCache;
68-
$this->lockdownManager = $lockdownManager;
69-
$this->userSession = $userSession;
70-
$this->cacheFactory = $cacheFactory;
71-
$this->logger = $logger;
72-
$this->config = $config;
7355
$this->setupManager = null;
7456
}
7557

@@ -86,7 +68,8 @@ public function create(IMountManager $mountManager): SetupManager {
8668
$this->userSession,
8769
$this->cacheFactory,
8870
$this->logger,
89-
$this->config
71+
$this->config,
72+
$this->shareDisableChecker,
9073
);
9174
}
9275
return $this->setupManager;

lib/private/Files/Storage/Home.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OC\Files\Storage;
2727

2828
use OC\Files\Cache\HomePropagator;
29+
use OCP\IUser;
2930

3031
/**
3132
* Specialized version of Local storage for home directory usage
@@ -94,7 +95,7 @@ public function getPropagator($storage = null) {
9495
*
9596
* @return \OC\User\User owner of this home storage
9697
*/
97-
public function getUser() {
98+
public function getUser(): IUser {
9899
return $this->user;
99100
}
100101

lib/private/Server.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
use OC\Security\VerificationToken\VerificationToken;
148148
use OC\Session\CryptoWrapper;
149149
use OC\Share20\ProviderFactory;
150+
use OC\Share20\ShareDisableChecker;
150151
use OC\Share20\ShareHelper;
151152
use OC\SpeechToText\SpeechToTextManager;
152153
use OC\SystemTag\ManagerFactory as SystemTagManagerFactory;
@@ -1252,7 +1253,8 @@ public function __construct($webRoot, \OC\Config $config) {
12521253
$c->get('ThemingDefaults'),
12531254
$c->get(IEventDispatcher::class),
12541255
$c->get(IUserSession::class),
1255-
$c->get(KnownUserService::class)
1256+
$c->get(KnownUserService::class),
1257+
$c->get(ShareDisableChecker::class)
12561258
);
12571259

12581260
return $manager;

0 commit comments

Comments
 (0)