1
0

DocumentAccess.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\FullTextSearch\Model;
  25. use JsonSerializable;
  26. use OCP\FullTextSearch\Model\IDocumentAccess;
  27. /**
  28. * Class IDocumentAccess
  29. *
  30. * This object is used as a data transfer object when
  31. *
  32. * - indexing a document,
  33. * - generating a search request.
  34. *
  35. * During the index, it is used to define which users, groups, circles, ...
  36. * have access to the IndexDocument
  37. *
  38. * During the search, it is internally use to define to which group, circles, ...
  39. * a user that perform the search belongs to.
  40. *
  41. * @see IIndexDocument::setAccess
  42. *
  43. * @since 16.0.0
  44. *
  45. * @package OC\FullTextSearch\Model
  46. */
  47. final class DocumentAccess implements IDocumentAccess, JsonSerializable {
  48. private string $ownerId;
  49. private string $viewerId = '';
  50. private array $users = [];
  51. private array $groups = [];
  52. private array $circles = [];
  53. private array $links = [];
  54. /**
  55. * Owner of the document can be set at the init of the object.
  56. *
  57. * @since 16.0.0
  58. *
  59. * IDocumentAccess constructor.
  60. */
  61. public function __construct(string $ownerId = '') {
  62. $this->setOwnerId($ownerId);
  63. }
  64. /**
  65. * Set the Owner of the document.
  66. *
  67. * @since 16.0.0
  68. */
  69. public function setOwnerId(string $ownerId): IDocumentAccess {
  70. $this->ownerId = $ownerId;
  71. return $this;
  72. }
  73. /**
  74. * Get the Owner of the document.
  75. *
  76. * @since 16.0.0
  77. */
  78. public function getOwnerId(): string {
  79. return $this->ownerId;
  80. }
  81. /**
  82. * Set the viewer of the document.
  83. *
  84. * @since 16.0.0
  85. */
  86. public function setViewerId(string $viewerId): IDocumentAccess {
  87. $this->viewerId = $viewerId;
  88. return $this;
  89. }
  90. /**
  91. * Get the viewer of the document.
  92. *
  93. * @since 16.0.0
  94. */
  95. public function getViewerId(): string {
  96. return $this->viewerId;
  97. }
  98. /**
  99. * Set the list of users that have read access to the document.
  100. *
  101. * @since 16.0.0
  102. */
  103. public function setUsers(array $users): IDocumentAccess {
  104. $this->users = $users;
  105. return $this;
  106. }
  107. /**
  108. * Add an entry to the list of users that have read access to the document.
  109. *
  110. * @since 16.0.0
  111. */
  112. public function addUser(string $user): IDocumentAccess {
  113. $this->users[] = $user;
  114. return $this;
  115. }
  116. /**
  117. * Add multiple entries to the list of users that have read access to the
  118. * document.
  119. *
  120. * @since 16.0.0
  121. */
  122. public function addUsers($users): IDocumentAccess {
  123. $this->users = array_merge($this->users, $users);
  124. return $this;
  125. }
  126. /**
  127. * Get the complete list of users that have read access to the document.
  128. *
  129. * @since 16.0.0
  130. */
  131. public function getUsers(): array {
  132. return $this->users;
  133. }
  134. /**
  135. * Set the list of groups that have read access to the document.
  136. *
  137. * @since 16.0.0
  138. */
  139. public function setGroups(array $groups): IDocumentAccess {
  140. $this->groups = $groups;
  141. return $this;
  142. }
  143. /**
  144. * Add an entry to the list of groups that have read access to the document.
  145. *
  146. * @since 16.0.0
  147. */
  148. public function addGroup(string $group): IDocumentAccess {
  149. $this->groups[] = $group;
  150. return $this;
  151. }
  152. /**
  153. * Add multiple entries to the list of groups that have read access to the
  154. * document.
  155. *
  156. * @since 16.0.0
  157. */
  158. public function addGroups(array $groups): IDocumentAccess {
  159. $this->groups = array_merge($this->groups, $groups);
  160. return $this;
  161. }
  162. /**
  163. * Get the complete list of groups that have read access to the document.
  164. *
  165. * @since 16.0.0
  166. */
  167. public function getGroups(): array {
  168. return $this->groups;
  169. }
  170. /**
  171. * Set the list of circles that have read access to the document.
  172. *
  173. * @since 16.0.0
  174. */
  175. public function setCircles(array $circles): IDocumentAccess {
  176. $this->circles = $circles;
  177. return $this;
  178. }
  179. /**
  180. * Add an entry to the list of circles that have read access to the document.
  181. *
  182. * @since 16.0.0
  183. */
  184. public function addCircle(string $circle): IDocumentAccess {
  185. $this->circles[] = $circle;
  186. return $this;
  187. }
  188. /**
  189. * Add multiple entries to the list of groups that have read access to the
  190. * document.
  191. *
  192. * @since 16.0.0
  193. */
  194. public function addCircles(array $circles): IDocumentAccess {
  195. $this->circles = array_merge($this->circles, $circles);
  196. return $this;
  197. }
  198. /**
  199. * Get the complete list of circles that have read access to the document.
  200. *
  201. * @since 16.0.0
  202. */
  203. public function getCircles(): array {
  204. return $this->circles;
  205. }
  206. /**
  207. * Set the list of links that have read access to the document.
  208. *
  209. * @since 16.0.0
  210. */
  211. public function setLinks(array $links): IDocumentAccess {
  212. $this->links = $links;
  213. return $this;
  214. }
  215. /**
  216. * Get the list of links that have read access to the document.
  217. *
  218. * @since 16.0.0
  219. */
  220. public function getLinks(): array {
  221. return $this->links;
  222. }
  223. /**
  224. * @since 16.0.0
  225. */
  226. public function jsonSerialize(): array {
  227. return [
  228. 'ownerId' => $this->getOwnerId(),
  229. 'viewerId' => $this->getViewerId(),
  230. 'users' => $this->getUsers(),
  231. 'groups' => $this->getGroups(),
  232. 'circles' => $this->getCircles(),
  233. 'links' => $this->getLinks()
  234. ];
  235. }
  236. }