From 995cad0ceebc2efd1b10c0e9e16462ccca773aa8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 22 Feb 2019 09:48:43 +0100 Subject: [PATCH] Cache the access manually too Signed-off-by: Joas Schilling --- .../Collaboration/Resources/Manager.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lib/private/Collaboration/Resources/Manager.php b/lib/private/Collaboration/Resources/Manager.php index 4444d92419..0555be5d7a 100644 --- a/lib/private/Collaboration/Resources/Manager.php +++ b/lib/private/Collaboration/Resources/Manager.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace OC\Collaboration\Resources; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use OCP\Collaboration\Resources\CollectionException; use OCP\Collaboration\Resources\ICollection; use OCP\Collaboration\Resources\IManager; @@ -308,6 +309,11 @@ class Manager implements IManager { * @since 16.0.0 */ public function canAccessResource(IResource $resource, ?IUser $user): bool { + $access = $this->checkAccessCacheForUserByResource($resource, $user); + if (\is_bool($access)) { + return $access; + } + $access = false; foreach ($this->getProviders() as $provider) { if ($provider->getType() === $resource->getType()) { @@ -334,6 +340,11 @@ class Manager implements IManager { * @since 16.0.0 */ public function canAccessCollection(ICollection $collection, ?IUser $user): bool { + $access = $this->checkAccessCacheForUserByCollection($collection, $user); + if (\is_bool($access)) { + return $access; + } + $access = false; foreach ($collection->getResources() as $resource) { if ($resource->canAccess($user)) { @@ -345,6 +356,46 @@ class Manager implements IManager { return $access; } + protected function checkAccessCacheForUserByResource(IResource $resource, ?IUser $user): ?bool { + $query = $this->connection->getQueryBuilder(); + $userId = $user instanceof IUser ? $user->getUID() : ''; + + $query->select('access') + ->from(self::TABLE_ACCESS_CACHE) + ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId(), IQueryBuilder::PARAM_STR))) + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) + ->setMaxResults(1); + + $hasAccess = null; + $result = $query->execute(); + if ($row = $result->fetch()) { + $hasAccess = (bool) $row['access']; + } + $result->closeCursor(); + + return $hasAccess; + } + + protected function checkAccessCacheForUserByCollection(ICollection $collection, ?IUser $user): ?bool { + $query = $this->connection->getQueryBuilder(); + $userId = $user instanceof IUser ? $user->getUID() : ''; + + $query->select('access') + ->from(self::TABLE_ACCESS_CACHE) + ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) + ->setMaxResults(1); + + $hasAccess = null; + $result = $query->execute(); + if ($row = $result->fetch()) { + $hasAccess = (bool) $row['access']; + } + $result->closeCursor(); + + return $hasAccess; + } + public function cacheAccessForResource(IResource $resource, ?IUser $user, bool $access): void { $query = $this->connection->getQueryBuilder(); $userId = $user instanceof IUser ? $user->getUID() : '';