]> Raphaël G. Git Repositories - airbundle/blob - Controller/UserController.php
Register google client service and alias
[airbundle] / Controller / UserController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Controller;
13
14 use Symfony\Contracts\Cache\ItemInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
19 use Rapsys\UserBundle\Controller\UserController as BaseUserController;
20
21 use Rapsys\AirBundle\Entity\Dance;
22 use Rapsys\AirBundle\Entity\GoogleCalendar;
23 use Rapsys\AirBundle\Entity\GoogleToken;
24 use Rapsys\AirBundle\Entity\User;
25
26 /**
27 * {@inheritdoc}
28 */
29 class UserController extends BaseUserController {
30 /**
31 * Set google client scopes
32 */
33 const googleScopes = [\Google\Service\Calendar::CALENDAR_EVENTS, \Google\Service\Calendar::CALENDAR, \Google\Service\Oauth2::USERINFO_EMAIL];
34
35 /**
36 * {@inheritdoc}
37 */
38 public function edit(Request $request, string $hash, string $mail): Response {
39 //With invalid hash
40 if ($hash != $this->slugger->hash($mail)) {
41 //Throw bad request
42 throw new BadRequestHttpException($this->translator->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash]));
43 }
44
45 //Get mail
46 $mail = $this->slugger->unshort($smail = $mail);
47
48 //With existing subscriber
49 if (empty($user = $this->doctrine->getRepository($this->config['class']['user'])->findOneByMail($mail))) {
50 //Throw not found
51 //XXX: prevent slugger reverse engineering by not displaying decoded mail
52 throw $this->createNotFoundException($this->translator->trans('Unable to find account %mail%', ['%mail%' => $smail]));
53 }
54
55 //Prevent access when not admin, user is not guest and not currently logged user
56 if (!$this->checker->isGranted('ROLE_ADMIN') && $user != $this->security->getUser() || !$this->checker->isGranted('IS_AUTHENTICATED_FULLY')) {
57 //Throw access denied
58 //XXX: prevent slugger reverse engineering by not displaying decoded mail
59 throw $this->createAccessDeniedException($this->translator->trans('Unable to access user: %mail%', ['%mail%' => $smail]));
60 }
61
62 //Create the RegisterType form and give the proper parameters
63 $edit = $this->factory->create($this->config['edit']['view']['edit'], $user, [
64 //Set action to register route name and context
65 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $hash]+$this->config['route']['edit']['context']),
66 //Set civility class
67 'civility_class' => $this->config['class']['civility'],
68 //Set civility default
69 'civility_default' => $this->doctrine->getRepository($this->config['class']['civility'])->findOneByTitle($this->config['default']['civility']),
70 //Set country class
71 'country_class' => $this->config['class']['country'],
72 //Set country default
73 'country_default' => $this->doctrine->getRepository($this->config['class']['country'])->findOneByTitle($this->config['default']['country']),
74 //Set country favorites
75 'country_favorites' => $this->doctrine->getRepository($this->config['class']['country'])->findByTitle($this->config['default']['country_favorites']),
76 //Set dance
77 'dance' => $this->checker->isGranted('ROLE_ADMIN'),
78 //Set dance choices
79 'dance_choices' => $danceChoices = $this->doctrine->getRepository($this->config['class']['dance'])->findChoicesAsArray(),
80 //Set dance default
81 #'dance_default' => /*$this->doctrine->getRepository($this->config['class']['dance'])->findOneByNameType($this->config['default']['dance'])*/null,
82 //Set dance favorites
83 'dance_favorites' => $this->doctrine->getRepository($this->config['class']['dance'])->findIdByNameTypeAsArray($this->config['default']['dance_favorites']),
84 //Set subscription
85 'subscription' => $this->checker->isGranted('ROLE_ADMIN'),
86 //Set subscription choices
87 'subscription_choices' => $subscriptionChoices = $this->doctrine->getRepository($this->config['class']['user'])->findChoicesAsArray(),
88 //Set subscription default
89 #'subscription_default' => /*$this->doctrine->getRepository($this->config['class']['user'])->findOneByPseudonym($this->config['default']['subscription'])*/null,
90 //Set subscription favorites
91 'subscription_favorites' => $this->doctrine->getRepository($this->config['class']['user'])->findIdByPseudonymAsArray($this->config['default']['subscription_favorites']),
92 //Disable mail
93 'mail' => $this->checker->isGranted('ROLE_ADMIN'),
94 //Disable pseudonym
95 'pseudonym' => $this->checker->isGranted('ROLE_GUEST'),
96 //Disable password
97 'password' => false,
98 //Set method
99 'method' => 'POST'
100 ]+$this->config['edit']['field']);
101
102 //With admin role
103 if ($this->checker->isGranted('ROLE_ADMIN')) {
104 //Create the ResetType form and give the proper parameters
105 $reset = $this->factory->create($this->config['edit']['view']['reset'], $user, [
106 //Set action to register route name and context
107 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $hash]+$this->config['route']['edit']['context']),
108 //Disable mail
109 'mail' => false,
110 //Set method
111 'method' => 'POST'
112 ]);
113
114 //With post method
115 if ($request->isMethod('POST')) {
116 //Refill the fields in case the form is not valid.
117 $reset->handleRequest($request);
118
119 //With reset submitted and valid
120 if ($reset->isSubmitted() && $reset->isValid()) {
121 //Set data
122 $data = $reset->getData();
123
124 //Set password
125 $data->setPassword($this->hasher->hashPassword($data, $data->getPassword()));
126
127 //Queue user password save
128 $this->manager->persist($data);
129
130 //Flush to get the ids
131 $this->manager->flush();
132
133 //Add notice
134 $this->addFlash('notice', $this->translator->trans('Account %mail% password updated', ['%mail%' => $mail]));
135
136 //Redirect to cleanup the form
137 return $this->redirectToRoute($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $hash]+$this->config['route']['edit']['context']);
138 }
139 }
140
141 //Add reset view
142 $this->config['edit']['view']['context']['reset'] = $reset->createView();
143
144 //Add google calendar array
145 $this->config['edit']['view']['context']['calendar'] = [
146 //Form by mail
147 'form' => [],
148 //Uri to link account
149 'link' => null,
150 //Logo
151 'logo' => [
152 'png' => '@RapsysAir/png/calendar.png',
153 'svg' => '@RapsysAir/svg/calendar.svg'
154 ]
155 ];
156
157 //Get google client
158 $googleClient = new \Google\Client(
159 [
160 'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'],
161 'client_id' => $_ENV['GOOGLE_CLIENT_ID'],
162 'client_secret' => $_ENV['GOOGLE_CLIENT_SECRET'],
163 'redirect_uri' => $this->generateUrl('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
164 'scopes' => self::googleScopes,
165 'access_type' => 'offline',
166 'login_hint' => $user->getMail(),
167 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
168 #'approval_prompt' => 'force'
169 'prompt' => 'consent'
170 ]
171 );
172
173 //With user tokens
174 if (!($googleTokens = $user->getGoogleTokens())->isEmpty()) {
175 //Iterate on each google token
176 //XXX: either we finish with a valid token set or a logic exception after token removal
177 foreach($googleTokens as $googleToken) {
178 //Clear client cache before changing access token
179 //TODO: set a per token cache ?
180 $googleClient->getCache()->clear();
181
182 //Set access token
183 $googleClient->setAccessToken(
184 [
185 'access_token' => $googleToken->getAccess(),
186 'refresh_token' => $googleToken->getRefresh(),
187 'created' => $googleToken->getCreated()->getTimestamp(),
188 'expires_in' => $googleToken->getExpired()->getTimestamp() - (new \DateTime('now'))->getTimestamp(),
189 ]
190 );
191
192 //With expired token
193 if ($googleClient->isAccessTokenExpired()) {
194 //Refresh token
195 if (($refresh = $googleClient->getRefreshToken()) && ($token = $googleClient->fetchAccessTokenWithRefreshToken($refresh)) && empty($token['error'])) {
196 //Set access token
197 $googleToken->setAccess($token['access_token']);
198
199 //Set expires
200 $googleToken->setExpired(new \DateTime('+'.$token['expires_in'].' second'));
201
202 //Set refresh
203 $googleToken->setRefresh($token['refresh_token']);
204
205 //Queue google token save
206 $this->manager->persist($googleToken);
207
208 //Flush to get the ids
209 $this->manager->flush();
210 //Refresh failed
211 } else {
212 //Add error in flash message
213 $this->addFlash(
214 'error',
215 $this->translator->trans(
216 empty($token['error'])?'Unable to refresh token':'Unable to refresh token: %error%',
217 empty($token['error'])?[]:['%error%' => str_replace('_', ' ', $token['error'])]
218 )
219 );
220
221 //Set calendar mails
222 $cmails = [];
223
224 //Iterate on each google token calendars
225 foreach($googleToken->getGoogleCalendars() as $googleCalendar) {
226 //Add calendar mail
227 $cmails[] = $googleCalendar->getMail();
228
229 //Remove google token calendar
230 $this->manager->remove($googleCalendar);
231 }
232
233 //Log unlinked google token infos
234 $this->logger->emergency(
235 $this->translator->trans(
236 'expired: mail=%mail% gmail=%gmail% cmails=%cmails% locale=%locale%',
237 [
238 '%mail%' => $googleToken->getUser()->getMail(),
239 '%gmail%' => $googleToken->getMail(),
240 '%cmails' => implode(',', $cmails),
241 '%locale%' => $request->getLocale()
242 ]
243 )
244 );
245
246 //Remove google token
247 $this->manager->remove($googleToken);
248
249 //Flush to delete it
250 $this->manager->flush();
251
252 //TODO: warn user by mail ?
253
254 //Skip to next token
255 continue;
256 }
257 }
258
259 //XXX: TODO: remove DEBUG
260 #$this->cache->delete('user.edit.calendar.'.$this->slugger->short($googleToken->getMail()));
261
262 //Retrieve calendar
263 try {
264 //Get calendars
265 $calendars = $this->cache->get(
266 //Set key to user.edit.$mail
267 ($calendarKey = 'user.edit.calendar.'.($googleShortMail = $this->slugger->short($googleMail = $googleToken->getMail()))),
268 //Fetch mail calendar list
269 function (ItemInterface $item) use ($googleClient): array {
270 //Expire after 1h
271 $item->expiresAfter(3600);
272
273 //Get google calendar service
274 $service = new \Google\Service\Calendar($googleClient);
275
276 //Init calendars
277 $calendars = [];
278
279 //Init counter
280 $count = 0;
281
282 //Set page token
283 $pageToken = null;
284
285 //Iterate until next page token is null
286 do {
287 //Get token calendar list
288 //XXX: require permission to read and write events
289 $calendarList = $service->calendarList->listCalendarList(['pageToken' => $pageToken, 'minAccessRole' => 'writer', 'showHidden' => true]);
290
291 //Iterate on items
292 foreach($calendarList->getItems() as $calendarItem) {
293 //With primary calendar
294 if ($calendarItem->getPrimary()) {
295 //Add primary calendar
296 //XXX: use primary as key as described in google api documentation
297 $calendars = ['primary' => $this->translator->trans('Primary') /*$calendarItem->getSummary()*/] + $calendars;
298 //With secondary calendar
299 } else {
300 //Add secondary calendar
301 //XXX: Append counter to make sure summary is unique for later array_flip call
302 $calendars += [$calendarItem->getId() => $calendarItem->getSummary().' ('.++$count.')'];
303 }
304 }
305 } while ($pageToken = $calendarList->getNextPageToken());
306
307 //Cache calendars
308 return $calendars;
309 }
310 );
311 //Catch exception
312 } catch(\Google\Service\Exception $e) {
313 //With 401 or code
314 //XXX: see https://cloud.google.com/apis/design/errors
315 if ($e->getCode() == 401 || $e->getCode() == 403) {
316 //Add error in flash message
317 $this->addFlash(
318 'error',
319 $this->translator->trans(
320 'Unable to list calendars: %error%',
321 ['%error%' => $e->getMessage()]
322 )
323 );
324
325 //Set calendar mails
326 $cmails = [];
327
328 //Iterate on each google token calendars
329 foreach($googleToken->getGoogleCalendars() as $googleCalendar) {
330 //Add calendar mail
331 $cmails[] = $googleCalendar->getMail();
332
333 //Remove google token calendar
334 $this->manager->remove($googleCalendar);
335 }
336
337 //Log unlinked google token infos
338 $this->logger->emergency(
339 $this->translator->trans(
340 'denied: mail=%mail% gmail=%gmail% cmails=%cmails% locale=%locale%',
341 [
342 '%mail%' => $googleToken->getUser()->getMail(),
343 '%gmail%' => $googleToken->getMail(),
344 '%cmails' => implode(',', $cmails),
345 '%locale%' => $request->getLocale()
346 ]
347 )
348 );
349
350 //Remove google token
351 $this->manager->remove($googleToken);
352
353 //Flush to delete it
354 $this->manager->flush();
355
356 //TODO: warn user by mail ?
357
358 //Skip to next token
359 continue;
360 }
361
362 //Throw error
363 throw new \LogicException('Calendar list failed', 0, $e);
364 }
365
366 //Set formData array
367 $formData = ['calendar' => []];
368
369 //With google calendars
370 if (!($googleCalendars = $googleToken->getGoogleCalendars())->isEmpty()) {
371 //Iterate on each google calendars
372 foreach($googleCalendars as $googleCalendar) {
373 //With existing google calendar
374 if (isset($calendars[$googleCalendar->getMail()])) {
375 //Add google calendar to form data
376 $formData['calendar'][] = $googleCalendar->getMail();
377 } else {
378 //Remove google calendar from database
379 $this->manager->remove($googleCalendar);
380
381 //Flush to persist ids
382 $this->manager->flush();
383 }
384 }
385 }
386
387 //TODO: add feature for alerts (-30min/-1h) ?
388 //TODO: [Direct link to calendar ?][Direct link to calendar settings ?][Alerts][Remove]
389
390 //Create the CalendarType form and give the proper parameters
391 $form = $this->factory->createNamed('calendar_'.$googleShortMail, 'Rapsys\AirBundle\Form\CalendarType', $formData, [
392 //Set action to register route name and context
393 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $hash]+$this->config['route']['edit']['context']),
394 //Set calendar choices
395 //XXX: unique calendar summary required by choice widget is guaranteed by appending ' (x)' to secondary calendars earlier
396 'calendar_choices' => array_flip($calendars),
397 //Set method
398 'method' => 'POST'
399 ]);
400
401 //With post method
402 if ($request->isMethod('POST')) {
403 //Refill the fields in case the form is not valid.
404 $form->handleRequest($request);
405
406 //With reset submitted and valid
407 if ($form->isSubmitted() && $form->isValid()) {
408 //Set data
409 $data = $form->getData();
410
411 //Refresh button
412 if (($clicked = $form->getClickedButton()->getName()) == 'refresh') {
413 //Remove calendar key
414 $this->cache->delete($calendarKey);
415
416 //Add notice
417 $this->addFlash('notice', $this->translator->trans('Account %mail% calendars updated', ['%mail%' => $googleMail]));
418 //Add button
419 } elseif ($clicked == 'add') {
420 //Get google calendar service
421 $service = new \Google\Service\Calendar($googleClient);
422
423 //Add calendar
424 try {
425 //Instantiate calendar
426 $calendar = new \Google\Service\Calendar\Calendar(
427 [
428 'summary' => $this->translator->trans($this->config['context']['site']['title']),
429 'timeZone' => date_default_timezone_get()
430 ]
431 );
432
433 //Insert calendar
434 $service->calendars->insert($calendar);
435 //Catch exception
436 } catch(\Google\Service\Exception $e) {
437 //Throw error
438 throw new \LogicException('Calendar insert failed', 0, $e);
439 }
440
441 //Remove calendar key
442 $this->cache->delete($calendarKey);
443
444 //Add notice
445 $this->addFlash('notice', $this->translator->trans('Account %mail% calendar added', ['%mail%' => $googleMail]));
446 //Delete button
447 } elseif ($clicked == 'delete') {
448 //Get google calendar service
449 $service = new \Google\Service\Calendar($googleClient);
450
451 //Remove calendar
452 try {
453 //Set site title
454 $siteTitle = $this->translator->trans($this->config['context']['site']['title']);
455
456 //Iterate on calendars
457 foreach($calendars as $calendarId => $calendarSummary) {
458 //With calendar matching site title
459 if (substr($calendarSummary, 0, strlen($siteTitle)) == $siteTitle) {
460 //Delete the calendar
461 $service->calendars->delete($calendarId);
462 }
463 }
464 //Catch exception
465 } catch(\Google\Service\Exception $e) {
466 //Throw error
467 throw new \LogicException('Calendar delete failed', 0, $e);
468 }
469
470 //Remove calendar key
471 $this->cache->delete($calendarKey);
472
473 //Add notice
474 $this->addFlash('notice', $this->translator->trans('Account %mail% calendars deleted', ['%mail%' => $googleMail]));
475 //Unlink button
476 } elseif ($clicked == 'unlink') {
477 //Iterate on each google calendars
478 foreach($googleCalendars as $googleCalendar) {
479 //Remove google calendar from database
480 $this->manager->remove($googleCalendar);
481 }
482
483 //Remove google token from database
484 $this->manager->remove($googleToken);
485
486 //Flush to persist
487 $this->manager->flush();
488
489 //Revoke access token
490 $googleClient->revokeToken($googleToken->getAccess());
491
492 //With refresh token
493 if ($refresh = $googleToken->getRefresh()) {
494 //Revoke refresh token
495 $googleClient->revokeToken($googleToken->getRefresh());
496 }
497
498 //Remove calendar key
499 $this->cache->delete($calendarKey);
500
501 //Add notice
502 $this->addFlash('notice', $this->translator->trans('Account %mail% calendars unlinked', ['%mail%' => $googleMail]));
503 //Submit button
504 } else {
505 //Flipped calendar data
506 $dataCalendarFlip = array_flip($data['calendar']);
507
508 //Iterate on each google calendars
509 foreach($googleCalendars as $googleCalendar) {
510 //Without calendar in flipped data
511 if (!isset($dataCalendarFlip[$googleCalendarMail = $googleCalendar->getMail()])) {
512 //Remove google calendar from database
513 $this->manager->remove($googleCalendar);
514 //With calendar in flipped data
515 } else {
516 //Remove google calendar from calendar data
517 unset($data['calendar'][$dataCalendarFlip[$googleCalendarMail]]);
518 }
519 }
520
521 //Iterate on remaining calendar data
522 foreach($data['calendar'] as $googleCalendarMail) {
523 //Create new google calendar
524 //XXX: remove trailing ' (x)' from summary
525 $googleCalendar = new GoogleCalendar($googleToken, $googleCalendarMail, preg_replace('/ \([0-9]\)$/', '', $calendars[$googleCalendarMail]));
526
527 //Queue google calendar save
528 $this->manager->persist($googleCalendar);
529 }
530
531 //Flush to persist ids
532 $this->manager->flush();
533
534 //Add notice
535 $this->addFlash('notice', $this->translator->trans('Account %mail% calendars updated', ['%mail%' => $googleMail]));
536 }
537
538 //Redirect to cleanup the form
539 return $this->redirectToRoute($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $hash]+$this->config['route']['edit']['context']);
540 }
541 }
542
543 //Add form view
544 $this->config['edit']['view']['context']['calendar']['form'][$googleToken->getMail()] = $form->createView();
545 }
546 }
547
548 //Add google calendar auth url
549 $this->config['edit']['view']['context']['calendar']['link'] = $googleClient->createAuthUrl();
550 }
551
552 //With post method
553 if ($request->isMethod('POST')) {
554 //Refill the fields in case the form is not valid.
555 $edit->handleRequest($request);
556
557 //With edit submitted and valid
558 if ($edit->isSubmitted() && $edit->isValid()) {
559 //Set data
560 $data = $edit->getData();
561
562 //Queue user save
563 $this->manager->persist($data);
564
565 //Try saving in database
566 try {
567 //Flush to get the ids
568 $this->manager->flush();
569
570 //Add notice
571 //XXX: get mail from data as it may change
572 $this->addFlash('notice', $this->translator->trans('Account %mail% updated', ['%mail%' => $mail = $data->getMail()]));
573
574 //Redirect to cleanup the form
575 return $this->redirectToRoute($this->config['route']['edit']['name'], ['mail' => $smail = $this->slugger->short($mail), 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']);
576 //Catch double slug or mail
577 } catch (UniqueConstraintViolationException $e) {
578 //Add error message mail already exists
579 $this->addFlash('error', $this->translator->trans('Account %mail% already exists', ['%mail%' => $data->getMail()]));
580 }
581 }
582 //Without admin role
583 //XXX: prefer a reset on login to force user unspam action
584 } elseif (!$this->checker->isGranted('ROLE_ADMIN')) {
585 //Add notice
586 $this->addFlash('notice', $this->translator->trans('To change your password login with your mail and any password then follow the procedure'));
587 }
588
589 //Render view
590 return $this->render(
591 //Template
592 $this->config['edit']['view']['name'],
593 //Context
594 ['edit' => $edit->createView(), 'sent' => $request->query->get('sent', 0)]+$this->config['edit']['view']['context']
595 );
596 }
597
598 /**
599 * Handle google callback
600 *
601 * @param Request $request The request
602 * @return Response The response
603 */
604 public function googleCallback(Request $request): Response {
605 //Without code
606 if (empty($code = $request->query->get('code', ''))) {
607 throw new \InvalidArgumentException('Query parameter code is empty');
608 }
609
610 //Without user
611 if (empty($user = $this->security->getUser())) {
612 throw new \LogicException('User is empty');
613 }
614
615 //Get google client
616 $googleClient = new \Google\Client(
617 [
618 'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'],
619 'client_id' => $_ENV['GOOGLE_CLIENT_ID'],
620 'client_secret' => $_ENV['GOOGLE_CLIENT_SECRET'],
621 'redirect_uri' => $this->generateUrl('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
622 'scopes' => self::googleScopes,
623 'access_type' => 'offline',
624 'login_hint' => $user->getMail(),
625 #'approval_prompt' => 'force'
626 'prompt' => 'consent'
627 ]
628 );
629
630 //Protect to extract failure
631 try {
632 //Authenticate with code
633 if (!empty($token = $googleClient->authenticate($code))) {
634 //With error
635 if (!empty($token['error'])) {
636 throw new \LogicException('Client authenticate failed: '.str_replace('_', ' ', $token['error']));
637 //Without refresh token
638 } elseif (empty($token['refresh_token'])) {
639 throw new \LogicException('Refresh token is empty');
640 //Without expires in
641 } elseif (empty($token['expires_in'])) {
642 throw new \LogicException('Expires in is empty');
643 //Without scope
644 } elseif (empty($token['scope'])) {
645 throw new \LogicException('Scope in is empty');
646 //Without valid scope
647 } elseif (array_intersect(self::googleScopes, explode(' ', $token['scope'])) != self::googleScopes) {
648 throw new \LogicException('Scope in is not valid');
649 }
650
651 //Get Oauth2 object
652 $oauth2 = new \Google\Service\Oauth2($googleClient);
653
654 //Protect user info get call
655 try {
656 //Retrieve user info
657 $userInfo = $oauth2->userinfo->get();
658 //Catch exception
659 } catch(\Google\Service\Exception $e) {
660 //Throw error
661 throw new \LogicException('Userinfo get failed', 0, $e);
662 }
663
664 //With existing token
665 if (
666 //If available retrieve google token with matching mail
667 $googleToken = array_reduce(
668 $user->getGoogleTokens()->getValues(),
669 function ($c, $i) use ($userInfo) {
670 if ($i->getMail() == $userInfo['email']) {
671 return $i;
672 }
673 }
674 )
675 ) {
676 //Set mail
677 //XXX: TODO: should already be set and not change, remove ?
678 //XXX: TODO: store picture as well ?
679 $googleToken->setMail($userInfo['email']);
680
681 //Set access token
682 $googleToken->setAccess($token['access_token']);
683
684 //Set expires
685 $googleToken->setExpired(new \DateTime('+'.$token['expires_in'].' second'));
686
687 //Set refresh
688 $googleToken->setRefresh($token['refresh_token']);
689 } else {
690 //Create new token
691 //XXX: TODO: store picture as well ?
692 $googleToken = new GoogleToken($user, $userInfo['email'], $token['access_token'], new \DateTime('+'.$token['expires_in'].' second'), $token['refresh_token']);
693 }
694
695 //Queue google token save
696 $this->manager->persist($googleToken);
697
698 //Flush to get the ids
699 $this->manager->flush();
700
701 //Add notice
702 $this->addFlash('notice', $this->translator->trans('Account %mail% google token updated', ['%mail%' => $user->getMail()]));
703 //With failed authenticate
704 } else {
705 throw new \LogicException('Client authenticate failed');
706 }
707 //Catch exception
708 } catch(\Exception $e) {
709 //Add notice
710 $this->addFlash('error', $this->translator->trans('Account %mail% google token rejected: %error%', ['%mail%' => $user->getMail(), '%error%' => $e->getMessage()]));
711 }
712
713 //Redirect to user
714 return $this->redirectToRoute('rapsysuser_edit', ['mail' => $short = $this->slugger->short($user->getMail()), 'hash' => $this->slugger->hash($short)]);
715 }
716 }