Resource.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Collaboration\Resources;
  26. use OCP\Collaboration\Resources\ICollection;
  27. use OCP\Collaboration\Resources\IManager;
  28. use OCP\Collaboration\Resources\IResource;
  29. use OCP\IDBConnection;
  30. use OCP\IUser;
  31. class Resource implements IResource {
  32. /** @var IManager */
  33. protected $manager;
  34. /** @var IDBConnection */
  35. protected $connection;
  36. /** @var string */
  37. protected $type;
  38. /** @var string */
  39. protected $id;
  40. /** @var IUser|null */
  41. protected $userForAccess;
  42. /** @var bool|null */
  43. protected $access;
  44. /** @var array|null */
  45. protected $data;
  46. public function __construct(
  47. IManager $manager,
  48. IDBConnection $connection,
  49. string $type,
  50. string $id,
  51. ?IUser $userForAccess = null,
  52. ?bool $access = null
  53. ) {
  54. $this->manager = $manager;
  55. $this->connection = $connection;
  56. $this->type = $type;
  57. $this->id = $id;
  58. $this->userForAccess = $userForAccess;
  59. $this->access = $access;
  60. }
  61. /**
  62. * @return string
  63. * @since 16.0.0
  64. */
  65. public function getType(): string {
  66. return $this->type;
  67. }
  68. /**
  69. * @return string
  70. * @since 16.0.0
  71. */
  72. public function getId(): string {
  73. return $this->id;
  74. }
  75. /**
  76. * @return array
  77. * @since 16.0.0
  78. */
  79. public function getRichObject(): array {
  80. if ($this->data === null) {
  81. $this->data = $this->manager->getResourceRichObject($this);
  82. }
  83. return $this->data;
  84. }
  85. /**
  86. * Can a user/guest access the resource
  87. *
  88. * @param IUser|null $user
  89. * @return bool
  90. * @since 16.0.0
  91. */
  92. public function canAccess(?IUser $user): bool {
  93. if ($user instanceof IUser) {
  94. return $this->canUserAccess($user);
  95. }
  96. return $this->canGuestAccess();
  97. }
  98. protected function canUserAccess(IUser $user): bool {
  99. if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  100. return $this->access;
  101. }
  102. $access = $this->manager->canAccessResource($this, $user);
  103. if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  104. $this->access = $access;
  105. }
  106. return $access;
  107. }
  108. protected function canGuestAccess(): bool {
  109. if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
  110. return $this->access;
  111. }
  112. $access = $this->manager->canAccessResource($this, null);
  113. if (!$this->userForAccess instanceof IUser) {
  114. $this->access = $access;
  115. }
  116. return $access;
  117. }
  118. /**
  119. * @return ICollection[]
  120. * @since 16.0.0
  121. */
  122. public function getCollections(): array {
  123. $collections = [];
  124. $query = $this->connection->getQueryBuilder();
  125. $query->select('collection_id')
  126. ->from('collres_resources')
  127. ->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType())))
  128. ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId())));
  129. $result = $query->execute();
  130. while ($row = $result->fetch()) {
  131. $collections[] = $this->manager->getCollection((int) $row['collection_id']);
  132. }
  133. $result->closeCursor();
  134. return $collections;
  135. }
  136. }