1
0

Resource.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Collaboration\Resources;
  8. use OCP\Collaboration\Resources\ICollection;
  9. use OCP\Collaboration\Resources\IManager;
  10. use OCP\Collaboration\Resources\IResource;
  11. use OCP\IDBConnection;
  12. use OCP\IUser;
  13. class Resource implements IResource {
  14. protected ?array $data = null;
  15. public function __construct(
  16. protected IManager $manager,
  17. protected IDBConnection $connection,
  18. protected string $type,
  19. protected string $id,
  20. protected ?IUser $userForAccess = null,
  21. protected ?bool $access = null,
  22. ) {
  23. }
  24. /**
  25. * @since 16.0.0
  26. */
  27. public function getType(): string {
  28. return $this->type;
  29. }
  30. /**
  31. * @since 16.0.0
  32. */
  33. public function getId(): string {
  34. return $this->id;
  35. }
  36. /**
  37. * @since 16.0.0
  38. */
  39. public function getRichObject(): array {
  40. if ($this->data === null) {
  41. $this->data = $this->manager->getResourceRichObject($this);
  42. }
  43. return $this->data;
  44. }
  45. /**
  46. * Can a user/guest access the resource
  47. *
  48. * @since 16.0.0
  49. */
  50. public function canAccess(?IUser $user): bool {
  51. if ($user instanceof IUser) {
  52. return $this->canUserAccess($user);
  53. }
  54. return $this->canGuestAccess();
  55. }
  56. protected function canUserAccess(IUser $user): bool {
  57. if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  58. return $this->access;
  59. }
  60. $access = $this->manager->canAccessResource($this, $user);
  61. if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
  62. $this->access = $access;
  63. }
  64. return $access;
  65. }
  66. protected function canGuestAccess(): bool {
  67. if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
  68. return $this->access;
  69. }
  70. $access = $this->manager->canAccessResource($this, null);
  71. if (!$this->userForAccess instanceof IUser) {
  72. $this->access = $access;
  73. }
  74. return $access;
  75. }
  76. /**
  77. * @return ICollection[]
  78. * @since 16.0.0
  79. */
  80. public function getCollections(): array {
  81. $collections = [];
  82. $query = $this->connection->getQueryBuilder();
  83. $query->select('collection_id')
  84. ->from('collres_resources')
  85. ->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType())))
  86. ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId())));
  87. $result = $query->execute();
  88. while ($row = $result->fetch()) {
  89. $collections[] = $this->manager->getCollection((int)$row['collection_id']);
  90. }
  91. $result->closeCursor();
  92. return $collections;
  93. }
  94. }