RequestHandlerController.php 9.9 KB

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 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\ILogger;
  40. use OCP\IRequest;
  41. use OCP\IURLGenerator;
  42. use OCP\IUserManager;
  43. use OCP\Share\Exceptions\ShareNotFound;
  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 ILogger */
  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. ILogger $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. return new JSONResponse(
  143. ['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
  144. Http::STATUS_BAD_REQUEST
  145. );
  146. }
  147. }
  148. if ($shareType === 'group') {
  149. if(!$this->groupManager->groupExists($shareWith)) {
  150. return new JSONResponse(
  151. ['message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
  152. Http::STATUS_BAD_REQUEST
  153. );
  154. }
  155. }
  156. // if no explicit display name is given, we use the uid as display name
  157. $ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName;
  158. $sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName;
  159. // sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner
  160. if ($sharedBy === null) {
  161. $sharedBy = $owner;
  162. $sharedByDisplayName = $ownerDisplayName;
  163. }
  164. try {
  165. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
  166. $share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType);
  167. $share->setProtocol($protocol);
  168. $provider->shareReceived($share);
  169. } catch (ProviderDoesNotExistsException $e) {
  170. return new JSONResponse(
  171. ['message' => $e->getMessage()],
  172. Http::STATUS_NOT_IMPLEMENTED
  173. );
  174. } catch (ProviderCouldNotAddShareException $e) {
  175. return new JSONResponse(
  176. ['message' => $e->getMessage()],
  177. $e->getCode()
  178. );
  179. } catch (\Exception $e) {
  180. return new JSONResponse(
  181. ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
  182. Http::STATUS_BAD_REQUEST
  183. );
  184. }
  185. $user = $this->userManager->get($shareWith);
  186. $recipientDisplayName = '';
  187. if($user) {
  188. $recipientDisplayName = $user->getDisplayName();
  189. }
  190. return new JSONResponse(
  191. ['recipientDisplayName' => $recipientDisplayName],
  192. Http::STATUS_CREATED);
  193. }
  194. /**
  195. * receive notification about existing share
  196. *
  197. * @NoCSRFRequired
  198. * @PublicPage
  199. * @BruteForceProtection(action=receiveFederatedShareNotification)
  200. *
  201. * @param string $notificationType (notification type, e.g. SHARE_ACCEPTED)
  202. * @param string $resourceType (calendar, file, contact,...)
  203. * @param string $providerId id of the share
  204. * @param array $notification the actual payload of the notification
  205. * @return JSONResponse
  206. */
  207. public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) {
  208. // check if all required parameters are set
  209. if ($notificationType === null ||
  210. $resourceType === null ||
  211. $providerId === null ||
  212. !is_array($notification)
  213. ) {
  214. return new JSONResponse(
  215. ['message' => 'Missing arguments'],
  216. Http::STATUS_BAD_REQUEST
  217. );
  218. }
  219. try {
  220. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
  221. $result = $provider->notificationReceived($notificationType, $providerId, $notification);
  222. } catch (ProviderDoesNotExistsException $e) {
  223. return new JSONResponse(
  224. ['message' => $e->getMessage()],
  225. Http::STATUS_BAD_REQUEST
  226. );
  227. } catch (ShareNotFound $e) {
  228. return new JSONResponse(
  229. ['message' => $e->getMessage()],
  230. Http::STATUS_BAD_REQUEST
  231. );
  232. } catch (ActionNotSupportedException $e) {
  233. return new JSONResponse(
  234. ['message' => $e->getMessage()],
  235. Http::STATUS_NOT_IMPLEMENTED
  236. );
  237. } catch (BadRequestException $e) {
  238. return new JSONResponse($e->getReturnMessage(), Http::STATUS_BAD_REQUEST);
  239. } catch (AuthenticationFailedException $e) {
  240. return new JSONResponse(["message" => "RESOURCE_NOT_FOUND"], Http::STATUS_FORBIDDEN);
  241. }
  242. catch (\Exception $e) {
  243. return new JSONResponse(
  244. ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
  245. Http::STATUS_BAD_REQUEST
  246. );
  247. }
  248. return new JSONResponse($result,Http::STATUS_CREATED);
  249. }
  250. /**
  251. * map login name to internal LDAP UID if a LDAP backend is in use
  252. *
  253. * @param string $uid
  254. * @return string mixed
  255. */
  256. private function mapUid($uid) {
  257. // FIXME this should be a method in the user management instead
  258. $this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
  259. \OCP\Util::emitHook(
  260. '\OCA\Files_Sharing\API\Server2Server',
  261. 'preLoginNameUsedAsUserName',
  262. ['uid' => &$uid]
  263. );
  264. $this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]);
  265. return $uid;
  266. }
  267. }