RequestHandlerController.php 9.8 KB

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