|
| 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 OC\DB\Connection; |
| 11 | +use OCA\Circles\Db\CircleRequest; |
| 12 | +use OCA\Circles\Db\MembershipRequest; |
| 13 | +use OCA\Circles\Exceptions\FrontendException; |
| 14 | +use OCA\Circles\Exceptions\MembershipNotFoundException; |
| 15 | +use OCA\Circles\FederatedItems\CircleJoin; |
| 16 | +use OCA\Circles\Model\Federated\FederatedEvent; |
| 17 | +use OCA\Circles\Service\ConfigService; |
| 18 | +use OCA\Circles\Service\FederatedUserService; |
| 19 | +use OCA\Circles\Tools\Traits\TDeserialize; |
| 20 | +use OCA\Circles\Tools\Traits\TNCLogger; |
| 21 | +use OCA\Contacts\AppInfo\Application; |
| 22 | +use OCP\AppFramework\ApiController; |
| 23 | +use OCP\AppFramework\Http\Attribute\UserRateLimit; |
| 24 | +use OCP\AppFramework\Http\DataResponse; |
| 25 | +use OCP\AppFramework\Http\TemplateResponse; |
| 26 | +use OCP\AppFramework\OCS\OCSException; |
| 27 | +use OCP\AppFramework\Services\IInitialState; |
| 28 | +use OCP\IL10N; |
| 29 | +use OCP\IRequest; |
| 30 | +use OCP\IUserSession; |
| 31 | + |
| 32 | +class InvitationController extends ApiController { |
| 33 | + private const SETTING_KEY_INVITATION_CODE = 'invitationCode'; |
| 34 | + |
| 35 | + use TDeserialize; |
| 36 | + use TNCLogger; |
| 37 | + |
| 38 | + public function __construct( |
| 39 | + IRequest $request, |
| 40 | + private IUserSession $userSession, |
| 41 | + private ConfigService $configService, |
| 42 | + private FederatedUserService $federatedUserService, |
| 43 | + private Connection $connection, |
| 44 | + private IInitialState $initialState, |
| 45 | + private CircleRequest $circleRequest, |
| 46 | + private MembershipRequest $membershipRequest, |
| 47 | + private CircleJoin $circleJoin, |
| 48 | + private IL10N $l10n, |
| 49 | + ) { |
| 50 | + parent::__construct(Application::APP_ID, $request); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @NoAdminRequired |
| 55 | + * @NoCSRFRequired |
| 56 | + * |
| 57 | + * @UserRateThrottle(limit=10, period=3600) |
| 58 | + */ |
| 59 | + #[UserRateLimit(limit: 10, period: 3600)] |
| 60 | + public function viewInvitation(string $invitationCode): TemplateResponse { |
| 61 | + $this->setCurrentFederatedUser(); |
| 62 | + |
| 63 | + try { |
| 64 | + $invitation = $this->getInvitation($invitationCode); |
| 65 | + } catch (\OutOfBoundsException $e) { |
| 66 | + return new TemplateResponse(Application::APP_ID, |
| 67 | + 'message', |
| 68 | + ['message' => $this->l10n->t('Link or team does not exist anymore')], |
| 69 | + TemplateResponse::RENDER_AS_USER, |
| 70 | + 404, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + $circle = $this->circleRequest->getCircle($invitation['circle_id']); |
| 75 | + $federatedUser = $this->federatedUserService->getLocalFederatedUser($this->userSession->getUser()->getUID()); |
| 76 | + |
| 77 | + try { |
| 78 | + $this->membershipRequest->getMembership($invitation['circle_id'], $federatedUser->getSingleId()); |
| 79 | + $isAlreadyMemberOfCircle = true; |
| 80 | + } catch (MembershipNotFoundException) { |
| 81 | + $isAlreadyMemberOfCircle = false; |
| 82 | + } |
| 83 | + |
| 84 | + $this->initialState->provideInitialState('circleIdToJoin', $invitation['circle_id']); |
| 85 | + $this->initialState->provideInitialState('circleNameToJoin', $circle->getName()); |
| 86 | + $this->initialState->provideInitialState('invitationCode', $invitationCode); |
| 87 | + $this->initialState->provideInitialState('isAlreadyMemberOfCircle', $isAlreadyMemberOfCircle); |
| 88 | + |
| 89 | + return new TemplateResponse(Application::APP_ID, 'join'); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @NoAdminRequired |
| 94 | + * @NoCSRFRequired |
| 95 | + * |
| 96 | + * @UserRateThrottle(limit=10, period=3600) |
| 97 | + */ |
| 98 | + #[UserRateLimit(limit: 10, period: 3600)] |
| 99 | + public function acceptInvitation(string $invitationCode): DataResponse { |
| 100 | + $joined = false; |
| 101 | + try { |
| 102 | + $invitation = $this->getInvitation($invitationCode); |
| 103 | + |
| 104 | + $circle = $this->circleRequest->getCircle($invitation['circle_id']); |
| 105 | + $userId = $this->userSession->getUser()->getUID(); |
| 106 | + $federatedInvitedUser = $this->federatedUserService->getLocalFederatedUser($userId); |
| 107 | + $federatedInvitedMember = $this->federatedUserService->getFederatedMember($userId); |
| 108 | + |
| 109 | + try { |
| 110 | + $this->membershipRequest->getMembership($invitation['circle_id'], $federatedInvitedUser->getSingleId()); |
| 111 | + } catch (MembershipNotFoundException) { |
| 112 | + $event = new FederatedEvent(CircleJoin::class); |
| 113 | + $event->setCircle($circle); |
| 114 | + $federatedInvitedMember->setInvitedBy($federatedInvitedUser); |
| 115 | + $circle->setInitiator($federatedInvitedMember); |
| 116 | + |
| 117 | + // fixme: implement |
| 118 | + $this->circleJoin->lightJoin($event); |
| 119 | + |
| 120 | + $joined = true; |
| 121 | + } |
| 122 | + |
| 123 | + return new DataResponse(['joined' => $joined]); |
| 124 | + } catch (Exception $e) { |
| 125 | + $this->e($e, ['circleId' => $invitation]); |
| 126 | + throw new OCSException($e->getMessage(), (int)$e->getCode()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private function setCurrentFederatedUser(): void { |
| 131 | + if (!$this->configService->getAppValueBool(ConfigService::FRONTEND_ENABLED)) { |
| 132 | + throw new FrontendException('frontend disabled'); |
| 133 | + } |
| 134 | + |
| 135 | + $user = $this->userSession->getUser(); |
| 136 | + $this->federatedUserService->setLocalCurrentUser($user); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param string $invitationCode |
| 141 | + * @return array{circle_id: string, created_by: string} |
| 142 | + */ |
| 143 | + private function getInvitation(string $invitationCode): array { |
| 144 | + $invitationCode = str_replace('-', '', $invitationCode); |
| 145 | + |
| 146 | + $qb = $this->connection->getQueryBuilder(); |
| 147 | + $row = $qb->select('circle_id', 'created_by') |
| 148 | + ->from('contacts_circle_invitations') |
| 149 | + ->where($qb->expr()->eq('invitation_code', $qb->createNamedParameter($invitationCode))) |
| 150 | + ->executeQuery() |
| 151 | + ->fetch(); |
| 152 | + |
| 153 | + if (empty($row)) { |
| 154 | + throw new \OutOfBoundsException('Invitation code not found'); |
| 155 | + } |
| 156 | + |
| 157 | + return $row; |
| 158 | + } |
| 159 | +} |
0 commit comments