DocumentAccess.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 OC\FullTextSearch\Model;
  28. use JsonSerializable;
  29. use OCP\FullTextSearch\Model\IDocumentAccess;
  30. /**
  31. * Class IDocumentAccess
  32. *
  33. * This object is used as a data transfer object when
  34. *
  35. * - indexing a document,
  36. * - generating a search request.
  37. *
  38. * During the index, it is used to define which users, groups, circles, ...
  39. * have access to the IndexDocument
  40. *
  41. * During the search, it is internally use to define to which group, circles, ...
  42. * a user that perform the search belongs to.
  43. *
  44. * @see IIndexDocument::setAccess
  45. *
  46. * @since 16.0.0
  47. *
  48. * @package OC\FullTextSearch\Model
  49. */
  50. final class DocumentAccess implements IDocumentAccess, JsonSerializable {
  51. /** @var string */
  52. private $ownerId;
  53. /** @var string */
  54. private $viewerId = '';
  55. /** @var array */
  56. private $users = [];
  57. /** @var array */
  58. private $groups = [];
  59. /** @var array */
  60. private $circles = [];
  61. /** @var array */
  62. private $links = [];
  63. /**
  64. * Owner of the document can be set at the init of the object.
  65. *
  66. * @since 16.0.0
  67. *
  68. * IDocumentAccess constructor.
  69. *
  70. * @param string $ownerId
  71. */
  72. public function __construct(string $ownerId = '') {
  73. $this->setOwnerId($ownerId);
  74. }
  75. /**
  76. * Set the Owner of the document.
  77. *
  78. * @since 16.0.0
  79. *
  80. * @param string $ownerId
  81. *
  82. * @return IDocumentAccess
  83. */
  84. public function setOwnerId(string $ownerId): IDocumentAccess {
  85. $this->ownerId = $ownerId;
  86. return $this;
  87. }
  88. /**
  89. * Get the Owner of the document.
  90. *
  91. * @since 16.0.0
  92. *
  93. * @return string
  94. */
  95. public function getOwnerId(): string {
  96. return $this->ownerId;
  97. }
  98. /**
  99. * Set the viewer of the document.
  100. *
  101. * @since 16.0.0
  102. *
  103. * @param string $viewerId
  104. *
  105. * @return IDocumentAccess
  106. */
  107. public function setViewerId(string $viewerId): IDocumentAccess {
  108. $this->viewerId = $viewerId;
  109. return $this;
  110. }
  111. /**
  112. * Get the viewer of the document.
  113. *
  114. * @since 16.0.0
  115. *
  116. * @return string
  117. */
  118. public function getViewerId(): string {
  119. return $this->viewerId;
  120. }
  121. /**
  122. * Set the list of users that have read access to the document.
  123. *
  124. * @since 16.0.0
  125. *
  126. * @param array $users
  127. *
  128. * @return IDocumentAccess
  129. */
  130. public function setUsers(array $users): IDocumentAccess {
  131. $this->users = $users;
  132. return $this;
  133. }
  134. /**
  135. * Add an entry to the list of users that have read access to the document.
  136. *
  137. * @since 16.0.0
  138. *
  139. * @param string $user
  140. *
  141. * @return IDocumentAccess
  142. */
  143. public function addUser(string $user): IDocumentAccess {
  144. $this->users[] = $user;
  145. return $this;
  146. }
  147. /**
  148. * Add multiple entries to the list of users that have read access to the
  149. * document.
  150. *
  151. * @since 16.0.0
  152. *
  153. * @param array $users
  154. *
  155. * @return IDocumentAccess
  156. */
  157. public function addUsers($users): IDocumentAccess {
  158. $this->users = array_merge($this->users, $users);
  159. return $this;
  160. }
  161. /**
  162. * Get the complete list of users that have read access to the document.
  163. *
  164. * @since 16.0.0
  165. *
  166. * @return array
  167. */
  168. public function getUsers(): array {
  169. return $this->users;
  170. }
  171. /**
  172. * Set the list of groups that have read access to the document.
  173. *
  174. * @since 16.0.0
  175. *
  176. * @param array $groups
  177. *
  178. * @return IDocumentAccess
  179. */
  180. public function setGroups(array $groups): IDocumentAccess {
  181. $this->groups = $groups;
  182. return $this;
  183. }
  184. /**
  185. * Add an entry to the list of groups that have read access to the document.
  186. *
  187. * @since 16.0.0
  188. *
  189. * @param string $group
  190. *
  191. * @return IDocumentAccess
  192. */
  193. public function addGroup(string $group): IDocumentAccess {
  194. $this->groups[] = $group;
  195. return $this;
  196. }
  197. /**
  198. * Add multiple entries to the list of groups that have read access to the
  199. * document.
  200. *
  201. * @since 16.0.0
  202. *
  203. * @param array $groups
  204. *
  205. * @return IDocumentAccess
  206. */
  207. public function addGroups(array $groups) {
  208. $this->groups = array_merge($this->groups, $groups);
  209. return $this;
  210. }
  211. /**
  212. * Get the complete list of groups that have read access to the document.
  213. *
  214. * @since 16.0.0
  215. *
  216. * @return array
  217. */
  218. public function getGroups(): array {
  219. return $this->groups;
  220. }
  221. /**
  222. * Set the list of circles that have read access to the document.
  223. *
  224. * @since 16.0.0
  225. *
  226. * @param array $circles
  227. *
  228. * @return IDocumentAccess
  229. */
  230. public function setCircles(array $circles): IDocumentAccess {
  231. $this->circles = $circles;
  232. return $this;
  233. }
  234. /**
  235. * Add an entry to the list of circles that have read access to the document.
  236. *
  237. * @since 16.0.0
  238. *
  239. * @param string $circle
  240. *
  241. * @return IDocumentAccess
  242. */
  243. public function addCircle(string $circle): IDocumentAccess {
  244. $this->circles[] = $circle;
  245. return $this;
  246. }
  247. /**
  248. * Add multiple entries to the list of groups that have read access to the
  249. * document.
  250. *
  251. * @since 16.0.0
  252. *
  253. * @param array $circles
  254. *
  255. * @return IDocumentAccess
  256. */
  257. public function addCircles(array $circles): IDocumentAccess {
  258. $this->circles = array_merge($this->circles, $circles);
  259. return $this;
  260. }
  261. /**
  262. * Get the complete list of circles that have read access to the document.
  263. *
  264. * @since 16.0.0
  265. *
  266. * @return array
  267. */
  268. public function getCircles(): array {
  269. return $this->circles;
  270. }
  271. /**
  272. * Set the list of links that have read access to the document.
  273. *
  274. * @since 16.0.0
  275. *
  276. * @param array $links
  277. *
  278. * @return IDocumentAccess
  279. */
  280. public function setLinks(array $links): IDocumentAccess {
  281. $this->links = $links;
  282. return $this;
  283. }
  284. /**
  285. * Get the list of links that have read access to the document.
  286. *
  287. * @since 16.0.0
  288. *
  289. * @return array
  290. */
  291. public function getLinks(): array {
  292. return $this->links;
  293. }
  294. /**
  295. * @since 16.0.0
  296. *
  297. * @return array
  298. */
  299. public function jsonSerialize(): array {
  300. return [
  301. 'ownerId' => $this->getOwnerId(),
  302. 'viewerId' => $this->getViewerId(),
  303. 'users' => $this->getUsers(),
  304. 'groups' => $this->getGroups(),
  305. 'circles' => $this->getCircles(),
  306. 'links' => $this->getLinks()
  307. ];
  308. }
  309. }