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 9.8KB

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