diff --git a/src/Bundle/ChillCalendarBundle/Command/AzureGetMachineAccessTokenCommand.php b/src/Bundle/ChillCalendarBundle/Command/AzureGetMachineAccessTokenCommand.php new file mode 100644 index 000000000..f59f188f8 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Command/AzureGetMachineAccessTokenCommand.php @@ -0,0 +1,85 @@ +azure = $azure; + $this->clientRegistry = $clientRegistry; + $this->machineTokenStorage = $machineTokenStorage; + } + + protected function configure() + { + $this + ->addOption('tenant', 't', InputOption::VALUE_OPTIONAL, 'the tenant, usually the application name', 'common'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->azure->tenant = $input->getOption('tenant'); + $this->azure->scope = ['https://graph.microsoft.com/.default']; + $authorizationUrl = explode('?', $this->azure->getAuthorizationUrl(['prompt' => 'consent'])); + // replace the first part by the admin consent authorization url + $authorizationUrl[0] = strtr('https://login.microsoftonline.com/{tenant}/adminconsent', ['{tenant}' => $this->azure->tenant]); + + $output->writeln('Go to the url'); + $output->writeln(implode('?', $authorizationUrl)); + $output->writeln('Authenticate as admin, and copy-paste the url you will reach'); + + // not necessary ? + $helper = $this->getHelper('question'); + $question = new Question('Paste here the return url after you completed the admin consent'); + + $returnUrl = $helper->ask($input, $output, $question); + + $keyValues = explode('&', parse_url($returnUrl, PHP_URL_QUERY)); + $params = []; + + foreach ($keyValues as $str) { + $strs = explode('=', $str); + $params[$strs[0]] = $strs[1]; + } + + dump($params); + + $token = $this->azure->getAccessToken('client_credentials', [ + 'scope' => $this->azure->scope, + ]); + + $this->machineTokenStorage->storeToken($token); + + $output->writeln('machine access token acquired and saved!'); + + return 0; + } +} diff --git a/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php b/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php new file mode 100644 index 000000000..2cf0daa50 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Controller/RemoteCalendarProxyController.php @@ -0,0 +1,41 @@ +query->has('startDate')) { + $startDate = DateTimeImmutable::createFromFormat('Y-m-dTHis', $request->query->get('startDate') . '000000'); + } else { + throw new BadRequestHttpException('startDate not provided'); + } + + if ($request->query->has('endDate')) { + $startDate = DateTimeImmutable::createFromFormat('Y-m-dTHis', $request->query->get('endDate') . '000000'); + } else { + throw new BadRequestHttpException('endDate not provided'); + } + } +} diff --git a/src/Bundle/ChillCalendarBundle/Resources/config/services.yml b/src/Bundle/ChillCalendarBundle/Resources/config/services.yml index c41c2ad93..fde32b70b 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/config/services.yml +++ b/src/Bundle/ChillCalendarBundle/Resources/config/services.yml @@ -12,3 +12,15 @@ services: autoconfigure: true resource: '../../Menu/' tags: ['chill.menu_builder'] + + Chill\CalendarBundle\Command\: + autowire: true + autoconfigure: true + resource: '../../Command/' + + Chill\CalendarBundle\Command\AzureGetMachineAccessTokenCommand: + autoconfigure: true + autowire: true + arguments: + $azure: '@knpu.oauth2.provider.azure' + tags: ['console.command'] diff --git a/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraph/MSGraphClient.php b/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraph/MSGraphClient.php new file mode 100644 index 000000000..4391435ce --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraph/MSGraphClient.php @@ -0,0 +1,39 @@ +format(DateTimeImmutable::ATOM); + $to = $endDate->format(DateTimeImmutable::ATOM); + + $response = $this->provider->getObjects("{$calendar}/calendar/calendarView?startDateTime={$from}&endDateTime={$end}"); + + dump($response); + + return $response; + } +} diff --git a/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraph/MachineTokenStorage.php b/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraph/MachineTokenStorage.php new file mode 100644 index 000000000..89166874d --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraph/MachineTokenStorage.php @@ -0,0 +1,37 @@ +chillRedis = $chillRedis; + } + + public function getToken(): AccessToken + { + return unserialize($this->chillRedis->get(self::KEY)); + } + + public function storeToken(AccessToken $token): void + { + $this->chillRedis->set(self::KEY, serialize($token)); + } +} diff --git a/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraphRemoteCalendarConnector.php b/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraphRemoteCalendarConnector.php index df6e21f8e..7cdf37205 100644 --- a/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraphRemoteCalendarConnector.php +++ b/src/Bundle/ChillCalendarBundle/Synchro/Connector/MSGraphRemoteCalendarConnector.php @@ -11,22 +11,29 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Synchro\Connector; +use Chill\CalendarBundle\Synchro\Connector\MSGraph\MSGraphClient; use Chill\CalendarBundle\Synchro\Connector\MSGraph\MSGraphTokenStorage; +use Chill\MainBundle\Entity\User; +use DateTimeImmutable; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface { - private MSGraphTokenStorage $MSGraphTokenStorage; + private MSGraphClient $client; + + private MSGraphTokenStorage $tokenStorage; private UrlGeneratorInterface $urlGenerator; public function __construct( - MSGraphTokenStorage $MSGraphTokenStorage, + MSGraphClient $client, + MSGraphTokenStorage $tokenStorage, UrlGeneratorInterface $urlGenerator ) { - $this->MSGraphTokenStorage = $MSGraphTokenStorage; + $this->client = $client; + $this->tokenStorage = $tokenStorage; $this->urlGenerator = $urlGenerator; } @@ -38,6 +45,11 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface public function isReady(): bool { - return $this->MSGraphTokenStorage->hasToken(); + return $this->tokenStorage->hasToken(); + } + + public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array + { + return $this->client->listEventsForUserCalendar($user->getEmail(), $startDate, $endDate); } } diff --git a/src/Bundle/ChillCalendarBundle/Synchro/Connector/NullRemoteCalendarConnector.php b/src/Bundle/ChillCalendarBundle/Synchro/Connector/NullRemoteCalendarConnector.php index 9b64889c0..40e53b612 100644 --- a/src/Bundle/ChillCalendarBundle/Synchro/Connector/NullRemoteCalendarConnector.php +++ b/src/Bundle/ChillCalendarBundle/Synchro/Connector/NullRemoteCalendarConnector.php @@ -11,6 +11,8 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Synchro\Connector; +use Chill\MainBundle\Entity\User; +use DateTimeImmutable; use LogicException; use Symfony\Component\HttpFoundation\Response; @@ -25,4 +27,9 @@ class NullRemoteCalendarConnector implements RemoteCalendarConnectorInterface { return true; } + + public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array + { + return []; + } } diff --git a/src/Bundle/ChillCalendarBundle/Synchro/Connector/RemoteCalendarConnectorInterface.php b/src/Bundle/ChillCalendarBundle/Synchro/Connector/RemoteCalendarConnectorInterface.php index b105fc81f..998d945f2 100644 --- a/src/Bundle/ChillCalendarBundle/Synchro/Connector/RemoteCalendarConnectorInterface.php +++ b/src/Bundle/ChillCalendarBundle/Synchro/Connector/RemoteCalendarConnectorInterface.php @@ -11,6 +11,9 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Synchro\Connector; +use Chill\CalendarBundle\Synchro\Model\RemoteEvent; +use Chill\MainBundle\Entity\User; +use DateTimeImmutable; use Symfony\Component\HttpFoundation\Response; interface RemoteCalendarConnectorInterface @@ -27,4 +30,9 @@ interface RemoteCalendarConnectorInterface * remote calendars. */ public function isReady(): bool; + + /** + * @return array|RemoteEvent[] + */ + public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array; } diff --git a/src/Bundle/ChillCalendarBundle/Synchro/Model/RemoteEvent.php b/src/Bundle/ChillCalendarBundle/Synchro/Model/RemoteEvent.php new file mode 100644 index 000000000..91305a9d6 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Synchro/Model/RemoteEvent.php @@ -0,0 +1,33 @@ +title = $title; + $this->description = $description; + $this->startDate = $startDate; + $this->endDate = $endDate; + } +}