123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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\ICollection;
- use OCP\Collaboration\Resources\IManager;
- use OCP\Collaboration\Resources\IResource;
- use OCP\Collaboration\Resources\ResourceException;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\IDBConnection;
- use OCP\IUser;
- class Collection implements ICollection {
- /** @var IResource[] */
- protected array $resources = [];
- public function __construct(
- /** @var Manager $manager */
- protected IManager $manager,
- protected IDBConnection $connection,
- protected int $id,
- protected string $name,
- protected ?IUser $userForAccess = null,
- protected ?bool $access = null
- ) {
- }
- /**
- * @since 16.0.0
- */
- public function getId(): int {
- return $this->id;
- }
- /**
- * @since 16.0.0
- */
- public function getName(): string {
- return $this->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)) {
- $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess);
- }
- return $this->resources;
- }
- /**
- * Adds a resource to a collection
- *
- * @throws ResourceException when the resource is already part of the collection
- * @since 16.0.0
- */
- 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->getResources());
- $this->resources[] = $resource;
- $query = $this->connection->getQueryBuilder();
- $query->insert(Manager::TABLE_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');
- }
- $this->manager->invalidateAccessCacheForCollection($this);
- }
- /**
- * Removes a resource from a collection
- *
- * @since 16.0.0
- */
- 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(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())));
- $query->execute();
- if (empty($this->resources)) {
- $this->removeCollection();
- } else {
- $this->manager->invalidateAccessCacheForCollection($this);
- }
- }
- /**
- * Can a user/guest access the collection
- *
- * @since 16.0.0
- */
- 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->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 {
- return $resource1->getType() === $resource2->getType() &&
- $resource1->getId() === $resource2->getId();
- }
- protected function removeCollection(): void {
- $query = $this->connection->getQueryBuilder();
- $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;
- }
- }
|