ShareesAPIController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Maxence Lange <maxence@nextcloud.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Controller;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCS\OCSBadRequestException;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\Collaboration\Collaborators\ISearch;
  35. use OCP\IRequest;
  36. use OCP\IConfig;
  37. use OCP\IURLGenerator;
  38. use OCP\Share;
  39. use OCP\Share\IManager;
  40. class ShareesAPIController extends OCSController {
  41. /** @var IConfig */
  42. protected $config;
  43. /** @var IURLGenerator */
  44. protected $urlGenerator;
  45. /** @var IManager */
  46. protected $shareManager;
  47. /** @var bool */
  48. protected $shareWithGroupOnly = false;
  49. /** @var bool */
  50. protected $shareeEnumeration = true;
  51. /** @var int */
  52. protected $offset = 0;
  53. /** @var int */
  54. protected $limit = 10;
  55. /** @var array */
  56. protected $result = [
  57. 'exact' => [
  58. 'users' => [],
  59. 'groups' => [],
  60. 'remotes' => [],
  61. 'remote_groups' => [],
  62. 'emails' => [],
  63. 'circles' => [],
  64. 'rooms' => [],
  65. ],
  66. 'users' => [],
  67. 'groups' => [],
  68. 'remotes' => [],
  69. 'remote_groups' => [],
  70. 'emails' => [],
  71. 'lookup' => [],
  72. 'circles' => [],
  73. 'rooms' => [],
  74. ];
  75. protected $reachedEndFor = [];
  76. /** @var ISearch */
  77. private $collaboratorSearch;
  78. /**
  79. * @param string $appName
  80. * @param IRequest $request
  81. * @param IConfig $config
  82. * @param IURLGenerator $urlGenerator
  83. * @param IManager $shareManager
  84. * @param ISearch $collaboratorSearch
  85. */
  86. public function __construct(
  87. string $appName,
  88. IRequest $request,
  89. IConfig $config,
  90. IURLGenerator $urlGenerator,
  91. IManager $shareManager,
  92. ISearch $collaboratorSearch
  93. ) {
  94. parent::__construct($appName, $request);
  95. $this->config = $config;
  96. $this->urlGenerator = $urlGenerator;
  97. $this->shareManager = $shareManager;
  98. $this->collaboratorSearch = $collaboratorSearch;
  99. }
  100. /**
  101. * @NoAdminRequired
  102. *
  103. * @param string $search
  104. * @param string $itemType
  105. * @param int $page
  106. * @param int $perPage
  107. * @param int|int[] $shareType
  108. * @param bool $lookup
  109. * @return DataResponse
  110. * @throws OCSBadRequestException
  111. */
  112. public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse {
  113. // only search for string larger than a given threshold
  114. $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
  115. if (strlen($search) < $threshold) {
  116. return new DataResponse($this->result);
  117. }
  118. // never return more than the max. number of results configured in the config.php
  119. $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
  120. if ($maxResults > 0) {
  121. $perPage = min($perPage, $maxResults);
  122. }
  123. if ($perPage <= 0) {
  124. throw new OCSBadRequestException('Invalid perPage argument');
  125. }
  126. if ($page <= 0) {
  127. throw new OCSBadRequestException('Invalid page');
  128. }
  129. $shareTypes = [
  130. Share::SHARE_TYPE_USER,
  131. ];
  132. if ($itemType === null) {
  133. throw new OCSBadRequestException('Missing itemType');
  134. } elseif ($itemType === 'file' || $itemType === 'folder') {
  135. if ($this->shareManager->allowGroupSharing()) {
  136. $shareTypes[] = Share::SHARE_TYPE_GROUP;
  137. }
  138. if ($this->isRemoteSharingAllowed($itemType)) {
  139. $shareTypes[] = Share::SHARE_TYPE_REMOTE;
  140. }
  141. if ($this->isRemoteGroupSharingAllowed($itemType)) {
  142. $shareTypes[] = Share::SHARE_TYPE_REMOTE_GROUP;
  143. }
  144. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
  145. $shareTypes[] = Share::SHARE_TYPE_EMAIL;
  146. }
  147. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_ROOM)) {
  148. $shareTypes[] = Share::SHARE_TYPE_ROOM;
  149. }
  150. } else {
  151. $shareTypes[] = Share::SHARE_TYPE_GROUP;
  152. $shareTypes[] = Share::SHARE_TYPE_EMAIL;
  153. }
  154. // FIXME: DI
  155. if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
  156. $shareTypes[] = Share::SHARE_TYPE_CIRCLE;
  157. }
  158. if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
  159. $shareTypes = array_intersect($shareTypes, $_GET['shareType']);
  160. sort($shareTypes);
  161. } else if (is_numeric($shareType)) {
  162. $shareTypes = array_intersect($shareTypes, [(int) $shareType]);
  163. sort($shareTypes);
  164. }
  165. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  166. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  167. $this->limit = (int) $perPage;
  168. $this->offset = $perPage * ($page - 1);
  169. list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset);
  170. // extra treatment for 'exact' subarray, with a single merge expected keys might be lost
  171. if(isset($result['exact'])) {
  172. $result['exact'] = array_merge($this->result['exact'], $result['exact']);
  173. }
  174. $this->result = array_merge($this->result, $result);
  175. $response = new DataResponse($this->result);
  176. if ($hasMoreResults) {
  177. $response->addHeader('Link', $this->getPaginationLink($page, [
  178. 'search' => $search,
  179. 'itemType' => $itemType,
  180. 'shareType' => $shareTypes,
  181. 'perPage' => $perPage,
  182. ]));
  183. }
  184. return $response;
  185. }
  186. /**
  187. * Method to get out the static call for better testing
  188. *
  189. * @param string $itemType
  190. * @return bool
  191. */
  192. protected function isRemoteSharingAllowed(string $itemType): bool {
  193. try {
  194. // FIXME: static foo makes unit testing unnecessarily difficult
  195. $backend = \OC\Share\Share::getBackend($itemType);
  196. return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE);
  197. } catch (\Exception $e) {
  198. return false;
  199. }
  200. }
  201. protected function isRemoteGroupSharingAllowed(string $itemType): bool {
  202. try {
  203. // FIXME: static foo makes unit testing unnecessarily difficult
  204. $backend = \OC\Share\Share::getBackend($itemType);
  205. return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE_GROUP);
  206. } catch (\Exception $e) {
  207. return false;
  208. }
  209. }
  210. /**
  211. * Generates a bunch of pagination links for the current page
  212. *
  213. * @param int $page Current page
  214. * @param array $params Parameters for the URL
  215. * @return string
  216. */
  217. protected function getPaginationLink(int $page, array $params): string {
  218. if ($this->isV2()) {
  219. $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
  220. } else {
  221. $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
  222. }
  223. $params['page'] = $page + 1;
  224. return '<' . $url . http_build_query($params) . '>; rel="next"';
  225. }
  226. /**
  227. * @return bool
  228. */
  229. protected function isV2(): bool {
  230. return $this->request->getScriptName() === '/ocs/v2.php';
  231. }
  232. }