You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestHandlerController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Maxence Lange <maxence@artificial-owl.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Kate Döen <kate.doeen@nextcloud.com>
  10. *
  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 OCA\CloudFederationAPI\Controller;
  28. use OCA\CloudFederationAPI\Config;
  29. use OCA\CloudFederationAPI\ResponseDefinitions;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\JSONResponse;
  33. use OCP\Federation\Exceptions\ActionNotSupportedException;
  34. use OCP\Federation\Exceptions\AuthenticationFailedException;
  35. use OCP\Federation\Exceptions\BadRequestException;
  36. use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
  37. use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
  38. use OCP\Federation\ICloudFederationFactory;
  39. use OCP\Federation\ICloudFederationProviderManager;
  40. use OCP\Federation\ICloudIdManager;
  41. use OCP\IGroupManager;
  42. use OCP\IRequest;
  43. use OCP\IURLGenerator;
  44. use OCP\IUserManager;
  45. use OCP\Share\Exceptions\ShareNotFound;
  46. use Psr\Log\LoggerInterface;
  47. /**
  48. * Open-Cloud-Mesh-API
  49. *
  50. * @package OCA\CloudFederationAPI\Controller
  51. *
  52. * @psalm-import-type CloudFederationApiAddShare from ResponseDefinitions
  53. * @psalm-import-type CloudFederationApiValidationError from ResponseDefinitions
  54. * @psalm-import-type CloudFederationApiError from ResponseDefinitions
  55. */
  56. class RequestHandlerController extends Controller {
  57. public function __construct(
  58. string $appName,
  59. IRequest $request,
  60. private LoggerInterface $logger,
  61. private IUserManager $userManager,
  62. private IGroupManager $groupManager,
  63. private IURLGenerator $urlGenerator,
  64. private ICloudFederationProviderManager $cloudFederationProviderManager,
  65. private Config $config,
  66. private ICloudFederationFactory $factory,
  67. private ICloudIdManager $cloudIdManager
  68. ) {
  69. parent::__construct($appName, $request);
  70. }
  71. /**
  72. * Add share
  73. *
  74. * @NoCSRFRequired
  75. * @PublicPage
  76. * @BruteForceProtection(action=receiveFederatedShare)
  77. *
  78. * @param string $shareWith The user who the share will be shared with
  79. * @param string $name The resource name (e.g. document.odt)
  80. * @param string|null $description Share description
  81. * @param string $providerId Resource UID on the provider side
  82. * @param string $owner Provider specific UID of the user who owns the resource
  83. * @param string|null $ownerDisplayName Display name of the user who shared the item
  84. * @param string|null $sharedBy Provider specific UID of the user who shared the resource
  85. * @param string|null $sharedByDisplayName Display name of the user who shared the resource
  86. * @param array{name: string[], options: array<string, mixed>} $protocol e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]]
  87. * @param string $shareType 'group' or 'user' share
  88. * @param string $resourceType 'file', 'calendar',...
  89. *
  90. * @return JSONResponse<Http::STATUS_CREATED, CloudFederationApiAddShare, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, CloudFederationApiValidationError, array{}>|JSONResponse<Http::STATUS_NOT_IMPLEMENTED, CloudFederationApiError, array{}>
  91. * 201: The notification was successfully received. The display name of the recipient might be returned in the body
  92. * 400: Bad request due to invalid parameters, e.g. when `shareWith` is not found or required properties are missing
  93. * 501: Share type or the resource type is not supported
  94. */
  95. public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) {
  96. // check if all required parameters are set
  97. if ($shareWith === null ||
  98. $name === null ||
  99. $providerId === null ||
  100. $owner === null ||
  101. $resourceType === null ||
  102. $shareType === null ||
  103. !is_array($protocol) ||
  104. !isset($protocol['name']) ||
  105. !isset($protocol['options']) ||
  106. !is_array($protocol['options']) ||
  107. !isset($protocol['options']['sharedSecret'])
  108. ) {
  109. return new JSONResponse(
  110. [
  111. 'message' => 'Missing arguments',
  112. 'validationErrors' => [],
  113. ],
  114. Http::STATUS_BAD_REQUEST
  115. );
  116. }
  117. $supportedShareTypes = $this->config->getSupportedShareTypes($resourceType);
  118. if (!in_array($shareType, $supportedShareTypes)) {
  119. return new JSONResponse(
  120. ['message' => 'Share type "' . $shareType . '" not implemented'],
  121. Http::STATUS_NOT_IMPLEMENTED
  122. );
  123. }
  124. $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
  125. $shareWith = $cloudId->getUser();
  126. if ($shareType === 'user') {
  127. $shareWith = $this->mapUid($shareWith);
  128. if (!$this->userManager->userExists($shareWith)) {
  129. $response = new JSONResponse(
  130. [
  131. 'message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl(),
  132. 'validationErrors' => [],
  133. ],
  134. Http::STATUS_BAD_REQUEST
  135. );
  136. $response->throttle();
  137. return $response;
  138. }
  139. }
  140. if ($shareType === 'group') {
  141. if (!$this->groupManager->groupExists($shareWith)) {
  142. $response = new JSONResponse(
  143. [
  144. 'message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl(),
  145. 'validationErrors' => [],
  146. ],
  147. Http::STATUS_BAD_REQUEST
  148. );
  149. $response->throttle();
  150. return $response;
  151. }
  152. }
  153. // if no explicit display name is given, we use the uid as display name
  154. $ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName;
  155. $sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName;
  156. // sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner
  157. if ($sharedBy === null) {
  158. $sharedBy = $owner;
  159. $sharedByDisplayName = $ownerDisplayName;
  160. }
  161. try {
  162. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
  163. $share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType);
  164. $share->setProtocol($protocol);
  165. $provider->shareReceived($share);
  166. } catch (ProviderDoesNotExistsException|ProviderCouldNotAddShareException $e) {
  167. return new JSONResponse(
  168. ['message' => $e->getMessage()],
  169. Http::STATUS_NOT_IMPLEMENTED
  170. );
  171. } catch (\Exception $e) {
  172. $this->logger->error($e->getMessage(), ['exception' => $e]);
  173. return new JSONResponse(
  174. [
  175. 'message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl(),
  176. 'validationErrors' => [],
  177. ],
  178. Http::STATUS_BAD_REQUEST
  179. );
  180. }
  181. $user = $this->userManager->get($shareWith);
  182. $recipientDisplayName = '';
  183. if ($user) {
  184. $recipientDisplayName = $user->getDisplayName();
  185. }
  186. return new JSONResponse(
  187. ['recipientDisplayName' => $recipientDisplayName],
  188. Http::STATUS_CREATED);
  189. }
  190. /**
  191. * Send a notification about an existing share
  192. *
  193. * @NoCSRFRequired
  194. * @PublicPage
  195. * @BruteForceProtection(action=receiveFederatedShareNotification)
  196. *
  197. * @param string $notificationType Notification type, e.g. SHARE_ACCEPTED
  198. * @param string $resourceType calendar, file, contact,...
  199. * @param string|null $providerId ID of the share
  200. * @param array<string, mixed>|null $notification The actual payload of the notification
  201. *
  202. * @return JSONResponse<Http::STATUS_CREATED, array<string, mixed>, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, CloudFederationApiValidationError, array{}>|JSONResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_IMPLEMENTED, CloudFederationApiError, array{}>
  203. * 201: The notification was successfully received
  204. * 400: Bad request due to invalid parameters, e.g. when `type` is invalid or missing
  205. * 403: Getting resource is not allowed
  206. * 501: The resource type is not supported
  207. */
  208. public function receiveNotification($notificationType, $resourceType, $providerId, ?array $notification) {
  209. // check if all required parameters are set
  210. if ($notificationType === null ||
  211. $resourceType === null ||
  212. $providerId === null ||
  213. !is_array($notification)
  214. ) {
  215. return new JSONResponse(
  216. [
  217. 'message' => 'Missing arguments',
  218. 'validationErrors' => [],
  219. ],
  220. Http::STATUS_BAD_REQUEST
  221. );
  222. }
  223. try {
  224. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
  225. $result = $provider->notificationReceived($notificationType, $providerId, $notification);
  226. } catch (ProviderDoesNotExistsException $e) {
  227. return new JSONResponse(
  228. [
  229. 'message' => $e->getMessage(),
  230. 'validationErrors' => [],
  231. ],
  232. Http::STATUS_BAD_REQUEST
  233. );
  234. } catch (ShareNotFound $e) {
  235. $response = new JSONResponse(
  236. [
  237. 'message' => $e->getMessage(),
  238. 'validationErrors' => [],
  239. ],
  240. Http::STATUS_BAD_REQUEST
  241. );
  242. $response->throttle();
  243. return $response;
  244. } catch (ActionNotSupportedException $e) {
  245. return new JSONResponse(
  246. ['message' => $e->getMessage()],
  247. Http::STATUS_NOT_IMPLEMENTED
  248. );
  249. } catch (BadRequestException $e) {
  250. return new JSONResponse($e->getReturnMessage(), Http::STATUS_BAD_REQUEST);
  251. } catch (AuthenticationFailedException $e) {
  252. $response = new JSONResponse(['message' => 'RESOURCE_NOT_FOUND'], Http::STATUS_FORBIDDEN);
  253. $response->throttle();
  254. return $response;
  255. } catch (\Exception $e) {
  256. return new JSONResponse(
  257. [
  258. 'message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl(),
  259. 'validationErrors' => [],
  260. ],
  261. Http::STATUS_BAD_REQUEST
  262. );
  263. }
  264. return new JSONResponse($result, Http::STATUS_CREATED);
  265. }
  266. /**
  267. * map login name to internal LDAP UID if a LDAP backend is in use
  268. *
  269. * @param string $uid
  270. * @return string mixed
  271. */
  272. private function mapUid($uid) {
  273. // FIXME this should be a method in the user management instead
  274. $this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
  275. \OCP\Util::emitHook(
  276. '\OCA\Files_Sharing\API\Server2Server',
  277. 'preLoginNameUsedAsUserName',
  278. ['uid' => &$uid]
  279. );
  280. $this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]);
  281. return $uid;
  282. }
  283. }