DocumentAccess.php 6.6 KB

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