DocumentAccess.php 6.7 KB

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