]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Controller/UserController.php
Register google client service and alias
[airbundle] / Controller / UserController.php
index 510ae33e66065abe36ddbdad8bd9dfea282a7a43..2e30fdc304884e9900db04d05e49658499686272 100644 (file)
@@ -157,10 +157,10 @@ class UserController extends BaseUserController {
                        //Get google client
                        $googleClient = new \Google\Client(
                                [
                        //Get google client
                        $googleClient = new \Google\Client(
                                [
-                                       'application_name' => $request->server->get('GOOGLE_PROJECT'),
-                                       'client_id' => $request->server->get('GOOGLE_CLIENT'),
-                                       'client_secret' => $request->server->get('GOOGLE_SECRET'),
-                                       'redirect_uri' => $this->generateUrl('rapsys_air_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
+                                       'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'],
+                                       'client_id' => $_ENV['GOOGLE_CLIENT_ID'],
+                                       'client_secret' => $_ENV['GOOGLE_CLIENT_SECRET'],
+                                       'redirect_uri' => $this->generateUrl('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
                                        'scopes' => self::googleScopes,
                                        'access_type' => 'offline',
                                        'login_hint' => $user->getMail(),
                                        'scopes' => self::googleScopes,
                                        'access_type' => 'offline',
                                        'login_hint' => $user->getMail(),
@@ -218,12 +218,39 @@ class UserController extends BaseUserController {
                                                                )
                                                        );
 
                                                                )
                                                        );
 
-                                                       //Remove user token
+                                                       //Set calendar mails
+                                                       $cmails = [];
+
+                                                       //Iterate on each google token calendars
+                                                       foreach($googleToken->getGoogleCalendars() as $googleCalendar) {
+                                                               //Add calendar mail
+                                                               $cmails[] = $googleCalendar->getMail();
+
+                                                               //Remove google token calendar
+                                                               $this->manager->remove($googleCalendar);
+                                                       }
+
+                                                       //Log unlinked google token infos
+                                                       $this->logger->emergency(
+                                                               $this->translator->trans(
+                                                                       'expired: mail=%mail% gmail=%gmail% cmails=%cmails% locale=%locale%',
+                                                                       [
+                                                                               '%mail%' => $googleToken->getUser()->getMail(),
+                                                                               '%gmail%' => $googleToken->getMail(),
+                                                                               '%cmails' => implode(',', $cmails),
+                                                                               '%locale%' => $request->getLocale()
+                                                                       ]
+                                                               )
+                                                       );
+
+                                                       //Remove google token
                                                        $this->manager->remove($googleToken);
 
                                                        //Flush to delete it
                                                        $this->manager->flush();
 
                                                        $this->manager->remove($googleToken);
 
                                                        //Flush to delete it
                                                        $this->manager->flush();
 
+                                                       //TODO: warn user by mail ?
+
                                                        //Skip to next token
                                                        continue;
                                                }
                                                        //Skip to next token
                                                        continue;
                                                }
@@ -232,26 +259,26 @@ class UserController extends BaseUserController {
                                        //XXX: TODO: remove DEBUG
                                        #$this->cache->delete('user.edit.calendar.'.$this->slugger->short($googleToken->getMail()));
 
                                        //XXX: TODO: remove DEBUG
                                        #$this->cache->delete('user.edit.calendar.'.$this->slugger->short($googleToken->getMail()));
 
-                                       //Get calendars
-                                       $calendars = $this->cache->get(
-                                               //Set key to user.edit.$mail
-                                               ($calendarKey = 'user.edit.calendar.'.($googleShortMail = $this->slugger->short($googleMail = $googleToken->getMail()))),
-                                               //Fetch mail calendar list
-                                               function (ItemInterface $item) use ($googleClient): array {
-                                                       //Expire after 1h
-                                                       $item->expiresAfter(3600);
+                                       //Retrieve calendar
+                                       try {
+                                               //Get calendars
+                                               $calendars = $this->cache->get(
+                                                       //Set key to user.edit.$mail
+                                                       ($calendarKey = 'user.edit.calendar.'.($googleShortMail = $this->slugger->short($googleMail = $googleToken->getMail()))),
+                                                       //Fetch mail calendar list
+                                                       function (ItemInterface $item) use ($googleClient): array {
+                                                               //Expire after 1h
+                                                               $item->expiresAfter(3600);
 
 
-                                                       //Get google calendar service
-                                                       $service = new \Google\Service\Calendar($googleClient);
+                                                               //Get google calendar service
+                                                               $service = new \Google\Service\Calendar($googleClient);
 
 
-                                                       //Init calendars
-                                                       $calendars = [];
+                                                               //Init calendars
+                                                               $calendars = [];
 
 
-                                                       //Init counter
-                                                       $count = 0;
+                                                               //Init counter
+                                                               $count = 0;
 
 
-                                                       //Retrieve calendar
-                                                       try {
                                                                //Set page token
                                                                $pageToken = null;
 
                                                                //Set page token
                                                                $pageToken = null;
 
@@ -276,16 +303,65 @@ class UserController extends BaseUserController {
                                                                                }
                                                                        }
                                                                } while ($pageToken = $calendarList->getNextPageToken());
                                                                                }
                                                                        }
                                                                } while ($pageToken = $calendarList->getNextPageToken());
-                                                       //Catch exception
-                                                       } catch(\Google\Service\Exception $e) {
-                                                               //Throw error
-                                                               throw new \LogicException('Calendar list failed', 0, $e);
+
+                                                               //Cache calendars
+                                                               return $calendars;
+                                                       }
+                                               );
+                                       //Catch exception
+                                       } catch(\Google\Service\Exception $e) {
+                                               //With 401 or code
+                                               //XXX: see https://cloud.google.com/apis/design/errors
+                                               if ($e->getCode() == 401 || $e->getCode() == 403) {
+                                                       //Add error in flash message
+                                                       $this->addFlash(
+                                                               'error',
+                                                               $this->translator->trans(
+                                                                       'Unable to list calendars: %error%',
+                                                                       ['%error%' => $e->getMessage()]
+                                                               )
+                                                       );
+
+                                                       //Set calendar mails
+                                                       $cmails = [];
+
+                                                       //Iterate on each google token calendars
+                                                       foreach($googleToken->getGoogleCalendars() as $googleCalendar) {
+                                                               //Add calendar mail
+                                                               $cmails[] = $googleCalendar->getMail();
+
+                                                               //Remove google token calendar
+                                                               $this->manager->remove($googleCalendar);
                                                        }
 
                                                        }
 
-                                                       //Cache calendars
-                                                       return $calendars;
+                                                       //Log unlinked google token infos
+                                                       $this->logger->emergency(
+                                                               $this->translator->trans(
+                                                                       'denied: mail=%mail% gmail=%gmail% cmails=%cmails% locale=%locale%',
+                                                                       [
+                                                                               '%mail%' => $googleToken->getUser()->getMail(),
+                                                                               '%gmail%' => $googleToken->getMail(),
+                                                                               '%cmails' => implode(',', $cmails),
+                                                                               '%locale%' => $request->getLocale()
+                                                                       ]
+                                                               )
+                                                       );
+
+                                                       //Remove google token
+                                                       $this->manager->remove($googleToken);
+
+                                                       //Flush to delete it
+                                                       $this->manager->flush();
+
+                                                       //TODO: warn user by mail ?
+
+                                                       //Skip to next token
+                                                       continue;
                                                }
                                                }
