2018-09-14 13:34:24 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Collaboration\Resources;
|
|
|
|
|
|
|
|
|
2019-02-22 11:48:43 +03:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2019-02-22 17:11:09 +03:00
|
|
|
use OCP\AppFramework\QueryException;
|
2018-10-24 15:25:35 +03:00
|
|
|
use OCP\Collaboration\Resources\CollectionException;
|
2018-09-14 13:34:24 +03:00
|
|
|
use OCP\Collaboration\Resources\ICollection;
|
|
|
|
use OCP\Collaboration\Resources\IManager;
|
2018-10-17 21:03:44 +03:00
|
|
|
use OCP\Collaboration\Resources\IProvider;
|
2018-09-14 13:34:24 +03:00
|
|
|
use OCP\Collaboration\Resources\IResource;
|
2018-10-17 21:03:44 +03:00
|
|
|
use OCP\Collaboration\Resources\ResourceException;
|
2018-10-24 15:25:35 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2018-09-14 13:34:24 +03:00
|
|
|
use OCP\IDBConnection;
|
2019-02-22 17:11:09 +03:00
|
|
|
use OCP\ILogger;
|
2018-10-17 21:03:44 +03:00
|
|
|
use OCP\IUser;
|
2018-09-14 13:34:24 +03:00
|
|
|
|
|
|
|
class Manager implements IManager {
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
public const TABLE_COLLECTIONS = 'collres_collections';
|
|
|
|
public const TABLE_RESOURCES = 'collres_resources';
|
|
|
|
public const TABLE_ACCESS_CACHE = 'collres_accesscache';
|
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $connection;
|
2019-02-22 17:11:09 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
2018-09-14 13:34:24 +03:00
|
|
|
|
2019-02-22 17:11:09 +03:00
|
|
|
/** @var string[] */
|
2019-01-24 13:43:18 +03:00
|
|
|
protected $providers = [];
|
|
|
|
|
2019-02-22 17:11:09 +03:00
|
|
|
/** @var IProvider[] */
|
|
|
|
protected $providerInstances = [];
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $connection, ILogger $logger) {
|
2018-09-14 13:34:24 +03:00
|
|
|
$this->connection = $connection;
|
2019-02-22 17:11:09 +03:00
|
|
|
$this->logger = $logger;
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return ICollection
|
2018-10-24 15:25:35 +03:00
|
|
|
* @throws CollectionException when the collection could not be found
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-09-14 13:34:24 +03:00
|
|
|
*/
|
|
|
|
public function getCollection(int $id): ICollection {
|
2018-10-24 15:25:35 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
2019-02-11 14:57:38 +03:00
|
|
|
->from(self::TABLE_COLLECTIONS)
|
2018-10-24 15:25:35 +03:00
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
|
|
|
$result = $query->execute();
|
|
|
|
$row = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if (!$row) {
|
|
|
|
throw new CollectionException('Collection not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name']);
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
/**
|
|
|
|
* @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('*')
|
2019-02-16 11:39:16 +03:00
|
|
|
->from(self::TABLE_COLLECTIONS, 'c')
|
2019-02-11 14:57:38 +03:00
|
|
|
->leftJoin(
|
2019-02-16 11:39:16 +03:00
|
|
|
'c', self::TABLE_ACCESS_CACHE, 'a',
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->expr()->andX(
|
2019-02-16 11:39:16 +03:00
|
|
|
$query->expr()->eq('c.id', 'a.collection_id'),
|
2019-02-11 14:57:38 +03:00
|
|
|
$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) {
|
2019-02-22 11:58:38 +03:00
|
|
|
return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access);
|
2019-02-11 14:57:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 11:39:16 +03:00
|
|
|
return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access);
|
2019-02-11 14:57:38 +03:00
|
|
|
}
|
|
|
|
|
2019-01-29 14:23:14 +03:00
|
|
|
/**
|
2019-02-07 18:04:15 +03:00
|
|
|
* @param IUser $user
|
|
|
|
* @param string $filter
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $start
|
|
|
|
* @return ICollection[]
|
|
|
|
* @since 16.0.0
|
2019-01-29 14:23:14 +03:00
|
|
|
*/
|
|
|
|
public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2019-02-11 15:04:27 +03:00
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
|
|
|
$query->select('c.*', 'a.access')
|
2019-02-16 11:39:16 +03:00
|
|
|
->from(self::TABLE_COLLECTIONS, 'c')
|
2019-02-11 15:04:27 +03:00
|
|
|
->leftJoin(
|
2019-02-16 11:39:16 +03:00
|
|
|
'c', self::TABLE_ACCESS_CACHE, 'a',
|
2019-02-11 15:04:27 +03:00
|
|
|
$query->expr()->andX(
|
2019-02-16 11:39:16 +03:00
|
|
|
$query->expr()->eq('c.id', 'a.collection_id'),
|
2019-02-11 15:04:27 +03:00
|
|
|
$query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
|
|
|
|
)
|
|
|
|
)
|
2019-03-28 11:33:52 +03:00
|
|
|
->where($query->expr()->eq('a.access', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
|
2019-02-11 15:10:33 +03:00
|
|
|
->orderBy('c.id')
|
2019-01-29 14:23:14 +03:00
|
|
|
->setMaxResults($limit)
|
|
|
|
->setFirstResult($start);
|
2019-03-28 11:33:52 +03:00
|
|
|
|
|
|
|
if ($filter !== '') {
|
|
|
|
$query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%')));
|
|
|
|
}
|
|
|
|
|
2019-01-29 14:23:14 +03:00
|
|
|
$result = $query->execute();
|
|
|
|
$collections = [];
|
2019-02-11 15:04:27 +03:00
|
|
|
|
|
|
|
$foundResults = 0;
|
2019-01-29 14:23:14 +03:00
|
|
|
while ($row = $result->fetch()) {
|
2019-02-11 15:04:27 +03:00
|
|
|
$foundResults++;
|
|
|
|
$access = $row['access'] === null ? null : (bool) $row['access'];
|
|
|
|
$collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access);
|
2019-01-29 14:23:14 +03:00
|
|
|
if ($collection->canAccess($user)) {
|
|
|
|
$collections[] = $collection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
2019-02-11 15:04:27 +03:00
|
|
|
if (empty($collections) && $foundResults === $limit) {
|
2019-03-28 11:27:05 +03:00
|
|
|
return $this->searchCollections($user, $filter, $limit, $start + $limit);
|
2019-02-11 15:04:27 +03:00
|
|
|
}
|
2019-01-29 14:23:14 +03:00
|
|
|
|
|
|
|
return $collections;
|
|
|
|
}
|
|
|
|
|
2018-10-18 14:00:25 +03:00
|
|
|
/**
|
2018-10-24 15:25:35 +03:00
|
|
|
* @param string $name
|
2018-10-18 14:00:25 +03:00
|
|
|
* @return ICollection
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-18 14:00:25 +03:00
|
|
|
*/
|
2018-10-24 15:25:35 +03:00
|
|
|
public function newCollection(string $name): ICollection {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->insert(self::TABLE_COLLECTIONS)
|
2018-10-29 13:18:38 +03:00
|
|
|
->values([
|
|
|
|
'name' => $query->createNamedParameter($name),
|
|
|
|
]);
|
2018-10-24 15:25:35 +03:00
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
return new Collection($this, $this->connection, $query->getLastInsertId(), $name);
|
2018-10-18 14:00:25 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param string $id
|
|
|
|
* @return IResource
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-09-14 13:34:24 +03:00
|
|
|
*/
|
2019-02-11 14:57:38 +03:00
|
|
|
public function createResource(string $type, string $id): IResource {
|
2018-09-14 13:34:24 +03:00
|
|
|
return new Resource($this, $this->connection, $type, $id);
|
|
|
|
}
|
2018-10-17 21:03:44 +03:00
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
/**
|
|
|
|
* @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(
|
2019-02-16 11:39:16 +03:00
|
|
|
$query->expr()->eq('r.resource_id', 'a.resource_id'),
|
2019-02-22 13:37:04 +03:00
|
|
|
$query->expr()->eq('r.resource_type', 'a.resource_type'),
|
2019-02-11 14:57:38 +03:00
|
|
|
$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();
|
2019-02-16 11:39:16 +03:00
|
|
|
$row = $result->fetch();
|
2019-02-11 14:57:38 +03:00
|
|
|
$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(
|
2019-02-16 11:39:16 +03:00
|
|
|
$query->expr()->eq('r.resource_id', 'a.resource_id'),
|
2019-02-22 13:37:04 +03:00
|
|
|
$query->expr()->eq('r.resource_type', 'a.resource_type'),
|
2019-02-11 14:57:38 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:03:44 +03:00
|
|
|
/**
|
|
|
|
* @return IProvider[]
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-17 21:03:44 +03:00
|
|
|
*/
|
|
|
|
public function getProviders(): array {
|
2019-02-22 17:11:09 +03:00
|
|
|
if (!empty($this->providers)) {
|
|
|
|
foreach ($this->providers as $provider) {
|
|
|
|
try {
|
|
|
|
$this->providerInstances[] = \OC::$server->query($provider);
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Error when instantiating resource provider'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->providers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->providerInstances;
|
2018-10-17 21:03:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-14 14:30:34 +03:00
|
|
|
* Get the rich object data of a resource
|
2018-10-17 21:03:44 +03:00
|
|
|
*
|
|
|
|
* @param IResource $resource
|
2019-03-14 14:30:34 +03:00
|
|
|
* @return array
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-17 21:03:44 +03:00
|
|
|
*/
|
2019-03-14 14:30:34 +03:00
|
|
|
public function getResourceRichObject(IResource $resource): array {
|
2019-01-23 19:48:24 +03:00
|
|
|
foreach ($this->getProviders() as $provider) {
|
2019-01-24 13:50:01 +03:00
|
|
|
if ($provider->getType() === $resource->getType()) {
|
|
|
|
try {
|
2019-03-14 14:30:34 +03:00
|
|
|
return $provider->getResourceRichObject($resource);
|
2019-01-24 13:50:01 +03:00
|
|
|
} catch (ResourceException $e) {
|
|
|
|
}
|
2019-01-23 19:48:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:30:34 +03:00
|
|
|
return [];
|
2019-01-23 19:48:24 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:03:44 +03:00
|
|
|
/**
|
|
|
|
* Can a user/guest access the collection
|
|
|
|
*
|
|
|
|
* @param IResource $resource
|
2019-02-11 14:57:38 +03:00
|
|
|
* @param IUser|null $user
|
2018-10-17 21:03:44 +03:00
|
|
|
* @return bool
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-17 21:03:44 +03:00
|
|
|
*/
|
2019-02-11 14:57:38 +03:00
|
|
|
public function canAccessResource(IResource $resource, ?IUser $user): bool {
|
2019-02-22 11:48:43 +03:00
|
|
|
$access = $this->checkAccessCacheForUserByResource($resource, $user);
|
|
|
|
if (\is_bool($access)) {
|
|
|
|
return $access;
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$access = false;
|
2018-10-17 21:03:44 +03:00
|
|
|
foreach ($this->getProviders() as $provider) {
|
2019-01-24 13:50:01 +03:00
|
|
|
if ($provider->getType() === $resource->getType()) {
|
|
|
|
try {
|
2019-02-16 11:39:16 +03:00
|
|
|
if ($provider->canAccessResource($resource, $user)) {
|
2019-02-11 14:57:38 +03:00
|
|
|
$access = true;
|
|
|
|
break;
|
2019-01-24 13:50:01 +03:00
|
|
|
}
|
|
|
|
} catch (ResourceException $e) {
|
2018-10-17 21:03:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$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 {
|
2019-02-22 11:48:43 +03:00
|
|
|
$access = $this->checkAccessCacheForUserByCollection($collection, $user);
|
|
|
|
if (\is_bool($access)) {
|
|
|
|
return $access;
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$access = false;
|
|
|
|
foreach ($collection->getResources() as $resource) {
|
|
|
|
if ($resource->canAccess($user)) {
|
|
|
|
$access = true;
|
2019-02-22 17:11:49 +03:00
|
|
|
break;
|
2019-02-11 14:57:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cacheAccessForCollection($collection, $user, $access);
|
|
|
|
return $access;
|
2018-10-17 21:03:44 +03:00
|
|
|
}
|
2019-01-24 13:43:18 +03:00
|
|
|
|
2019-02-22 11:48:43 +03:00
|
|
|
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)))
|
2019-02-22 13:37:04 +03:00
|
|
|
->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR)))
|
2019-02-22 11:48:43 +03:00
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:43:20 +03:00
|
|
|
public function cacheAccessForResource(IResource $resource, ?IUser $user, bool $access): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->insert(self::TABLE_ACCESS_CACHE)
|
2019-02-07 17:43:20 +03:00
|
|
|
->values([
|
|
|
|
'user_id' => $query->createNamedParameter($userId),
|
|
|
|
'resource_id' => $query->createNamedParameter($resource->getId()),
|
2019-02-22 13:37:04 +03:00
|
|
|
'resource_type' => $query->createNamedParameter($resource->getType()),
|
2019-03-22 18:00:44 +03:00
|
|
|
'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL),
|
2019-02-07 17:43:20 +03:00
|
|
|
]);
|
2019-02-22 11:49:01 +03:00
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
|
|
|
}
|
2019-02-07 17:43:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cacheAccessForCollection(ICollection $collection, ?IUser $user, bool $access): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->insert(self::TABLE_ACCESS_CACHE)
|
2019-02-07 17:43:20 +03:00
|
|
|
->values([
|
|
|
|
'user_id' => $query->createNamedParameter($userId),
|
|
|
|
'collection_id' => $query->createNamedParameter($collection->getId()),
|
2019-04-27 14:24:08 +03:00
|
|
|
'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL),
|
2019-02-07 17:43:20 +03:00
|
|
|
]);
|
2019-02-22 11:49:01 +03:00
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
|
|
|
}
|
2019-02-07 17:43:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invalidateAccessCacheForUser(?IUser $user): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
2019-02-07 17:43:20 +03:00
|
|
|
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invalidateAccessCacheForResource(IResource $resource): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
2019-02-22 13:37:04 +03:00
|
|
|
->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId())))
|
|
|
|
->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR)));
|
2019-02-07 17:43:20 +03:00
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
foreach ($resource->getCollections() as $collection) {
|
|
|
|
$this->invalidateAccessCacheForCollection($collection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
public function invalidateAccessCacheForCollection(ICollection $collection): void {
|
2019-02-07 17:43:20 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
2019-02-07 17:43:20 +03:00
|
|
|
->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId())));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2019-02-22 13:37:04 +03:00
|
|
|
public function invalidateAccessCacheForProvider(IProvider $provider): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
|
|
|
->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR)));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:43:20 +03:00
|
|
|
public function invalidateAccessCacheForResourceByUser(IResource $resource, ?IUser $user): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
2019-02-07 17:43:20 +03:00
|
|
|
->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId())))
|
|
|
|
->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
foreach ($resource->getCollections() as $collection) {
|
|
|
|
$this->invalidateAccessCacheForCollectionByUser($collection, $user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function invalidateAccessCacheForCollectionByUser(ICollection $collection, ?IUser $user): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
2019-02-07 17:43:20 +03:00
|
|
|
->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId())))
|
|
|
|
->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2019-02-22 13:37:04 +03:00
|
|
|
public function invalidateAccessCacheForProviderByUser(IProvider $provider, ?IUser $user): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
|
|
|
|
|
|
|
$query->delete(self::TABLE_ACCESS_CACHE)
|
|
|
|
->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR)))
|
|
|
|
->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2019-01-24 13:43:18 +03:00
|
|
|
/**
|
2019-02-22 17:11:09 +03:00
|
|
|
* @param string $provider
|
2019-01-24 13:43:18 +03:00
|
|
|
*/
|
2019-02-22 17:11:09 +03:00
|
|
|
public function registerResourceProvider(string $provider): void {
|
2019-01-24 13:43:18 +03:00
|
|
|
$this->providers[] = $provider;
|
|
|
|
}
|
2019-01-24 13:50:01 +03:00
|
|
|
|
|
|
|
/**
|
2019-02-22 11:58:15 +03:00
|
|
|
* Get the resource type of the provider
|
2019-01-24 13:50:01 +03:00
|
|
|
*
|
|
|
|
* @return string
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2019-01-24 13:50:01 +03:00
|
|
|
*/
|
|
|
|
public function getType(): string {
|
|
|
|
return '';
|
|
|
|
}
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|