mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
[wip] first impl for getting authorization for admin
This commit is contained in:
parent
a7ec843509
commit
5331f1becc
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Command;
|
||||
|
||||
use Chill\CalendarBundle\Synchro\Connector\MSGraph\MachineTokenStorage;
|
||||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use TheNetworg\OAuth2\Client\Provider\Azure;
|
||||
use const PHP_URL_QUERY;
|
||||
|
||||
class AzureGetMachineAccessTokenCommand extends Command
|
||||
{
|
||||
private Azure $azure;
|
||||
|
||||
private ClientRegistry $clientRegistry;
|
||||
|
||||
private MachineTokenStorage $machineTokenStorage;
|
||||
|
||||
public function __construct(Azure $azure, ClientRegistry $clientRegistry, MachineTokenStorage $machineTokenStorage)
|
||||
{
|
||||
parent::__construct('chill:calendar:get-access-token');
|
||||
|
||||
$this->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;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Controller;
|
||||
|
||||
use Chill\CalendarBundle\Synchro\Connector\RemoteCalendarConnectorInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
/**
|
||||
* Contains method to get events (Calendar) from remote calendar.
|
||||
*/
|
||||
class RemoteCalendarProxyController
|
||||
{
|
||||
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
|
||||
|
||||
public function listEventForCalendar(User $user, Request $request): Response
|
||||
{
|
||||
if ($request->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');
|
||||
}
|
||||
}
|
||||
}
|
@ -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']
|
||||
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Synchro\Connector\MSGraph;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use TheNetworg\OAuth2\Client\Provider\Azure;
|
||||
|
||||
class MSGraphClient
|
||||
{
|
||||
private Azure $provider;
|
||||
|
||||
private MSGraphTokenStorage $tokenStorage;
|
||||
|
||||
/**
|
||||
* @param mixed $calendar
|
||||
*
|
||||
* @return array as a json response
|
||||
*/
|
||||
public function listEventsForUserCalendar($calendar, DateTimeImmutable $startDate, DateTimeImmutable $endDate, ?int $start = null, ?int $limit = null): array
|
||||
{
|
||||
$from = $startDate->format(DateTimeImmutable::ATOM);
|
||||
$to = $endDate->format(DateTimeImmutable::ATOM);
|
||||
|
||||
$response = $this->provider->getObjects("{$calendar}/calendar/calendarView?startDateTime={$from}&endDateTime={$end}");
|
||||
|
||||
dump($response);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Synchro\Connector\MSGraph;
|
||||
|
||||
use Chill\MainBundle\Redis\ChillRedis;
|
||||
use TheNetworg\OAuth2\Client\Token\AccessToken;
|
||||
|
||||
class MachineTokenStorage
|
||||
{
|
||||
private const KEY = 'msgraph_access_token';
|
||||
|
||||
private ChillRedis $chillRedis;
|
||||
|
||||
public function __construct(ChillRedis $chillRedis)
|
||||
{
|
||||
$this->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));
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 [];
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
33
src/Bundle/ChillCalendarBundle/Synchro/Model/RemoteEvent.php
Normal file
33
src/Bundle/ChillCalendarBundle/Synchro/Model/RemoteEvent.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\Synchro\Model;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class RemoteEvent
|
||||
{
|
||||
public string $description;
|
||||
|
||||
public DateTimeImmutable $endDate;
|
||||
|
||||
public DateTimeImmutable $startDate;
|
||||
|
||||
public string $title;
|
||||
|
||||
public function __construct(string $title, string $description, DateTimeImmutable $startDate, DateTimeImmutable $endDate)
|
||||
{
|
||||
$this->title = $title;
|
||||
$this->description = $description;
|
||||
$this->startDate = $startDate;
|
||||
$this->endDate = $endDate;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user