|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Contacts\Controller; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use OC\App\CompareVersion; |
| 12 | +use OCA\DAV\CardDAV\CardDavBackend; |
| 13 | +use OCA\CloudFederationAPI\Db\FederatedInviteMapper; |
| 14 | +use OCA\Contacts\AppInfo\Application; |
| 15 | +use OCA\Contacts\Service\GroupSharingService; |
| 16 | +use OCA\Contacts\Service\SocialApiService; |
| 17 | +use OCA\FederatedFileSharing\AddressHandler; |
| 18 | +use OCP\App\IAppManager; |
| 19 | +use OCP\AppFramework\Http; |
| 20 | +use OCP\AppFramework\Http\DataResponse; |
| 21 | +use OCP\AppFramework\Http\TemplateResponse; |
| 22 | +use OCP\Contacts\IManager; |
| 23 | +use OCP\Http\Client\IClientService; |
| 24 | +use OCP\IConfig; |
| 25 | +use OCP\IInitialStateService; |
| 26 | +use OCP\IL10N; |
| 27 | +use OCP\IRequest; |
| 28 | +use OCP\IURLGenerator; |
| 29 | +use OCP\IUserManager; |
| 30 | +use OCP\IUserSession; |
| 31 | +use OCP\L10N\IFactory; |
| 32 | +use Psr\Log\LoggerInterface; |
| 33 | + |
| 34 | +/** |
| 35 | + * Controller for federated invites related routes. |
| 36 | + * |
| 37 | + */ |
| 38 | + |
| 39 | +class FederatedInvitesController extends PageController |
| 40 | +{ |
| 41 | + |
| 42 | + public function __construct( |
| 43 | + IRequest $request, |
| 44 | + private AddressHandler $addressHandler, |
| 45 | + private CardDavBackend $cardDavBackend, |
| 46 | + private IClientService $httpClient, |
| 47 | + private IConfig $config, |
| 48 | + private IInitialStateService $initialStateService, |
| 49 | + private IFactory $languageFactory, |
| 50 | + private IManager $contactsManager, |
| 51 | + private IUserSession $userSession, |
| 52 | + private SocialApiService $socialApiService, |
| 53 | + private IAppManager $appManager, |
| 54 | + private CompareVersion $compareVersion, |
| 55 | + private GroupSharingService $groupSharingService, |
| 56 | + private IL10N $il10, |
| 57 | + private IURLGenerator $urlGenerator, |
| 58 | + private IUserManager $userManager, |
| 59 | + private FederatedInviteMapper $federatedInviteMapper, |
| 60 | + private LoggerInterface $logger, |
| 61 | + ) { |
| 62 | + parent::__construct( |
| 63 | + $request, |
| 64 | + $config, |
| 65 | + $initialStateService, |
| 66 | + $languageFactory, |
| 67 | + $userSession, |
| 68 | + $socialApiService, |
| 69 | + $appManager, |
| 70 | + $compareVersion, |
| 71 | + $groupSharingService, |
| 72 | + $logger, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @NoAdminRequired |
| 78 | + * @NoCSRFRequired |
| 79 | + * |
| 80 | + * Sets the token and provider states which triggers display of the invite accept dialog. |
| 81 | + * |
| 82 | + * @param string $token |
| 83 | + * @param string $provider |
| 84 | + */ |
| 85 | + public function inviteAcceptDialog(string $token = "", string $provider = ""): TemplateResponse |
| 86 | + { |
| 87 | + $this->logger->debug(" - FederatedInvitesController inviteAcceptDialog method: setting initial state ($token, $provider) and returning PageController index ", ['app' => Application::APP_ID]); |
| 88 | + $this->initialStateService->provideInitialState(Application::APP_ID, 'inviteToken', $token); |
| 89 | + $this->initialStateService->provideInitialState(Application::APP_ID, 'inviteProvider', $provider); |
| 90 | + // TODO read from config |
| 91 | + $this->initialStateService->provideInitialState(Application::APP_ID, 'acceptInviteDialogUrl', '/ocm/invite-accept-dialog'); |
| 92 | + |
| 93 | + return $this->index(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @NoAdminRequired |
| 98 | + * @NoCSRFRequired |
| 99 | + * |
| 100 | + * Creates an invitation to exchange contact info for the user with the specified uid. |
| 101 | + * |
| 102 | + * @return DataResponse |
| 103 | + */ |
| 104 | + public function createInvite(string $uid = ''): DataResponse |
| 105 | + { |
| 106 | + return new DataResponse(['message' => "Route not implemented"], Http::STATUS_NOT_IMPLEMENTED); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @NoAdminRequired |
| 111 | + * @NoCSRFRequired |
| 112 | + * |
| 113 | + * Accepts the invite and creates a new contact for it. |
| 114 | + * On success the user will be redirected to the contacts list with the newly created contact in focus. |
| 115 | + * |
| 116 | + * @param string $token the token of the invite |
| 117 | + * @param string $provider the provider of the sender of the invite |
| 118 | + * @return DataResponse with data signature ['contact' | 'message'] - the new contact url or a message in case of error |
| 119 | + */ |
| 120 | + public function inviteAccepted(string $token = "", string $provider = ""): DataResponse { |
| 121 | + if ($token === "" || $provider === "") { |
| 122 | + $this->logger->error("Both token and provider must be specified. Received: token=$token, provider=$provider", ['app' => Application::APP_ID]); |
| 123 | + return new DataResponse(['message' => 'Both token and provider must be specified.'], Http::STATUS_NOT_FOUND); |
| 124 | + } |
| 125 | + try { |
| 126 | + // delegate further to OCM /invite-accepted |
| 127 | + // this returns a response with the following data signature: ['userID', 'email', 'name'] |
| 128 | + // @link https://cs3org.github.io/OCM-API/docs.html?branch=v1.1.0&repo=OCM-API&user=cs3org#/paths/~1invite-accepted/post |
| 129 | + $localUser = $this->userSession->getUser(); |
| 130 | + $client = $this->httpClient->newClient(); |
| 131 | + $responseData = null; |
| 132 | + $response = $client->post( |
| 133 | + // TODO get the correct url in an appropriate manner |
| 134 | + "http://nc-ocm.nl/ocm/invite-accepted", |
| 135 | + [ |
| 136 | + 'body' => |
| 137 | + [ |
| 138 | + 'recipientProvider' => $provider, |
| 139 | + 'token' => $token, |
| 140 | + 'userId' => $localUser->getUID(), |
| 141 | + 'email' => $localUser->getEMailAddress(), |
| 142 | + 'name' => $localUser->getDisplayName(), |
| 143 | + ], |
| 144 | + 'connect_timeout' => 10, |
| 145 | + ] |
| 146 | + ); |
| 147 | + $responseData = $response->getBody(); |
| 148 | + $data = json_decode($responseData, true); |
| 149 | + $newContact = $this->socialApiService->createFederatedContact( |
| 150 | + // nextcloud cloud id format, ie. the ocm address |
| 151 | + $data['userID'] . "@" . $this->addressHandler->removeProtocolFromUrl($provider), |
| 152 | + $data['email'], |
| 153 | + $data['name'], |
| 154 | + $localUser->getUID(), |
| 155 | + ); |
| 156 | + if (!isset($newContact)) { |
| 157 | + return new DataResponse(['message' => 'An unexpected error occurred trying to accept invite: could not create new contact'], Http::STATUS_NOT_FOUND); |
| 158 | + } |
| 159 | + $this->logger->info("Created new contact with UID: " . $newContact['UID'] . " for user with UID: " . $localUser->getUID(), ['app' => Application::APP_ID]); |
| 160 | + |
| 161 | + $contact = $newContact['UID'] . "~" . CardDavBackend::PERSONAL_ADDRESSBOOK_URI; |
| 162 | + $url = $this->urlGenerator->getAbsoluteURL( |
| 163 | + $this->urlGenerator->linkToRoute('contacts.page.index') . $this->il10->t('All contacts') . '/' . $contact |
| 164 | + ); |
| 165 | + return new DataResponse(['contact' => $url], Http::STATUS_OK); |
| 166 | + } catch (\GuzzleHttp\Exception\RequestException $e) { |
| 167 | + $this->logger->error("/invite-accepted returned an error: " . print_r($responseData, true), ['app' => Application::APP_ID]); |
| 168 | + /** |
| 169 | + * 400: Invalid or non existing token |
| 170 | + * 409: Invite already accepted |
| 171 | + */ |
| 172 | + $statusCode = $e->getCode(); |
| 173 | + switch ($statusCode) { |
| 174 | + case Http::STATUS_BAD_REQUEST: |
| 175 | + return new DataResponse(['message' => 'Invalid, non existing or expired token'], $e->getCode()); |
| 176 | + case Http::STATUS_CONFLICT: |
| 177 | + return new DataResponse(['message' => 'Invite already accepted'], $e->getCode()); |
| 178 | + } |
| 179 | + $this->logger->error("An unexpected error occurred accepting invite with token=$token and provider=$provider. Stacktrace: " . $e->getTraceAsString(), ['app' => Application::APP_ID]); |
| 180 | + return new DataResponse(['message' => 'An unexpected error occurred trying to accept invite.'], Http::STATUS_NOT_FOUND); |
| 181 | + } catch (Exception $e) { |
| 182 | + $this->logger->error("An unexpected error occurred accepting invite with token=$token and provider=$provider. Stacktrace: " . $e->getTraceAsString(), ['app' => Application::APP_ID]); |
| 183 | + return new DataResponse(['message' => 'An unexpected error occurred trying to accept invite'], Http::STATUS_NOT_FOUND); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments