1
0

Resource.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. protected ?array $data = null;
  33. public function __construct(
  34. protected IManager $manager,
  35. protected IDBConnection $connection,
  36. protected string $type,
  37. protected string $id,
  38. protected ?IUser $userForAccess = null,
  39. protected ?bool $access = null
  40. ) {
  41. }
  42. /**
  43. * @since 16.0.0
  44. */
  45. public function getType(): string {
  46. return $this->type;
  47. }
  48. /**
  49. * @since 16.0.0
  50. */
  51. public function getId(): string {
  52. return $this->id;
  53. }
  54. /**
  55. * @since 16.0.0
  56. */
  57. public function getRichObject(): array {
  58. if ($this->data === null) {
  59. $this->data = $this->manager->getResourceRichObject($this);
  60. }
  61. return $this->data;
  62. }
  63. /**
  64. * Can a user/guest access the resource
  65. *
  66. * @since 16.0.0
  67. */
  68. public function canAccess(?IUser $user): bool {
  69. if ($user instanceof IUser) {
  70. return $this->canUserAccess($user);
  71. }
  72. return $this->canGuestAccess();
  73. }
  74. protected function canUserAccess(IUser $user): bool {
  75. if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  76. return $this->access;
  77. }
  78. $access = $this->manager->canAccessResource($this, $user);
  79. if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  80. $this->access = $access;
  81. }
  82. return $access;
  83. }
  84. protected function canGuestAccess(): bool {
  85. if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
  86. return $this->access;
  87. }
  88. $access = $this->manager->canAccessResource($this, null);
  89. if (!$this->userForAccess instanceof IUser) {
  90. $this->access = $access;
  91. }
  92. return $access;
  93. }
  94. /**
  95. * @return ICollection[]
  96. * @since 16.0.0
  97. */
  98. public function getCollections(): array {
  99. $collections = [];
  100. $query = $this->connection->getQueryBuilder();
  101. $query->select('collection_id')
  102. ->from('collres_resources')
  103. ->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType())))
  104. ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId())));
  105. $result = $query->execute();
  106. while ($row = $result->fetch()) {
  107. $collections[] = $this->manager->getCollection((int) $row['collection_id']);
  108. }
  109. $result->closeCursor();
  110. return $collections;
  111. }
  112. }