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;
|
|
|
|
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Exception\ConstraintViolationException;
|
|
|
|
use OCP\Collaboration\Resources\IManager;
|
|
|
|
use OCP\Collaboration\Resources\ResourceException;
|
|
|
|
use OCP\Collaboration\Resources\ICollection;
|
|
|
|
use OCP\Collaboration\Resources\IResource;
|
|
|
|
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 {
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
protected $manager;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $id;
|
|
|
|
|
2018-10-24 15:25:35 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $name;
|
|
|
|
|
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,
|
|
|
|
string $name
|
|
|
|
) {
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function addResource(IResource $resource) {
|
|
|
|
array_map(function(IResource $r) use ($resource) {
|
|
|
|
if ($this->isSameResource($r, $resource)) {
|
|
|
|
throw new ResourceException('Already part of the collection');
|
|
|
|
}
|
|
|
|
}, $this->resources);
|
|
|
|
|
|
|
|
$this->resources[] = $resource;
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert('collres_resources')
|
|
|
|
->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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
public function removeResource(IResource $resource) {
|
|
|
|
$this->resources = array_filter($this->resources, function(IResource $r) use ($resource) {
|
|
|
|
return !$this->isSameResource($r, $resource);
|
|
|
|
});
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('collres_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())));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
if (empty($this->resources)) {
|
2018-10-24 15:25:35 +03:00
|
|
|
$this->removeCollection();
|
2018-09-14 13:34:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:03:44 +03:00
|
|
|
/**
|
|
|
|
* Can a user/guest access the collection
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @return bool
|
2019-02-07 18:04:15 +03:00
|
|
|
* @since 16.0.0
|
2018-10-17 21:03:44 +03:00
|
|
|
*/
|
|
|
|
public function canAccess(IUser $user = null): bool {
|
|
|
|
foreach ($this->getResources() as $resource) {
|
|
|
|
if ($resource->canAccess($user)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-10-24 15:25:35 +03:00
|
|
|
protected function removeCollection() {
|
2018-09-14 13:34:24 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('collres_collections')
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$this->id = 0;
|
|
|
|
}
|
|
|
|
}
|