diff options
author | Joas Schilling <coding@schilljs.com> | 2019-02-11 12:57:38 +0100 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2019-03-01 20:56:19 +0100 |
commit | 59c92a75139b9db1cc1171f41bac971c2c528873 (patch) | |
tree | c897e2fcec33a4c3509539f67198a90c17efc307 /lib/private/Collaboration | |
parent | ece471de360ebbebee6b098a20d65042dd69928c (diff) | |
download | nextcloud-server-59c92a75139b9db1cc1171f41bac971c2c528873.tar.gz nextcloud-server-59c92a75139b9db1cc1171f41bac971c2c528873.zip |
Further work on the access cache
Searching for all is still a problem
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Collaboration')
-rw-r--r-- | lib/private/Collaboration/Resources/Collection.php | 94 | ||||
-rw-r--r-- | lib/private/Collaboration/Resources/Manager.php | 184 | ||||
-rw-r--r-- | lib/private/Collaboration/Resources/Resource.php | 42 |
3 files changed, 252 insertions, 68 deletions
diff --git a/lib/private/Collaboration/Resources/Collection.php b/lib/private/Collaboration/Resources/Collection.php index c538580b8f8..6d37cebdc2f 100644 --- a/lib/private/Collaboration/Resources/Collection.php +++ b/lib/private/Collaboration/Resources/Collection.php @@ -34,7 +34,7 @@ use OCP\IUser; class Collection implements ICollection { - /** @var IManager */ + /** @var IManager|Manager */ protected $manager; /** @var IDBConnection */ @@ -46,6 +46,9 @@ class Collection implements ICollection { /** @var string */ protected $name; + /** @var IUser|null */ + protected $userForAccess; + /** @var bool|null */ protected $access; @@ -57,12 +60,14 @@ class Collection implements ICollection { IDBConnection $connection, int $id, string $name, - ?bool $access + ?IUser $userForAccess = null, + ?bool $access = null ) { $this->manager = $manager; $this->connection = $connection; $this->id = $id; $this->name = $name; + $this->userForAccess = $userForAccess; $this->access = $access; $this->resources = []; } @@ -84,21 +89,26 @@ class Collection implements ICollection { } /** + * @param string $name + * @since 16.0.0 + */ + public function setName(string $name): void { + $query = $this->connection->getQueryBuilder(); + $query->update(Manager::TABLE_COLLECTIONS) + ->set('name', $query->createNamedParameter($name)) + ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); + $query->execute(); + + $this->name = $name; + } + + /** * @return IResource[] * @since 16.0.0 */ public function getResources(): array { if (empty($this->resources)) { - $query = $this->connection->getQueryBuilder(); - $query->select('resource_type', 'resource_id') - ->from('collres_resources') - ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); - - $result = $query->execute(); - while ($row = $result->fetch()) { - $this->resources[] = $this->manager->getResource($row['resource_type'], $row['resource_id']); - } - $result->closeCursor(); + $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess); } return $this->resources; @@ -111,17 +121,17 @@ class Collection implements ICollection { * @throws ResourceException when the resource is already part of the collection * @since 16.0.0 */ - public function addResource(IResource $resource) { + public function addResource(IResource $resource): void { array_map(function(IResource $r) use ($resource) { if ($this->isSameResource($r, $resource)) { throw new ResourceException('Already part of the collection'); } - }, $this->resources); + }, $this->getResources()); $this->resources[] = $resource; $query = $this->connection->getQueryBuilder(); - $query->insert('collres_resources') + $query->insert(Manager::TABLE_RESOURCES) ->values([ 'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT), 'resource_type' => $query->createNamedParameter($resource->getType()), @@ -133,6 +143,8 @@ class Collection implements ICollection { } catch (ConstraintViolationException $e) { throw new ResourceException('Already part of the collection'); } + + $this->manager->invalidateAccessCacheForCollection($this); } /** @@ -141,13 +153,13 @@ class Collection implements ICollection { * @param IResource $resource * @since 16.0.0 */ - public function removeResource(IResource $resource) { - $this->resources = array_filter($this->resources, function(IResource $r) use ($resource) { + public function removeResource(IResource $resource): void { + $this->resources = array_filter($this->getResources(), function(IResource $r) use ($resource) { return !$this->isSameResource($r, $resource); }); $query = $this->connection->getQueryBuilder(); - $query->delete('collres_resources') + $query->delete(Manager::TABLE_RESOURCES) ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType()))) ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); @@ -155,27 +167,48 @@ class Collection implements ICollection { if (empty($this->resources)) { $this->removeCollection(); + } else { + $this->manager->invalidateAccessCacheForCollection($this); } + } /** * Can a user/guest access the collection * - * @param IUser $user + * @param IUser|null $user * @return bool * @since 16.0.0 */ - public function canAccess(IUser $user = null): bool { - if ($this->access === null) { - $this->access = false; - foreach ($this->getResources() as $resource) { - if ($resource->canAccess($user)) { - $this->access = true; - } - } + public function canAccess(?IUser $user): bool { + if ($user instanceof IUser) { + return $this->canUserAccess($user); + } + return $this->canGuestAccess(); + } + + protected function canUserAccess(IUser $user): bool { + if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { + return $this->access; } - return $this->access; + $access = $this->manager->canAccessCollection($this, $user); + if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { + $this->access = $access; + } + return $access; + } + + protected function canGuestAccess(): bool { + if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { + return $this->access; + } + + $access = $this->manager->canAccessCollection($this, null); + if (!$this->userForAccess instanceof IUser) { + $this->access = $access; + } + return $access; } protected function isSameResource(IResource $resource1, IResource $resource2): bool { @@ -183,12 +216,13 @@ class Collection implements ICollection { $resource1->getId() === $resource2->getId(); } - protected function removeCollection() { + protected function removeCollection(): void { $query = $this->connection->getQueryBuilder(); - $query->delete('collres_collections') + $query->delete(Manager::TABLE_COLLECTIONS) ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); $query->execute(); + $this->manager->invalidateAccessCacheForCollection($this); $this->id = 0; } } diff --git a/lib/private/Collaboration/Resources/Manager.php b/lib/private/Collaboration/Resources/Manager.php index ae42f272c05..b7aeb221d88 100644 --- a/lib/private/Collaboration/Resources/Manager.php +++ b/lib/private/Collaboration/Resources/Manager.php @@ -35,6 +35,10 @@ use OCP\IUser; class Manager implements IManager { + public const TABLE_COLLECTIONS = 'collres_collections'; + public const TABLE_RESOURCES = 'collres_resources'; + public const TABLE_ACCESS_CACHE = 'collres_accesscache'; + /** @var IDBConnection */ protected $connection; @@ -54,7 +58,7 @@ class Manager implements IManager { public function getCollection(int $id): ICollection { $query = $this->connection->getQueryBuilder(); $query->select('*') - ->from('collres_collections') + ->from(self::TABLE_COLLECTIONS) ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); $result = $query->execute(); $row = $result->fetch(); @@ -68,6 +72,45 @@ class Manager implements IManager { } /** + * @param int $id + * @param IUser|null $user + * @return ICollection + * @throws CollectionException when the collection could not be found + * @since 16.0.0 + */ + public function getCollectionForUser(int $id, ?IUser $user): ICollection { + $query = $this->connection->getQueryBuilder(); + $userId = $user instanceof IUser ? $user->getUID() : ''; + + $query->select('*') + ->from(self::TABLE_COLLECTIONS) + ->leftJoin( + 'r', self::TABLE_ACCESS_CACHE, 'a', + $query->expr()->andX( + $query->expr()->eq('c.id', 'a.resource_id'), + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) + ) + ) + ->where($query->expr()->eq('c.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); + $result = $query->execute(); + $row = $result->fetch(); + $result->closeCursor(); + + if (!$row) { + throw new CollectionException('Collection not found'); + } + + + $access = $row['access'] === null ? null : (bool) $row['access']; + if ($user instanceof IUser) { + $access = [$user->getUID() => $access]; + return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $access, null); + } + + return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], [], $access); + } + + /** * @param IUser $user * @param string $filter * @param int $limit @@ -78,7 +121,7 @@ class Manager implements IManager { public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array { $query = $this->connection->getQueryBuilder(); $query->select('*') - ->from('collres_collections') + ->from(self::TABLE_COLLECTIONS) ->where($query->expr()->iLike('name', $query->createNamedParameter($filter, IQueryBuilder::PARAM_STR))) ->setMaxResults($limit) ->setFirstResult($start); @@ -105,7 +148,7 @@ class Manager implements IManager { */ public function newCollection(string $name): ICollection { $query = $this->connection->getQueryBuilder(); - $query->insert('collres_collections') + $query->insert(self::TABLE_COLLECTIONS) ->values([ 'name' => $query->createNamedParameter($name), ]); @@ -120,11 +163,82 @@ class Manager implements IManager { * @return IResource * @since 16.0.0 */ - public function getResource(string $type, string $id): IResource { + public function createResource(string $type, string $id): IResource { return new Resource($this, $this->connection, $type, $id); } /** + * @param string $type + * @param string $id + * @param IUser|null $user + * @return IResource + * @throws ResourceException + * @since 16.0.0 + */ + public function getResourceForUser(string $type, string $id, ?IUser $user): IResource { + $query = $this->connection->getQueryBuilder(); + $userId = $user instanceof IUser ? $user->getUID() : ''; + + $query->select('r.*', 'a.access') + ->from(self::TABLE_RESOURCES, 'r') + ->leftJoin( + 'r', self::TABLE_ACCESS_CACHE, 'a', + $query->expr()->andX( + $query->expr()->eq('r.id', 'a.resource_id'), + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) + ) + ) + ->where($query->expr()->eq('r.resource_type', $query->createNamedParameter($type, IQueryBuilder::PARAM_STR))) + ->andWhere($query->expr()->eq('r.resource_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_STR))); + $result = $query->execute(); + $row = $result; + $result->closeCursor(); + + if (!$row) { + throw new ResourceException('Resource not found'); + } + + $access = $row['access'] === null ? null : (bool) $row['access']; + if ($user instanceof IUser) { + return new Resource($this, $this->connection, $type, $id, $user, $access); + } + + return new Resource($this, $this->connection, $type, $id, null, $access); + } + + /** + * @param ICollection $collection + * @param IUser|null $user + * @return IResource[] + * @since 16.0.0 + */ + public function getResourcesByCollectionForUser(ICollection $collection, ?IUser $user): array { + $query = $this->connection->getQueryBuilder(); + $userId = $user instanceof IUser ? $user->getUID() : ''; + + $query->select('r.*', 'a.access') + ->from(self::TABLE_RESOURCES, 'r') + ->leftJoin( + 'r', self::TABLE_ACCESS_CACHE, 'a', + $query->expr()->andX( + $query->expr()->eq('r.id', 'a.resource_id'), + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) + ) + ) + ->where($query->expr()->eq('r.collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT))); + + $resources = []; + $result = $query->execute(); + while ($row = $result->fetch()) { + $access = $row['access'] === null ? null : (bool) $row['access']; + $resources[] = new Resource($this, $this->connection, $row['resource_type'], $row['resource_id'], $user, $access); + } + $result->closeCursor(); + + return $resources; + } + + /** * @return IProvider[] * @since 16.0.0 */ @@ -174,30 +288,53 @@ class Manager implements IManager { * Can a user/guest access the collection * * @param IResource $resource - * @param IUser $user + * @param IUser|null $user * @return bool * @since 16.0.0 */ - public function canAccess(IResource $resource, IUser $user = null): bool { + public function canAccessResource(IResource $resource, ?IUser $user): bool { + $access = false; foreach ($this->getProviders() as $provider) { if ($provider->getType() === $resource->getType()) { try { if ($provider->canAccess($resource, $user)) { - return true; + $access = true; + break; } } catch (ResourceException $e) { } } } - return false; + $this->cacheAccessForResource($resource, $user, $access); + return $access; + } + + /** + * Can a user/guest access the collection + * + * @param ICollection $collection + * @param IUser|null $user + * @return bool + * @since 16.0.0 + */ + public function canAccessCollection(ICollection $collection, ?IUser $user): bool { + $access = false; + foreach ($collection->getResources() as $resource) { + if ($resource->canAccess($user)) { + $access = true; + } + } + + $this->cacheAccessForCollection($collection, $user, $access); + return $access; } public function cacheAccessForResource(IResource $resource, ?IUser $user, bool $access): void { $query = $this->connection->getQueryBuilder(); $userId = $user instanceof IUser ? $user->getUID() : ''; - $query->insert('collres_accesscache') + $query->insert(self::TABLE_ACCESS_CACHE) ->values([ 'user_id' => $query->createNamedParameter($userId), 'resource_id' => $query->createNamedParameter($resource->getId()), @@ -210,7 +347,7 @@ class Manager implements IManager { $query = $this->connection->getQueryBuilder(); $userId = $user instanceof IUser ? $user->getUID() : ''; - $query->insert('collres_accesscache') + $query->insert(self::TABLE_ACCESS_CACHE) ->values([ 'user_id' => $query->createNamedParameter($userId), 'collection_id' => $query->createNamedParameter($collection->getId()), @@ -223,7 +360,7 @@ class Manager implements IManager { $query = $this->connection->getQueryBuilder(); $userId = $user instanceof IUser ? $user->getUID() : ''; - $query->delete('collres_accesscache') + $query->delete(self::TABLE_ACCESS_CACHE) ->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))); $query->execute(); } @@ -231,7 +368,7 @@ class Manager implements IManager { public function invalidateAccessCacheForResource(IResource $resource): void { $query = $this->connection->getQueryBuilder(); - $query->delete('collres_accesscache') + $query->delete(self::TABLE_ACCESS_CACHE) ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); $query->execute(); @@ -240,10 +377,10 @@ class Manager implements IManager { } } - protected function invalidateAccessCacheForCollection(ICollection $collection): void { + public function invalidateAccessCacheForCollection(ICollection $collection): void { $query = $this->connection->getQueryBuilder(); - $query->delete('collres_accesscache') + $query->delete(self::TABLE_ACCESS_CACHE) ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId()))); $query->execute(); } @@ -252,7 +389,7 @@ class Manager implements IManager { $query = $this->connection->getQueryBuilder(); $userId = $user instanceof IUser ? $user->getUID() : ''; - $query->delete('collres_accesscache') + $query->delete(self::TABLE_ACCESS_CACHE) ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))) ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); $query->execute(); @@ -266,7 +403,7 @@ class Manager implements IManager { $query = $this->connection->getQueryBuilder(); $userId = $user instanceof IUser ? $user->getUID() : ''; - $query->delete('collres_accesscache') + $query->delete(self::TABLE_ACCESS_CACHE) ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId()))) ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); $query->execute(); @@ -309,19 +446,4 @@ class Manager implements IManager { return ''; } - - /** - * @param string $name - * @return ICollection - * @since 16.0.0 - */ - public function renameCollection(int $id, string $name): ICollection { - $query = $this->connection->getQueryBuilder(); - $query->update('collres_collections') - ->set('name', $query->createNamedParameter($name)) - ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); - $query->execute(); - - return new Collection($this, $this->connection, $id, $name); - } } diff --git a/lib/private/Collaboration/Resources/Resource.php b/lib/private/Collaboration/Resources/Resource.php index bfc63fefdfc..d9c72ca2039 100644 --- a/lib/private/Collaboration/Resources/Resource.php +++ b/lib/private/Collaboration/Resources/Resource.php @@ -43,6 +43,9 @@ class Resource implements IResource { /** @var string */ protected $id; + /** @var IUser|null */ + protected $userForAccess; + /** @var bool|null */ protected $access; @@ -60,12 +63,14 @@ class Resource implements IResource { IDBConnection $connection, string $type, string $id, - ?bool $access + ?IUser $userForAccess = null, + ?bool $access = null ) { $this->manager = $manager; $this->connection = $connection; $this->type = $type; $this->id = $id; + $this->userForAccess = $userForAccess; $this->access = $access; } @@ -117,19 +122,42 @@ class Resource implements IResource { return $this->link; } - /** * Can a user/guest access the resource * - * @param IUser $user + * @param IUser|null $user * @return bool * @since 16.0.0 */ - public function canAccess(IUser $user = null): bool { - if ($this->access === null) { - $this->access = $this->manager->canAccess($this, $user); + public function canAccess(?IUser $user): bool { + if ($user instanceof IUser) { + return $this->canUserAccess($user); + } + return $this->canGuestAccess(); + } + + protected function canUserAccess(IUser $user): bool { + if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { + return $this->access; + } + + $access = $this->manager->canAccessResource($this, $user); + if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { + $this->access = $access; + } + return $access; + } + + protected function canGuestAccess(): bool { + if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { + return $this->access; + } + + $access = $this->manager->canAccessResource($this, null); + if (!$this->userForAccess instanceof IUser) { + $this->access = $access; } - return $this->access; + return $access; } /** |