@@ -40,204 +40,204 @@ |
||
| 40 | 40 | use OCP\Share\IManager; |
| 41 | 41 | |
| 42 | 42 | class ShareesAPIController extends OCSController { |
| 43 | - /** @var IConfig */ |
|
| 44 | - protected $config; |
|
| 45 | - |
|
| 46 | - /** @var IURLGenerator */ |
|
| 47 | - protected $urlGenerator; |
|
| 48 | - |
|
| 49 | - /** @var IManager */ |
|
| 50 | - protected $shareManager; |
|
| 51 | - |
|
| 52 | - /** @var bool */ |
|
| 53 | - protected $shareWithGroupOnly = false; |
|
| 54 | - |
|
| 55 | - /** @var bool */ |
|
| 56 | - protected $shareeEnumeration = true; |
|
| 57 | - |
|
| 58 | - /** @var int */ |
|
| 59 | - protected $offset = 0; |
|
| 60 | - |
|
| 61 | - /** @var int */ |
|
| 62 | - protected $limit = 10; |
|
| 63 | - |
|
| 64 | - /** @var array */ |
|
| 65 | - protected $result = [ |
|
| 66 | - 'exact' => [ |
|
| 67 | - 'users' => [], |
|
| 68 | - 'groups' => [], |
|
| 69 | - 'remotes' => [], |
|
| 70 | - 'emails' => [], |
|
| 71 | - 'circles' => [], |
|
| 72 | - ], |
|
| 73 | - 'users' => [], |
|
| 74 | - 'groups' => [], |
|
| 75 | - 'remotes' => [], |
|
| 76 | - 'emails' => [], |
|
| 77 | - 'lookup' => [], |
|
| 78 | - 'circles' => [], |
|
| 79 | - ]; |
|
| 80 | - |
|
| 81 | - protected $reachedEndFor = []; |
|
| 82 | - /** @var ISearch */ |
|
| 83 | - private $collaboratorSearch; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $appName |
|
| 87 | - * @param IRequest $request |
|
| 88 | - * @param IConfig $config |
|
| 89 | - * @param IURLGenerator $urlGenerator |
|
| 90 | - * @param IManager $shareManager |
|
| 91 | - * @param ISearch $collaboratorSearch |
|
| 92 | - */ |
|
| 93 | - public function __construct( |
|
| 94 | - string $appName, |
|
| 95 | - IRequest $request, |
|
| 96 | - IConfig $config, |
|
| 97 | - IURLGenerator $urlGenerator, |
|
| 98 | - IManager $shareManager, |
|
| 99 | - ISearch $collaboratorSearch |
|
| 100 | - ) { |
|
| 101 | - parent::__construct($appName, $request); |
|
| 102 | - |
|
| 103 | - $this->config = $config; |
|
| 104 | - $this->urlGenerator = $urlGenerator; |
|
| 105 | - $this->shareManager = $shareManager; |
|
| 106 | - $this->collaboratorSearch = $collaboratorSearch; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @NoAdminRequired |
|
| 111 | - * |
|
| 112 | - * @param string $search |
|
| 113 | - * @param string $itemType |
|
| 114 | - * @param int $page |
|
| 115 | - * @param int $perPage |
|
| 116 | - * @param int|int[] $shareType |
|
| 117 | - * @param bool $lookup |
|
| 118 | - * @return DataResponse |
|
| 119 | - * @throws OCSBadRequestException |
|
| 120 | - */ |
|
| 121 | - public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse { |
|
| 122 | - |
|
| 123 | - // only search for string larger than a given threshold |
|
| 124 | - $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
| 125 | - if (strlen($search) < $threshold) { |
|
| 126 | - return new DataResponse($this->result); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // never return more than the max. number of results configured in the config.php |
|
| 130 | - $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
| 131 | - if ($maxResults > 0) { |
|
| 132 | - $perPage = min($perPage, $maxResults); |
|
| 133 | - } |
|
| 134 | - if ($perPage <= 0) { |
|
| 135 | - throw new OCSBadRequestException('Invalid perPage argument'); |
|
| 136 | - } |
|
| 137 | - if ($page <= 0) { |
|
| 138 | - throw new OCSBadRequestException('Invalid page'); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $shareTypes = [ |
|
| 142 | - Share::SHARE_TYPE_USER, |
|
| 143 | - ]; |
|
| 144 | - |
|
| 145 | - if ($itemType === null) { |
|
| 146 | - throw new OCSBadRequestException('Missing itemType'); |
|
| 147 | - } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
| 148 | - if ($this->shareManager->allowGroupSharing()) { |
|
| 149 | - $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if ($this->isRemoteSharingAllowed($itemType)) { |
|
| 153 | - $shareTypes[] = Share::SHARE_TYPE_REMOTE; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 157 | - $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
| 158 | - } |
|
| 159 | - } else { |
|
| 160 | - $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
| 161 | - $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // FIXME: DI |
|
| 165 | - if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 166 | - $shareTypes[] = Share::SHARE_TYPE_CIRCLE; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
| 170 | - $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
| 171 | - sort($shareTypes); |
|
| 172 | - } else if (is_numeric($shareType)) { |
|
| 173 | - $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
| 174 | - sort($shareTypes); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 178 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 179 | - $this->limit = (int) $perPage; |
|
| 180 | - $this->offset = $perPage * ($page - 1); |
|
| 181 | - |
|
| 182 | - list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
| 183 | - |
|
| 184 | - // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
| 185 | - if(isset($result['exact'])) { |
|
| 186 | - $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
| 187 | - } |
|
| 188 | - $this->result = array_merge($this->result, $result); |
|
| 189 | - $response = new DataResponse($this->result); |
|
| 190 | - |
|
| 191 | - if ($hasMoreResults) { |
|
| 192 | - $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
| 193 | - 'search' => $search, |
|
| 194 | - 'itemType' => $itemType, |
|
| 195 | - 'shareType' => $shareTypes, |
|
| 196 | - 'perPage' => $perPage, |
|
| 197 | - ])); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - return $response; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Method to get out the static call for better testing |
|
| 205 | - * |
|
| 206 | - * @param string $itemType |
|
| 207 | - * @return bool |
|
| 208 | - */ |
|
| 209 | - protected function isRemoteSharingAllowed(string $itemType): bool { |
|
| 210 | - try { |
|
| 211 | - // FIXME: static foo makes unit testing unnecessarily difficult |
|
| 212 | - $backend = \OC\Share\Share::getBackend($itemType); |
|
| 213 | - return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); |
|
| 214 | - } catch (\Exception $e) { |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Generates a bunch of pagination links for the current page |
|
| 222 | - * |
|
| 223 | - * @param int $page Current page |
|
| 224 | - * @param array $params Parameters for the URL |
|
| 225 | - * @return string |
|
| 226 | - */ |
|
| 227 | - protected function getPaginationLink(int $page, array $params): string { |
|
| 228 | - if ($this->isV2()) { |
|
| 229 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 230 | - } else { |
|
| 231 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 232 | - } |
|
| 233 | - $params['page'] = $page + 1; |
|
| 234 | - return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @return bool |
|
| 239 | - */ |
|
| 240 | - protected function isV2(): bool { |
|
| 241 | - return $this->request->getScriptName() === '/ocs/v2.php'; |
|
| 242 | - } |
|
| 43 | + /** @var IConfig */ |
|
| 44 | + protected $config; |
|
| 45 | + |
|
| 46 | + /** @var IURLGenerator */ |
|
| 47 | + protected $urlGenerator; |
|
| 48 | + |
|
| 49 | + /** @var IManager */ |
|
| 50 | + protected $shareManager; |
|
| 51 | + |
|
| 52 | + /** @var bool */ |
|
| 53 | + protected $shareWithGroupOnly = false; |
|
| 54 | + |
|
| 55 | + /** @var bool */ |
|
| 56 | + protected $shareeEnumeration = true; |
|
| 57 | + |
|
| 58 | + /** @var int */ |
|
| 59 | + protected $offset = 0; |
|
| 60 | + |
|
| 61 | + /** @var int */ |
|
| 62 | + protected $limit = 10; |
|
| 63 | + |
|
| 64 | + /** @var array */ |
|
| 65 | + protected $result = [ |
|
| 66 | + 'exact' => [ |
|
| 67 | + 'users' => [], |
|
| 68 | + 'groups' => [], |
|
| 69 | + 'remotes' => [], |
|
| 70 | + 'emails' => [], |
|
| 71 | + 'circles' => [], |
|
| 72 | + ], |
|
| 73 | + 'users' => [], |
|
| 74 | + 'groups' => [], |
|
| 75 | + 'remotes' => [], |
|
| 76 | + 'emails' => [], |
|
| 77 | + 'lookup' => [], |
|
| 78 | + 'circles' => [], |
|
| 79 | + ]; |
|
| 80 | + |
|
| 81 | + protected $reachedEndFor = []; |
|
| 82 | + /** @var ISearch */ |
|
| 83 | + private $collaboratorSearch; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $appName |
|
| 87 | + * @param IRequest $request |
|
| 88 | + * @param IConfig $config |
|
| 89 | + * @param IURLGenerator $urlGenerator |
|
| 90 | + * @param IManager $shareManager |
|
| 91 | + * @param ISearch $collaboratorSearch |
|
| 92 | + */ |
|
| 93 | + public function __construct( |
|
| 94 | + string $appName, |
|
| 95 | + IRequest $request, |
|
| 96 | + IConfig $config, |
|
| 97 | + IURLGenerator $urlGenerator, |
|
| 98 | + IManager $shareManager, |
|
| 99 | + ISearch $collaboratorSearch |
|
| 100 | + ) { |
|
| 101 | + parent::__construct($appName, $request); |
|
| 102 | + |
|
| 103 | + $this->config = $config; |
|
| 104 | + $this->urlGenerator = $urlGenerator; |
|
| 105 | + $this->shareManager = $shareManager; |
|
| 106 | + $this->collaboratorSearch = $collaboratorSearch; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @NoAdminRequired |
|
| 111 | + * |
|
| 112 | + * @param string $search |
|
| 113 | + * @param string $itemType |
|
| 114 | + * @param int $page |
|
| 115 | + * @param int $perPage |
|
| 116 | + * @param int|int[] $shareType |
|
| 117 | + * @param bool $lookup |
|
| 118 | + * @return DataResponse |
|
| 119 | + * @throws OCSBadRequestException |
|
| 120 | + */ |
|
| 121 | + public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse { |
|
| 122 | + |
|
| 123 | + // only search for string larger than a given threshold |
|
| 124 | + $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
| 125 | + if (strlen($search) < $threshold) { |
|
| 126 | + return new DataResponse($this->result); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // never return more than the max. number of results configured in the config.php |
|
| 130 | + $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
| 131 | + if ($maxResults > 0) { |
|
| 132 | + $perPage = min($perPage, $maxResults); |
|
| 133 | + } |
|
| 134 | + if ($perPage <= 0) { |
|
| 135 | + throw new OCSBadRequestException('Invalid perPage argument'); |
|
| 136 | + } |
|
| 137 | + if ($page <= 0) { |
|
| 138 | + throw new OCSBadRequestException('Invalid page'); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $shareTypes = [ |
|
| 142 | + Share::SHARE_TYPE_USER, |
|
| 143 | + ]; |
|
| 144 | + |
|
| 145 | + if ($itemType === null) { |
|
| 146 | + throw new OCSBadRequestException('Missing itemType'); |
|
| 147 | + } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
| 148 | + if ($this->shareManager->allowGroupSharing()) { |
|
| 149 | + $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if ($this->isRemoteSharingAllowed($itemType)) { |
|
| 153 | + $shareTypes[] = Share::SHARE_TYPE_REMOTE; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 157 | + $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
| 158 | + } |
|
| 159 | + } else { |
|
| 160 | + $shareTypes[] = Share::SHARE_TYPE_GROUP; |
|
| 161 | + $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // FIXME: DI |
|
| 165 | + if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 166 | + $shareTypes[] = Share::SHARE_TYPE_CIRCLE; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
| 170 | + $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
| 171 | + sort($shareTypes); |
|
| 172 | + } else if (is_numeric($shareType)) { |
|
| 173 | + $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
| 174 | + sort($shareTypes); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 178 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 179 | + $this->limit = (int) $perPage; |
|
| 180 | + $this->offset = $perPage * ($page - 1); |
|
| 181 | + |
|
| 182 | + list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
| 183 | + |
|
| 184 | + // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
| 185 | + if(isset($result['exact'])) { |
|
| 186 | + $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
| 187 | + } |
|
| 188 | + $this->result = array_merge($this->result, $result); |
|
| 189 | + $response = new DataResponse($this->result); |
|
| 190 | + |
|
| 191 | + if ($hasMoreResults) { |
|
| 192 | + $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
| 193 | + 'search' => $search, |
|
| 194 | + 'itemType' => $itemType, |
|
| 195 | + 'shareType' => $shareTypes, |
|
| 196 | + 'perPage' => $perPage, |
|
| 197 | + ])); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + return $response; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Method to get out the static call for better testing |
|
| 205 | + * |
|
| 206 | + * @param string $itemType |
|
| 207 | + * @return bool |
|
| 208 | + */ |
|
| 209 | + protected function isRemoteSharingAllowed(string $itemType): bool { |
|
| 210 | + try { |
|
| 211 | + // FIXME: static foo makes unit testing unnecessarily difficult |
|
| 212 | + $backend = \OC\Share\Share::getBackend($itemType); |
|
| 213 | + return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); |
|
| 214 | + } catch (\Exception $e) { |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Generates a bunch of pagination links for the current page |
|
| 222 | + * |
|
| 223 | + * @param int $page Current page |
|
| 224 | + * @param array $params Parameters for the URL |
|
| 225 | + * @return string |
|
| 226 | + */ |
|
| 227 | + protected function getPaginationLink(int $page, array $params): string { |
|
| 228 | + if ($this->isV2()) { |
|
| 229 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 230 | + } else { |
|
| 231 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 232 | + } |
|
| 233 | + $params['page'] = $page + 1; |
|
| 234 | + return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @return bool |
|
| 239 | + */ |
|
| 240 | + protected function isV2(): bool { |
|
| 241 | + return $this->request->getScriptName() === '/ocs/v2.php'; |
|
| 242 | + } |
|
| 243 | 243 | } |
@@ -121,13 +121,13 @@ discard block |
||
| 121 | 121 | public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse { |
| 122 | 122 | |
| 123 | 123 | // only search for string larger than a given threshold |
| 124 | - $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
| 124 | + $threshold = (int) $this->config->getSystemValue('sharing.minSearchStringLength', 0); |
|
| 125 | 125 | if (strlen($search) < $threshold) { |
| 126 | 126 | return new DataResponse($this->result); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // never return more than the max. number of results configured in the config.php |
| 130 | - $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
| 130 | + $maxResults = (int) $this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
|
| 131 | 131 | if ($maxResults > 0) { |
| 132 | 132 | $perPage = min($perPage, $maxResults); |
| 133 | 133 | } |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
| 183 | 183 | |
| 184 | 184 | // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
| 185 | - if(isset($result['exact'])) { |
|
| 185 | + if (isset($result['exact'])) { |
|
| 186 | 186 | $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
| 187 | 187 | } |
| 188 | 188 | $this->result = array_merge($this->result, $result); |
@@ -226,12 +226,12 @@ discard block |
||
| 226 | 226 | */ |
| 227 | 227 | protected function getPaginationLink(int $page, array $params): string { |
| 228 | 228 | if ($this->isV2()) { |
| 229 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 229 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees').'?'; |
|
| 230 | 230 | } else { |
| 231 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 231 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees').'?'; |
|
| 232 | 232 | } |
| 233 | 233 | $params['page'] = $page + 1; |
| 234 | - return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
| 234 | + return '<'.$url.http_build_query($params).'>; rel="next"'; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |