Collection.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Collaboration\Resources;
  27. use Doctrine\DBAL\Exception\ConstraintViolationException;
  28. use OCP\Collaboration\Resources\ICollection;
  29. use OCP\Collaboration\Resources\IManager;
  30. use OCP\Collaboration\Resources\IResource;
  31. use OCP\Collaboration\Resources\ResourceException;
  32. use OCP\DB\QueryBuilder\IQueryBuilder;
  33. use OCP\IDBConnection;
  34. use OCP\IUser;
  35. class Collection implements ICollection {
  36. /** @var IResource[] */
  37. protected array $resources = [];
  38. public function __construct(
  39. /** @var Manager $manager */
  40. protected IManager $manager,
  41. protected IDBConnection $connection,
  42. protected int $id,
  43. protected string $name,
  44. protected ?IUser $userForAccess = null,
  45. protected ?bool $access = null
  46. ) {
  47. }
  48. /**
  49. * @since 16.0.0
  50. */
  51. public function getId(): int {
  52. return $this->id;
  53. }
  54. /**
  55. * @since 16.0.0
  56. */
  57. public function getName(): string {
  58. return $this->name;
  59. }
  60. /**
  61. * @since 16.0.0
  62. */
  63. public function setName(string $name): void {
  64. $query = $this->connection->getQueryBuilder();
  65. $query->update(Manager::TABLE_COLLECTIONS)
  66. ->set('name', $query->createNamedParameter($name))
  67. ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
  68. $query->execute();
  69. $this->name = $name;
  70. }
  71. /**
  72. * @return IResource[]
  73. * @since 16.0.0
  74. */
  75. public function getResources(): array {
  76. if (empty($this->resources)) {
  77. $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess);
  78. }
  79. return $this->resources;
  80. }
  81. /**
  82. * Adds a resource to a collection
  83. *
  84. * @throws ResourceException when the resource is already part of the collection
  85. * @since 16.0.0
  86. */
  87. public function addResource(IResource $resource): void {
  88. array_map(function (IResource $r) use ($resource) {
  89. if ($this->isSameResource($r, $resource)) {
  90. throw new ResourceException('Already part of the collection');
  91. }
  92. }, $this->getResources());
  93. $this->resources[] = $resource;
  94. $query = $this->connection->getQueryBuilder();
  95. $query->insert(Manager::TABLE_RESOURCES)
  96. ->values([
  97. 'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT),
  98. 'resource_type' => $query->createNamedParameter($resource->getType()),
  99. 'resource_id' => $query->createNamedParameter($resource->getId()),
  100. ]);
  101. try {
  102. $query->execute();
  103. } catch (ConstraintViolationException $e) {
  104. throw new ResourceException('Already part of the collection');
  105. }
  106. $this->manager->invalidateAccessCacheForCollection($this);
  107. }
  108. /**
  109. * Removes a resource from a collection
  110. *
  111. * @since 16.0.0
  112. */
  113. public function removeResource(IResource $resource): void {
  114. $this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) {
  115. return !$this->isSameResource($r, $resource);
  116. });
  117. $query = $this->connection->getQueryBuilder();
  118. $query->delete(Manager::TABLE_RESOURCES)
  119. ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)))
  120. ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType())))
  121. ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId())));
  122. $query->execute();
  123. if (empty($this->resources)) {
  124. $this->removeCollection();
  125. } else {
  126. $this->manager->invalidateAccessCacheForCollection($this);
  127. }
  128. }
  129. /**
  130. * Can a user/guest access the collection
  131. *
  132. * @since 16.0.0
  133. */
  134. public function canAccess(?IUser $user): bool {
  135. if ($user instanceof IUser) {
  136. return $this->canUserAccess($user);
  137. }
  138. return $this->canGuestAccess();
  139. }
  140. protected function canUserAccess(IUser $user): bool {
  141. if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  142. return $this->access;
  143. }
  144. $access = $this->manager->canAccessCollection($this, $user);
  145. if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  146. $this->access = $access;
  147. }
  148. return $access;
  149. }
  150. protected function canGuestAccess(): bool {
  151. if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
  152. return $this->access;
  153. }
  154. $access = $this->manager->canAccessCollection($this, null);
  155. if (!$this->userForAccess instanceof IUser) {
  156. $this->access = $access;
  157. }
  158. return $access;
  159. }
  160. protected function isSameResource(IResource $resource1, IResource $resource2): bool {
  161. return $resource1->getType() === $resource2->getType() &&
  162. $resource1->getId() === $resource2->getId();
  163. }
  164. protected function removeCollection(): void {
  165. $query = $this->connection->getQueryBuilder();
  166. $query->delete(Manager::TABLE_COLLECTIONS)
  167. ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)));
  168. $query->execute();
  169. $this->manager->invalidateAccessCacheForCollection($this);
  170. $this->id = 0;
  171. }
  172. }