From 38d828cf36755a0ca8e53626c26c44e9bd23e0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 9 May 2022 16:05:10 +0200 Subject: [PATCH] refactor access to calendar and use real userid --- .../RemoteCalendarProxyController.php | 4 +- .../Connector/MSGraph/MapCalendarToUser.php | 44 +++++++++--- .../MSGraphRemoteCalendarConnector.php | 68 +++++++++++-------- 3 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php b/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php index a0552391a..5c5ad5978 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php @@ -42,7 +42,7 @@ class RemoteCalendarProxyController public function listEventForCalendar(User $user, Request $request): Response { if ($request->query->has('startDate')) { - $startDate = DateTimeImmutable::createFromFormat('Y-m-d', $request->query->get('startDate')); + $startDate = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s', $request->query->get('startDate') . 'T00:00:00'); if (false === $startDate) { throw new BadRequestHttpException('startDate on bad format'); @@ -52,7 +52,7 @@ class RemoteCalendarProxyController } if ($request->query->has('endDate')) { - $endDate = DateTimeImmutable::createFromFormat('Y-m-d', $request->query->get('endDate')); + $endDate = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s', $request->query->get('endDate') . 'T23:59:59'); if (false === $endDate) { throw new BadRequestHttpException('endDate on bad format'); diff --git a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraph/MapCalendarToUser.php b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraph/MapCalendarToUser.php index 79589d28b..ff14e1d49 100644 --- a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraph/MapCalendarToUser.php +++ b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraph/MapCalendarToUser.php @@ -11,7 +11,6 @@ declare(strict_types=1); namespace Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph; -use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraphRemoteCalendarConnector; use Chill\MainBundle\Entity\User; use Psr\Log\LoggerInterface; @@ -24,32 +23,61 @@ class MapCalendarToUser private LoggerInterface $logger; - private MSGraphRemoteCalendarConnector $remoteCalendarConnector; + private MachineHttpClient $machineHttpClient; - public function __construct(MSGraphRemoteCalendarConnector $remoteCalendarConnector, LoggerInterface $logger) - { - $this->remoteCalendarConnector = $remoteCalendarConnector; + public function __construct( + MachineHttpClient $machineHttpClient, + LoggerInterface $logger + ) { + $this->machineHttpClient = $machineHttpClient; $this->logger = $logger; } public function getCalendarId(User $user): ?string { - if (null === $mskey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) { + if (null === $msKey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) { return null; } return $msKey['defaultCalendarId'] ?? null; } + public function getDefaultUserCalendar(string $idOrUserPrincipalName): ?array + { + $value = $this->machineHttpClient->request('GET', "users/{$idOrUserPrincipalName}/calendars", [ + 'query' => ['$filter' => 'isDefaultCalendar eq true'], + ])->toArray()['value']; + + return $value[0] ?? null; + } + + public function getUserByEmail(string $email): ?array + { + $value = $this->machineHttpClient->request('GET', 'users', [ + 'query' => ['$filter' => "mail eq '{$email}'"], + ])->toArray()['value']; + + return $value[0] ?? null; + } + + public function getUserId(User $user): ?string + { + if (null === $msKey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) { + return null; + } + + return $msKey['id'] ?? null; + } + public function writeMetadata(User $user): User { - if (null === $userData = $this->remoteCalendarConnector->getUserByEmail($user->getEmailCanonical())) { + if (null === $userData = $this->getUserByEmail($user->getEmailCanonical())) { $this->logger->warning('[MapCalendarToUser] could find user on msgraph', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]); return $this->writeNullData($user); } - if (null === $defaultCalendar = $this->remoteCalendarConnector->getDefaultUserCalendar($userData['id'])) { + if (null === $defaultCalendar = $this->getDefaultUserCalendar($userData['id'])) { $this->logger->warning('[MapCalendarToUser] could find default calendar', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]); return $this->writeNullData($user); diff --git a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php index 613dd7dc0..b9df6be27 100644 --- a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php +++ b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php @@ -12,19 +12,26 @@ declare(strict_types=1); namespace Chill\CalendarBundle\RemoteCalendar\Connector; use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineHttpClient; +use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser; use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserHttpClient; use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserTokenStorage; use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter; use Chill\MainBundle\Entity\User; use DateTimeImmutable; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface { + private LoggerInterface $logger; + private MachineHttpClient $machineHttpClient; + private MapCalendarToUser $mapCalendarToUser; + private RemoteEventConverter $remoteEventConverter; private OnBehalfOfUserTokenStorage $tokenStorage; @@ -35,42 +42,28 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface public function __construct( MachineHttpClient $machineHttpClient, + MapCalendarToUser $mapCalendarToUser, + LoggerInterface $logger, OnBehalfOfUserTokenStorage $tokenStorage, OnBehalfOfUserHttpClient $userHttpClient, RemoteEventConverter $remoteEventConverter, UrlGeneratorInterface $urlGenerator ) { $this->machineHttpClient = $machineHttpClient; + $this->mapCalendarToUser = $mapCalendarToUser; + $this->logger = $logger; $this->remoteEventConverter = $remoteEventConverter; $this->tokenStorage = $tokenStorage; $this->urlGenerator = $urlGenerator; $this->userHttpClient = $userHttpClient; } - public function getDefaultUserCalendar(string $idOrUserPrincipalName): ?array - { - $value = $this->machineHttpClient->request('GET', "users/{$idOrUserPrincipalName}/calendars", [ - 'query' => ['$filter' => 'isDefaultCalendar eq true'], - ])->toArray()['value']; - - return $value[0] ?? null; - } - public function getMakeReadyResponse(string $returnPath): Response { return new RedirectResponse($this->urlGenerator ->generate('chill_calendar_remote_connect_azure', ['returnPath' => $returnPath])); } - public function getUserByEmail(string $email): ?array - { - $value = $this->machineHttpClient->request('GET', 'users', [ - 'query' => ['$filter' => "mail eq '{$email}'"], - ])->toArray()['value']; - - return $value[0] ?? null; - } - public function isReady(): bool { return $this->tokenStorage->hasToken(); @@ -78,18 +71,33 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array { - $bareEvents = $this->userHttpClient->request( - 'GET', - 'users/c4f1fcc7-10e4-4ea9-89ac-c89a00e0a51a/calendarView', - [ - 'query' => [ - 'startDateTime' => $startDate->format(DateTimeImmutable::ATOM), - 'endDateTime' => $endDate->format(DateTimeImmutable::ATOM), - '$select' => 'id,subject,start,end', - ], - ] - )->toArray(); + $userId = $this->mapCalendarToUser->getUserId($user); - return array_map(function ($item) { return $this->remoteEventConverter->convertToRemote($item); }, $bareEvents['value']); + if (null === $userId) { + return []; + } + + try { + $bareEvents = $this->userHttpClient->request( + 'GET', + 'users/' . $userId . '/calendarView', + [ + 'query' => [ + 'startDateTime' => $startDate->format(DateTimeImmutable::ATOM), + 'endDateTime' => $endDate->format(DateTimeImmutable::ATOM), + '$select' => 'id,subject,start,end', + ], + ] + )->toArray(); + + return array_map(function ($item) { return $this->remoteEventConverter->convertToRemote($item); }, $bareEvents['value']); + } catch (ClientExceptionInterface $e) { + $this->logger->debug('Could not get list of event on MSGraph', [ + 'error_code' => $e->getResponse()->getStatusCode(), + 'error' => $e->getResponse()->getInfo(), + ]); + + return []; + } } }