3 namespace Rapsys\AirBundle\Controller
; 
   5 use Google\Service\Calendar
; 
   6 use Symfony\Component\HttpFoundation\Request
; 
   7 use Symfony\Component\HttpFoundation\Response
; 
   8 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
   9 use Symfony\Component\Cache\Adapter\FilesystemAdapter
; 
  11 #use Rapsys\AirBundle\Entity\Slot; 
  12 #use Rapsys\AirBundle\Entity\Session; 
  13 #use Rapsys\AirBundle\Entity\Location; 
  14 #use Rapsys\AirBundle\Entity\User; 
  15 #use Rapsys\AirBundle\Entity\Snippet; 
  17 class CalendarController 
extends DefaultController 
{ 
  19          * Calendar authorization 
  21          * @desc Initiate calendar oauth process 
  23          * @param Request $request The request instance 
  25          * @return Response The rendered view 
  27         public function index(Request 
$request): Response 
{ 
  28                 //Prevent non-admin to access here 
  29                 $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Admin')])); 
  32                 #$doctrine = $this->getDoctrine(); 
  35                 $section = $this->translator
->trans('Calendar oauth form'); 
  38                 $this->context
['description'] = $this->translator
->trans('Initiate calendar oauth process'); 
  41                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
  43                 //Create the form according to the FormType created previously. 
  44                 //And give the proper parameters 
  45                 $form = $this->createForm('Rapsys\AirBundle\Form\CalendarType', ['calendar' => $this->config
['calendar']['calendar'], 'prefix' => $this->config
['calendar']['prefix']], [ 
  46                         'action' => $this->generateUrl('rapsys_air_calendar'), 
  50                 if ($request->isMethod('POST')) { 
  51                         // Refill the fields in case the form is not valid. 
  52                         $form->handleRequest($request); 
  54                         if ($form->isValid()) { 
  56                                 $data = $form->getData(); 
  58                                 //When empty use config project 
  59                                 $data['project'] = $data['project']?:$this->config
['calendar']['project']; 
  61                                 //When empty use config client 
  62                                 $data['client'] = $data['client']?:$this->config
['calendar']['client']; 
  64                                 //When empty use config secret 
  65                                 $data['secret'] = $data['secret']?:$this->config
['calendar']['secret']; 
  68                                 $googleClient = new \Google\
Client( 
  70                                                 'application_name' => $data['project'], 
  71                                                 'client_id' => $data['client'], 
  72                                                 'client_secret' => $data['secret'], 
  73                                                 'redirect_uri' => $redirect = $this->generateUrl('rapsys_air_calendar_callback', [], UrlGeneratorInterface
::ABSOLUTE_URL
), 
  74                                                 'scopes' => [Calendar
::CALENDAR
, Calendar
::CALENDAR_EVENTS
], 
  75                                                 'access_type' => 'offline', 
  76                                                 'approval_prompt' => 'force' 
  80 #                               //Set application name 
  81 #                               $googleClient->setApplicationName($data['project']); 
  84 #                               $googleClient->setClientId($data['client']); 
  87 #                               $googleClient->setClientSecret($data['secret']); 
  89 #                               //Add calendar scope 
  90 #                               //XXX: required to create the airlibre calendar ? 
  91 #                               $googleClient->addScope(Calendar::CALENDAR); 
  92 #                               //Add calendar events scope 
  93 #                               $googleClient->addScope(Calendar::CALENDAR_EVENTS); 
  96 #                               $googleClient->setRedirectUri($redirect = $this->generateUrl('rapsys_air_calendar_callback', [], UrlGeneratorInterface::ABSOLUTE_URL)); 
  98 #                               //Set offline access 
  99 #                               $googleClient->setAccessType('offline'); 
 101 #                               //Set included scopes 
 102 #                               //TODO: remove that useless of check scopes in callback 
 103 #                               $googleClient->setIncludeGrantedScopes(true); 
 106                                 #$googleClient->setLoginHint('rapsys.eu@gmail.com'); 
 109                                 //TODO: force refresh token creation with approval prompt 
 110                                 #$googleClient->setApprovalPrompt('consent'); 
 113                                 $authUrl = $googleClient->createAuthUrl(); 
 116                                 $session = $request->getSession(); 
 118                                 //Store calendar, prefix, project, client and secret in session 
 119                                 $session->set('calendar.calendar', $data['calendar']); 
 120                                 $session->set('calendar.prefix', $data['prefix']); 
 121                                 $session->set('calendar.project', $data['project']); 
 122                                 $session->set('calendar.client', $data['client']); 
 123                                 $session->set('calendar.secret', $data['secret']); 
 124                                 $session->set('calendar.redirect', $redirect); 
 126                                 //Redirect externally 
 127                                 return $this->redirect($authUrl); 
 132                 return $this->render('@RapsysAir/calendar/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
 136          * List all sessions for the organizer 
 138          * @desc Display all sessions for the user with an application or login form 
 140          * @param Request $request The request instance 
 142          * @return Response The rendered view 
 144         public function callback(Request 
$request) { 
 146                 $section = $this->translator
->trans('Calendar callback'); 
 149                 $this->context
['description'] = $this->translator
->trans('Finish calendar oauth process'); 
 152                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 155                 if (!empty($code = $request->get('code'))) { 
 157                         $session = $request->getSession(); 
 160                         $calendar = $session->get('calendar.calendar'); 
 163                         $prefix = $session->get('calendar.prefix'); 
 166                         $project = $session->get('calendar.project'); 
 169                         $client = $session->get('calendar.client'); 
 172                         $secret = $session->get('calendar.secret'); 
 175                         $redirect = $session->get('calendar.redirect'); 
 178                         #$googleClient = new \Google\Client(['application_name' => $project, 'client_id' => $client, 'client_secret' => $secret, 'redirect_uri' => $redirect]); 
 179                         $googleClient = new \Google\
Client( 
 181                                         'application_name' => $project, 
 182                                         'client_id' => $client, 
 183                                         'client_secret' => $secret, 
 184                                         'redirect_uri' => $redirect, 
 185                                         'scopes' => [Calendar
::CALENDAR
, Calendar
::CALENDAR_EVENTS
], 
 186                                         'access_type' => 'offline', 
 187                                         'approval_prompt' => 'force' 
 191                         //Authenticate with code 
 192                         if (!empty($token = $googleClient->authenticate($code))) { 
 194                                 if (!empty($token['error'])) { 
 195                                         $this->context
['error'] = $this->translator
->trans(ucfirst(str_replace('_', ' ', $token['error']))); 
 196                                 //Without refresh token 
 197                                 } elseif (empty($token['refresh_token'])) { 
 198                                         $this->context
['error'] = $this->translator
->trans('Missing refresh token'); 
 201                                         //Retrieve cache object 
 202                                         $cache = new FilesystemAdapter($this->config
['cache']['namespace'], $this->config
['cache']['lifetime'], $this->config
['path']['cache']); 
 205                                         $cacheCalendars = $cache->getItem('calendars'); 
 211                                         if ($cacheCalendars->isHit()) { 
 213                                                 $calendars = $cacheCalendars->get(); 
 217                                         if (empty($calendars[$client])) { 
 219                                                 $calendars[$client] = [ 
 220                                                         'project' => $project, 
 222                                                         'redirect' => $redirect, 
 228                                         $calendars[$client]['tokens'][$token['access_token']] = [ 
 229                                                 'calendar' => $calendar, 
 231                                                 'refresh' => $token['refresh_token'], 
 232                                                 'expire' => $token['expires_in'], 
 233                                                 'scope' => $token['scope'], 
 234                                                 'type' => $token['token_type'], 
 235                                                 'created' => $token['created'] 
 239                                         $cacheCalendars->set($calendars); 
 242                                         $cache->save($cacheCalendars); 
 245                                         $this->context
['message'] = $this->translator
->trans('Token stored for project '.$project); 
 247                         //With failed authenticate 
 249                                 $this->context
['error'] = $this->translator
->trans('Client authenticate failed'); 
 252                 } elseif (!empty($error = $request->get('error'))) { 
 253                         $this->context
['error'] = $this->translator
->trans(ucfirst(str_replace('_', ' ', $error))); 
 257                 return $this->render('@RapsysAir/calendar/callback.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);