1
0

DocumentAccess.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\FullTextSearch\Model;
  8. use JsonSerializable;
  9. use OCP\FullTextSearch\Model\IDocumentAccess;
  10. /**
  11. * Class IDocumentAccess
  12. *
  13. * This object is used as a data transfer object when
  14. *
  15. * - indexing a document,
  16. * - generating a search request.
  17. *
  18. * During the index, it is used to define which users, groups, circles, ...
  19. * have access to the IndexDocument
  20. *
  21. * During the search, it is internally use to define to which group, circles, ...
  22. * a user that perform the search belongs to.
  23. *
  24. * @see IIndexDocument::setAccess
  25. *
  26. * @since 16.0.0
  27. *
  28. * @package OC\FullTextSearch\Model
  29. */
  30. final class DocumentAccess implements IDocumentAccess, JsonSerializable {
  31. private string $ownerId;
  32. private string $viewerId = '';
  33. private array $users = [];
  34. private array $groups = [];
  35. private array $circles = [];
  36. private array $links = [];
  37. /**
  38. * Owner of the document can be set at the init of the object.
  39. *
  40. * @since 16.0.0
  41. *
  42. * IDocumentAccess constructor.
  43. */
  44. public function __construct(string $ownerId = '') {
  45. $this->setOwnerId($ownerId);
  46. }
  47. /**
  48. * Set the Owner of the document.
  49. *
  50. * @since 16.0.0
  51. */
  52. public function setOwnerId(string $ownerId): IDocumentAccess {
  53. $this->ownerId = $ownerId;
  54. return $this;
  55. }
  56. /**
  57. * Get the Owner of the document.
  58. *
  59. * @since 16.0.0
  60. */
  61. public function getOwnerId(): string {
  62. return $this->ownerId;
  63. }
  64. /**
  65. * Set the viewer of the document.
  66. *
  67. * @since 16.0.0
  68. */
  69. public function setViewerId(string $viewerId): IDocumentAccess {
  70. $this->viewerId = $viewerId;
  71. return $this;
  72. }
  73. /**
  74. * Get the viewer of the document.
  75. *
  76. * @since 16.0.0
  77. */
  78. public function getViewerId(): string {
  79. return $this->viewerId;
  80. }
  81. /**
  82. * Set the list of users that have read access to the document.
  83. *
  84. * @since 16.0.0
  85. */
  86. public function setUsers(array $users): IDocumentAccess {
  87. $this->users = $users;
  88. return $this;
  89. }
  90. /**
  91. * Add an entry to the list of users that have read access to the document.
  92. *
  93. * @since 16.0.0
  94. */
  95. public function addUser(string $user): IDocumentAccess {
  96. $this->users[] = $user;
  97. return $this;
  98. }
  99. /**
  100. * Add multiple entries to the list of users that have read access to the
  101. * document.
  102. *
  103. * @since 16.0.0
  104. */
  105. public function addUsers($users): IDocumentAccess {
  106. $this->users = array_merge($this->users, $users);
  107. return $this;
  108. }
  109. /**
  110. * Get the complete list of users that have read access to the document.
  111. *
  112. * @since 16.0.0
  113. */
  114. public function getUsers(): array {
  115. return $this->users;
  116. }
  117. /**
  118. * Set the list of groups that have read access to the document.
  119. *
  120. * @since 16.0.0
  121. */
  122. public function setGroups(array $groups): IDocumentAccess {
  123. $this->groups = $groups;
  124. return $this;
  125. }
  126. /**
  127. * Add an entry to the list of groups that have read access to the document.
  128. *
  129. * @since 16.0.0
  130. */
  131. public function addGroup(string $group): IDocumentAccess {
  132. $this->groups[] = $group;
  133. return $this;
  134. }
  135. /**
  136. * Add multiple entries to the list of groups that have read access to the
  137. * document.
  138. *
  139. * @since 16.0.0
  140. */
  141. public function addGroups(array $groups): IDocumentAccess {
  142. $this->groups = array_merge($this->groups, $groups);
  143. return $this;
  144. }
  145. /**
  146. * Get the complete list of groups that have read access to the document.
  147. *
  148. * @since 16.0.0
  149. */
  150. public function getGroups(): array {
  151. return $this->groups;
  152. }
  153. /**
  154. * Set the list of circles that have read access to the document.
  155. *
  156. * @since 16.0.0
  157. */
  158. public function setCircles(array $circles): IDocumentAccess {
  159. $this->circles = $circles;
  160. return $this;
  161. }
  162. /**
  163. * Add an entry to the list of circles that have read access to the document.
  164. *
  165. * @since 16.0.0
  166. */
  167. public function addCircle(string $circle): IDocumentAccess {
  168. $this->circles[] = $circle;
  169. return $this;
  170. }
  171. /**
  172. * Add multiple entries to the list of groups that have read access to the
  173. * document.
  174. *
  175. * @since 16.0.0
  176. */
  177. public function addCircles(array $circles): IDocumentAccess {
  178. $this->circles = array_merge($this->circles, $circles);
  179. return $this;
  180. }
  181. /**
  182. * Get the complete list of circles that have read access to the document.
  183. *
  184. * @since 16.0.0
  185. */
  186. public function getCircles(): array {
  187. return $this->circles;
  188. }
  189. /**
  190. * Set the list of links that have read access to the document.
  191. *
  192. * @since 16.0.0
  193. */
  194. public function setLinks(array $links): IDocumentAccess {
  195. $this->links = $links;
  196. return $this;
  197. }
  198. /**
  199. * Get the list of links that have read access to the document.
  200. *
  201. * @since 16.0.0
  202. */
  203. public function getLinks(): array {
  204. return $this->links;
  205. }
  206. /**
  207. * @since 16.0.0
  208. */
  209. public function jsonSerialize(): array {
  210. return [
  211. 'ownerId' => $this->getOwnerId(),
  212. 'viewerId' => $this->getViewerId(),
  213. 'users' => $this->getUsers(),
  214. 'groups' => $this->getGroups(),
  215. 'circles' => $this->getCircles(),
  216. 'links' => $this->getLinks()
  217. ];
  218. }
  219. }