-                                       );
+
+                                               //Throw error
+                                               throw new \LogicException('Calendar list failed', 0, $e);
+                                       }
 
                                        //Set formData array
                                        $formData = ['calendar' => []];
 
                                        //Set formData array
                                        $formData = ['calendar' => []];
@@ -308,12 +384,8 @@ class UserController extends BaseUserController {
                                                }
                                        }
 
                                                }
                                        }
 
-                                       //XXX: TODO: remove DEBUG
-                                       #header('Content-Type: text/plain');
-
-                                       //TODO: add feature to filter synchronized data (OrganizerId/DanceId)
                                        //TODO: add feature for alerts (-30min/-1h) ?
                                        //TODO: add feature for alerts (-30min/-1h) ?
-                                       //[Direct link to calendar ?][Direct link to calendar settings ?][Alerts][Remove]
+                                       //TODO: [Direct link to calendar ?][Direct link to calendar settings ?][Alerts][Remove]
 
                                        //Create the CalendarType form and give the proper parameters
                                        $form = $this->factory->createNamed('calendar_'.$googleShortMail, 'Rapsys\AirBundle\Form\CalendarType', $formData, [
 
                                        //Create the CalendarType form and give the proper parameters
                                        $form = $this->factory->createNamed('calendar_'.$googleShortMail, 'Rapsys\AirBundle\Form\CalendarType', $formData, [
@@ -536,17 +608,17 @@ class UserController extends BaseUserController {
                }
 
                //Without user
                }
 
                //Without user
-               if (empty($user = $this->getUser())) {
+               if (empty($user = $this->security->getUser())) {
                        throw new \LogicException('User is empty');
                }
 
                //Get google client
                $googleClient = new \Google\Client(
                        [
                        throw new \LogicException('User is empty');
                }
 
                //Get google client
                $googleClient = new \Google\Client(
                        [
-                               'application_name' => $request->server->get('GOOGLE_PROJECT'),
-                               'client_id' => $request->server->get('GOOGLE_CLIENT'),
-                               'client_secret' => $request->server->get('GOOGLE_SECRET'),
-                               'redirect_uri' => $this->generateUrl('rapsys_air_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
+                               'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'],
+                               'client_id' => $_ENV['GOOGLE_CLIENT_ID'],
+                               'client_secret' => $_ENV['GOOGLE_CLIENT_SECRET'],
+                               'redirect_uri' => $this->generateUrl('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
                                'scopes' => self::googleScopes,
                                'access_type' => 'offline',
                                'login_hint' => $user->getMail(),
                                'scopes' => self::googleScopes,
                                'access_type' => 'offline',
                                'login_hint' => $user->getMail(),
@@ -598,8 +670,7 @@ class UserController extends BaseUserController {
                                                        if ($i->getMail() == $userInfo['email']) {
                                                                return $i;
                                                        }
                                                        if ($i->getMail() == $userInfo['email']) {
                                                                return $i;
                                                        }
-                                               },
-                                               (object)[]
+                                               }
                                        )
                                ) {
                                        //Set mail
                                        )
                                ) {
                                        //Set mail
@@ -640,6 +711,6 @@ class UserController extends BaseUserController {
                }
 
                //Redirect to user
                }
 
                //Redirect to user
-               return $this->redirectToRoute('rapsys_user_edit', ['mail' => $short = $this->slugger->short($user->getMail()), 'hash' => $this->slugger->hash($short)]);
+               return $this->redirectToRoute('rapsysuser_edit', ['mail' => $short = $this->slugger->short($user->getMail()), 'hash' => $this->slugger->hash($short)]);
        }
 }
        }
 }