RequestHandlerController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\CloudFederationAPI\Controller;
  26. use OCA\CloudFederationAPI\Config;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\JSONResponse;
  30. use OCP\Federation\Exceptions\ActionNotSupportedException;
  31. use OCP\Federation\Exceptions\AuthenticationFailedException;
  32. use OCP\Federation\Exceptions\BadRequestException;
  33. use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
  34. use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
  35. use OCP\Federation\ICloudFederationFactory;
  36. use OCP\Federation\ICloudFederationProviderManager;
  37. use OCP\Federation\ICloudIdManager;
  38. use OCP\IGroupManager;
  39. use OCP\IRequest;
  40. use OCP\IURLGenerator;
  41. use OCP\IUserManager;
  42. use OCP\Share\Exceptions\ShareNotFound;
  43. use Psr\Log\LoggerInterface;
  44. /**
  45. * Class RequestHandlerController
  46. *
  47. * handle API between different Cloud instances
  48. *
  49. * @package OCA\CloudFederationAPI\Controller
  50. */
  51. class RequestHandlerController extends Controller {
  52. /** @var LoggerInterface */
  53. private $logger;
  54. /** @var IUserManager */
  55. private $userManager;
  56. /** @var IGroupManager */
  57. private $groupManager;
  58. /** @var IURLGenerator */
  59. private $urlGenerator;
  60. /** @var ICloudFederationProviderManager */
  61. private $cloudFederationProviderManager;
  62. /** @var Config */
  63. private $config;
  64. /** @var ICloudFederationFactory */
  65. private $factory;
  66. /** @var ICloudIdManager */
  67. private $cloudIdManager;
  68. public function __construct($appName,
  69. IRequest $request,
  70. LoggerInterface $logger,
  71. IUserManager $userManager,
  72. IGroupManager $groupManager,
  73. IURLGenerator $urlGenerator,
  74. ICloudFederationProviderManager $cloudFederationProviderManager,
  75. Config $config,
  76. ICloudFederationFactory $factory,
  77. ICloudIdManager $cloudIdManager
  78. ) {
  79. parent::__construct($appName, $request);
  80. $this->logger = $logger;
  81. $this->userManager = $userManager;
  82. $this->groupManager = $groupManager;
  83. $this->urlGenerator = $urlGenerator;
  84. $this->cloudFederationProviderManager = $cloudFederationProviderManager;
  85. $this->config = $config;
  86. $this->factory = $factory;
  87. $this->cloudIdManager = $cloudIdManager;
  88. }
  89. /**
  90. * add share
  91. *
  92. * @NoCSRFRequired
  93. * @PublicPage
  94. * @BruteForceProtection(action=receiveFederatedShare)
  95. *
  96. * @param string $shareWith
  97. * @param string $name resource name (e.g. document.odt)
  98. * @param string $description share description (optional)
  99. * @param string $providerId resource UID on the provider side
  100. * @param string $owner provider specific UID of the user who owns the resource
  101. * @param string $ownerDisplayName display name of the user who shared the item
  102. * @param string $sharedBy provider specific UID of the user who shared the resource
  103. * @param string $sharedByDisplayName display name of the user who shared the resource
  104. * @param array $protocol (e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]])
  105. * @param string $shareType ('group' or 'user' share)
  106. * @param $resourceType ('file', 'calendar',...)
  107. * @return Http\DataResponse|JSONResponse
  108. *
  109. * Example: curl -H "Content-Type: application/json" -X POST -d '{"shareWith":"admin1@serve1","name":"welcome server2.txt","description":"desc","providerId":"2","owner":"admin2@http://localhost/server2","ownerDisplayName":"admin2 display","shareType":"user","resourceType":"file","protocol":{"name":"webdav","options":{"sharedSecret":"secret","permissions":"webdav-property"}}}' http://localhost/server/index.php/ocm/shares
  110. */
  111. public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) {
  112. // check if all required parameters are set
  113. if ($shareWith === null ||
  114. $name === null ||
  115. $providerId === null ||
  116. $owner === null ||
  117. $resourceType === null ||
  118. $shareType === null ||
  119. !is_array($protocol) ||
  120. !isset($protocol['name']) ||
  121. !isset($protocol['options']) ||
  122. !is_array($protocol['options']) ||
  123. !isset($protocol['options']['sharedSecret'])
  124. ) {
  125. return new JSONResponse(
  126. ['message' => 'Missing arguments'],
  127. Http::STATUS_BAD_REQUEST
  128. );
  129. }
  130. $supportedShareTypes = $this->config->getSupportedShareTypes($resourceType);
  131. if (!in_array($shareType, $supportedShareTypes)) {
  132. return new JSONResponse(
  133. ['message' => 'Share type "' . $shareType . '" not implemented'],
  134. Http::STATUS_NOT_IMPLEMENTED
  135. );
  136. }
  137. $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
  138. $shareWith = $cloudId->getUser();
  139. if ($shareType === 'user') {
  140. $shareWith = $this->mapUid($shareWith);
  141. if (!$this->userManager->userExists($shareWith)) {
  142. $response = new JSONResponse(
  143. ['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
  144. Http::STATUS_BAD_REQUEST
  145. );
  146. $response->throttle();
  147. return $response;
  148. }
  149. }
  150. if ($shareType === 'group') {
  151. if (!$this->groupManager->groupExists($shareWith)) {
  152. $response = new JSONResponse(
  153. ['message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
  154. Http::STATUS_BAD_REQUEST
  155. );
  156. $response->throttle();
  157. return $response;
  158. }
  159. }
  160. // if no explicit display name is given, we use the uid as display name
  161. $ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName;
  162. $sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName;
  163. // sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner
  164. if ($sharedBy === null) {
  165. $sharedBy = $owner;
  166. $sharedByDisplayName = $ownerDisplayName;
  167. }
  168. try {
  169. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
  170. $share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType);
  171. $share->setProtocol($protocol);
  172. $provider->shareReceived($share);
  173. } catch (ProviderDoesNotExistsException $e) {
  174. return new JSONResponse(
  175. ['message' => $e->getMessage()],
  176. Http::STATUS_NOT_IMPLEMENTED
  177. );
  178. } catch (ProviderCouldNotAddShareException $e) {
  179. return new JSONResponse(
  180. ['message' => $e->getMessage()],
  181. $e->getCode()
  182. );
  183. } catch (\Exception $e) {
  184. $this->logger->error($e->getMessage(), ['exception' => $e]);
  185. return new JSONResponse(
  186. ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
  187. Http::STATUS_BAD_REQUEST
  188. );
  189. }
  190. $user = $this->userManager->get($shareWith);
  191. $recipientDisplayName = '';
  192. if ($user) {
  193. $recipientDisplayName = $user->getDisplayName();
  194. }
  195. return new JSONResponse(
  196. ['recipientDisplayName' => $recipientDisplayName],
  197. Http::STATUS_CREATED);
  198. }
  199. /**
  200. * receive notification about existing share
  201. *
  202. * @NoCSRFRequired
  203. * @PublicPage
  204. * @BruteForceProtection(action=receiveFederatedShareNotification)
  205. *
  206. * @param string $notificationType (notification type, e.g. SHARE_ACCEPTED)
  207. * @param string $resourceType (calendar, file, contact,...)
  208. * @param string $providerId id of the share
  209. * @param array $notification the actual payload of the notification
  210. * @return JSONResponse
  211. */
  212. public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) {
  213. // check if all required parameters are set
  214. if ($notificationType === null ||
  215. $resourceType === null ||
  216. $providerId === null ||
  217. !is_array($notification)
  218. ) {
  219. return new JSONResponse(
  220. ['message' => 'Missing arguments'],
  221. Http::STATUS_BAD_REQUEST
  222. );
  223. }
  224. try {
  225. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
  226. $result = $provider->notificationReceived($notificationType, $providerId, $notification);
  227. } catch (ProviderDoesNotExistsException $e) {
  228. return new JSONResponse(
  229. ['message' => $e->getMessage()],
  230. Http::STATUS_BAD_REQUEST
  231. );
  232. } catch (ShareNotFound $e) {
  233. $response = new JSONResponse(
  234. ['message' => $e->getMessage()],
  235. Http::STATUS_BAD_REQUEST
  236. );
  237. $response->throttle();
  238. return $response;
  239. } catch (ActionNotSupportedException $e) {
  240. return new JSONResponse(
  241. ['message' => $e->getMessage()],
  242. Http::STATUS_NOT_IMPLEMENTED
  243. );
  244. } catch (BadRequestException $e) {
  245. return new JSONResponse($e->getReturnMessage(), Http::STATUS_BAD_REQUEST);
  246. } catch (AuthenticationFailedException $e) {
  247. $response = new JSONResponse(['message' => 'RESOURCE_NOT_FOUND'], Http::STATUS_FORBIDDEN);
  248. $response->throttle();
  249. return $response;
  250. } catch (\Exception $e) {
  251. return new JSONResponse(
  252. ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
  253. Http::STATUS_BAD_REQUEST
  254. );
  255. }
  256. return new JSONResponse($result,Http::STATUS_CREATED);
  257. }
  258. /**
  259. * map login name to internal LDAP UID if a LDAP backend is in use
  260. *
  261. * @param string $uid
  262. * @return string mixed
  263. */
  264. private function mapUid($uid) {
  265. // FIXME this should be a method in the user management instead
  266. $this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
  267. \OCP\Util::emitHook(
  268. '\OCA\Files_Sharing\API\Server2Server',
  269. 'preLoginNameUsedAsUserName',
  270. ['uid' => &$uid]
  271. );
  272. $this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]);
  273. return $uid;
  274. }
  275. }