2018-09-14 13:34:24 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2018-09-14 13:34:24 +03:00
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-09-14 13:34:24 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Collaboration\Resources;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Exception\ConstraintViolationException;
|
|
|
|
use OCP\Collaboration\Resources\ICollection;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Collaboration\Resources\IManager;
|
2018-09-14 13:34:24 +03:00
|
|
|
use OCP\Collaboration\Resources\IResource;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Collaboration\Resources\ResourceException;
|
2018-09-14 13:34:24 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
2018-10-17 21:03:44 +03:00
|
|
|
use OCP\IUser;
|
2018-09-14 13:34:24 +03:00
|
|
|
|
|
|
|
class Collection implements ICollection {
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
/** @var IManager|Manager */
|
2018-09-14 13:34:24 +03:00
|
|
|
protected $manager;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $id;
|
|
|
|
|
2018-10-24 15:25:35 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $name;
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
/** @var IUser|null */
|
|
|
|
protected $userForAccess;
|
|
|
|
|
2019-02-07 17:43:20 +03:00
|
|
|
/** @var bool|null */
|
|
|
|
protected $access;
|
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
/** @var IResource[] */
|
|
|
|
protected $resources;
|
|
|
|
|
2018-10-24 15:25:35 +03:00
|
|
|
public function __construct(
|
|
|
|
IManager $manager,
|
|
|
|
IDBConnection $connection,
|
|
|
|
int $id,
|
2019-02-07 17:43:20 +03:00
|
|
|
string $name,
|
2019-02-11 14:57:38 +03:00
|
|
|
?IUser $userForAccess = null,
|
|
|
|
?bool $access = null
|
2018-10-24 15:25:35 +03:00
|
|
|
) {
|
2018-09-14 13:34:24 +03:00
|
|
|
$this->manager = $manager;
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->id = $id;
|
2018-10-24 15:25:35 +03:00
|
|
|
$this->name = $name;
|
2019-02-11 14:57:38 +03:00
|
|
|
$this->userForAccess = $userForAccess;
|
2019-02-07 17:43:20 +03:00
|
|
|
$this->access = $access;
|
2018-09-14 13:34:24 +03:00
|
|
|
$this->resources = [];
|
|
|
|
}
|
|
|
|
|
2018-10-24 15:25:35 +03:00
|
|
|
/**
|
|
|
|
* @return int
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-24 15:25:35 +03:00
|
|
|
*/
|
|
|
|
public function getId(): int {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-24 15:25:35 +03:00
|
|
|
*/
|
|
|
|
public function getName(): string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
/**
|
|
|
|
* @return IResource[]
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-09-14 13:34:24 +03:00
|
|
|
*/
|
|
|
|
public function getResources(): array {
|
|
|
|
if (empty($this->resources)) {
|
2019-02-11 14:57:38 +03:00
|
|
|
$this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess);
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->resources;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a resource to a collection
|
|
|
|
*
|
|
|
|
* @param IResource $resource
|
|
|
|
* @throws ResourceException when the resource is already part of the collection
|
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 addResource(IResource $resource): void {
|
2020-04-09 14:53:40 +03:00
|
|
|
array_map(function (IResource $r) use ($resource) {
|
2018-09-14 13:34:24 +03:00
|
|
|
if ($this->isSameResource($r, $resource)) {
|
|
|
|
throw new ResourceException('Already part of the collection');
|
|
|
|
}
|
2019-02-11 14:57:38 +03:00
|
|
|
}, $this->getResources());
|
2018-09-14 13:34:24 +03:00
|
|
|
|
|
|
|
$this->resources[] = $resource;
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->insert(Manager::TABLE_RESOURCES)
|
2018-09-14 13:34:24 +03:00
|
|
|
->values([
|
|
|
|
'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT),
|
|
|
|
'resource_type' => $query->createNamedParameter($resource->getType()),
|
|
|
|
'resource_id' => $query->createNamedParameter($resource->getId()),
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (ConstraintViolationException $e) {
|
|
|
|
throw new ResourceException('Already part of the collection');
|
|
|
|
}
|
2019-02-11 14:57:38 +03:00
|
|
|
|
|
|
|
$this->manager->invalidateAccessCacheForCollection($this);
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a resource from a collection
|
|
|
|
*
|
|
|
|
* @param IResource $resource
|
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 removeResource(IResource $resource): void {
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) {
|
2018-09-14 13:34:24 +03:00
|
|
|
return !$this->isSameResource($r, $resource);
|
|
|
|
});
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(Manager::TABLE_RESOURCES)
|
2018-09-14 13:34:24 +03:00
|
|
|
->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())));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
if (empty($this->resources)) {
|
2018-10-24 15:25:35 +03:00
|
|
|
$this->removeCollection();
|
2019-02-11 14:57:38 +03:00
|
|
|
} else {
|
|
|
|
$this->manager->invalidateAccessCacheForCollection($this);
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:03:44 +03:00
|
|
|
/**
|
|
|
|
* Can a user/guest access the collection
|
|
|
|
*
|
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 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;
|
2018-10-17 21:03:44 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$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;
|
2018-10-17 21:03:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:34:24 +03:00
|
|
|
protected function isSameResource(IResource $resource1, IResource $resource2): bool {
|
|
|
|
return $resource1->getType() === $resource2->getType() &&
|
|
|
|
$resource1->getId() === $resource2->getId();
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
protected function removeCollection(): void {
|
2018-09-14 13:34:24 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2019-02-11 14:57:38 +03:00
|
|
|
$query->delete(Manager::TABLE_COLLECTIONS)
|
2018-09-14 13:34:24 +03:00
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
|
|
|
|
2019-02-11 14:57:38 +03:00
|
|
|
$this->manager->invalidateAccessCacheForCollection($this);
|
2018-09-14 13:34:24 +03:00
|
|
|
$this->id = 0;
|
|
|
|
}
|
|
|
|
}
|