From ae3d0d96fe7e273f6dfae3a5ca4754b5da4353cc Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 3 May 2016 11:58:33 +0200 Subject: [PATCH 1/3] Optimize isSharingDisabledForuser --- lib/private/Share20/Manager.php | 40 ++++++++++++++++++++------------- lib/public/Util.php | 14 +++++++----- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index ceaaa58cf6..2b1c7d828f 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -24,25 +24,24 @@ namespace OC\Share20; +use OC\Cache\CappedMemoryCache; use OC\Files\Mount\MoveableMount; -use OCP\Files\IRootFolder; -use OCP\Files\NotFoundException; -use OCP\IUserManager; -use OCP\Share\IManager; -use OCP\Share\IProviderFactory; -use OC\Share20\Exception\BackendError; -use OCP\IConfig; -use OCP\IL10N; -use OCP\ILogger; -use OCP\Security\ISecureRandom; -use OCP\Security\IHasher; -use OCP\Files\Mount\IMountManager; -use OCP\IGroupManager; use OCP\Files\File; use OCP\Files\Folder; - -use OCP\Share\Exceptions\ShareNotFound; +use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountManager; +use OCP\Files\NotFoundException; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IUserManager; +use OCP\Security\IHasher; +use OCP\Security\ISecureRandom; use OCP\Share\Exceptions\GenericShareException; +use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IManager; +use OCP\Share\IProviderFactory; /** * This class is the communication hub for all sharing related operations. @@ -69,6 +68,9 @@ class Manager implements IManager { private $userManager; /** @var IRootFolder */ private $rootFolder; + /** @var CappedMemoryCache */ + private $sharingDisabledForUsersCache; + /** * Manager constructor. @@ -106,6 +108,7 @@ class Manager implements IManager { $this->factory = $factory; $this->userManager = $userManager; $this->rootFolder = $rootFolder; + $this->sharingDisabledForUsersCache = new CappedMemoryCache(); } /** @@ -1209,6 +1212,10 @@ class Manager implements IManager { * @return bool */ public function sharingDisabledForUser($userId) { + if (isset($this->sharingDisabledForUsersCache[$userId])) { + return $this->sharingDisabledForUsersCache[$userId]; + } + if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); $excludedGroups = json_decode($groupsList); @@ -1224,10 +1231,13 @@ class Manager implements IManager { // if the user is only in groups which are disabled for sharing then // sharing is also disabled for the user if (empty($remainingGroups)) { + $this->sharingDisabledForUsersCache[$userId] = true; return true; } } } + + $this->sharingDisabledForUsersCache[$userId] = false; return false; } diff --git a/lib/public/Util.php b/lib/public/Util.php index eb573168e1..86f8456521 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -63,6 +63,9 @@ class Util { const ERROR=3; const FATAL=4; + /** \OCP\Share\IManager */ + private static $shareManager; + /** * get the current installed version of ownCloud * @return array @@ -171,13 +174,14 @@ class Util { * * @return boolean * @since 7.0.0 + * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser */ public static function isSharingDisabledForUser() { - return \OC_Util::isSharingDisabledForUser( - \OC::$server->getConfig(), - \OC::$server->getGroupManager(), - \OC::$server->getUserSession()->getUser() - ); + if (self::$shareManager === null) { + self::$shareManager = \OC::$server->getShareManager(); + } + + return self::$shareManager->sharingDisabledForUser(\OC::$server->getUserSession()->getUser()->getUID()); } /** From 28d070730d9de5aec690f152a95dbf1da123d2ee Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 3 May 2016 14:05:51 +0200 Subject: [PATCH 2/3] Fix case with no user --- lib/private/Share20/Manager.php | 4 ++++ lib/public/Util.php | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index 2b1c7d828f..e2730f4d5f 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -1212,6 +1212,10 @@ class Manager implements IManager { * @return bool */ public function sharingDisabledForUser($userId) { + if ($userId === null) { + return false; + } + if (isset($this->sharingDisabledForUsersCache[$userId])) { return $this->sharingDisabledForUsersCache[$userId]; } diff --git a/lib/public/Util.php b/lib/public/Util.php index 86f8456521..687f4e78f6 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -181,7 +181,12 @@ class Util { self::$shareManager = \OC::$server->getShareManager(); } - return self::$shareManager->sharingDisabledForUser(\OC::$server->getUserSession()->getUser()->getUID()); + $user = \OC::$server->getUserSession()->getUser(); + if ($user !== null) { + $user = $user->getUID(); + } + + return self::$shareManager->sharingDisabledForUser($user); } /** From ea4c5e6e0abb978d8cc49e604966609e97bec525 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 14 Jun 2016 11:03:32 +0200 Subject: [PATCH 3/3] Fix unit tests --- tests/lib/Files/ViewTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php index 59b17b8395..87feb63888 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -7,6 +7,7 @@ namespace Test\Files; +use OC\Cache\CappedMemoryCache; use OC\Files\Cache\Watcher; use OC\Files\Storage\Common; use OC\Files\Mount\MountPoint; @@ -269,6 +270,9 @@ class ViewTest extends \Test\TestCase { * @dataProvider sharingDisabledPermissionProvider */ public function testRemoveSharePermissionWhenSharingDisabledForUser($excludeGroups, $excludeGroupsList, $expectedShareable) { + // Reset sharing disabled for users cache + $this->invokePrivate(\OC::$server->getShareManager(), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]); + $appConfig = \OC::$server->getAppConfig(); $oldExcludeGroupsFlag = $appConfig->getValue('core', 'shareapi_exclude_groups', 'no'); $oldExcludeGroupsList = $appConfig->getValue('core', 'shareapi_exclude_groups_list', ''); @@ -290,6 +294,9 @@ class ViewTest extends \Test\TestCase { $appConfig->setValue('core', 'shareapi_exclude_groups', $oldExcludeGroupsFlag); $appConfig->setValue('core', 'shareapi_exclude_groups_list', $oldExcludeGroupsList); + + // Reset sharing disabled for users cache + $this->invokePrivate(\OC::$server->getShareManager(), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]); } public function testCacheIncompleteFolder() {