public function onPostAuthorizationProcess(OAuthEvent $event)
 {
     if (!$event->isAuthorizedClient()) {
         return;
     }
     if (null === ($client = $event->getClient())) {
         return;
     }
     $user = $this->getUser($event);
     $scope = $this->getScope();
     $em = $this->doctrine->getManager();
     $authRepo = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:Authorization');
     $currentAuth = $authRepo->findOneBy(array('person' => $user, 'client' => $client));
     // if the authorization is already there, update it.
     if ($currentAuth instanceof Authorization) {
         $merged = array_merge($currentAuth->getScope(), $scope);
         $currentAuth->setScope($merged);
     } else {
         $authorization = new Authorization();
         $authorization->setClient($client);
         $authorization->setPerson($user);
         $authorization->setScope($scope);
         $em->persist($authorization);
     }
     $em->flush();
 }
 public function onRegistrationCompleted(FilterUserResponseEvent $event)
 {
     $user = $event->getUser();
     $auth = new Authorization();
     $auth->setPerson($user);
     $auth->setClient($this->notificationHandler->getLoginCidadaoClient());
     $auth->setScope(explode(' ', $this->lcSupportedScopes));
     $this->em->persist($auth);
     $this->em->flush();
     $this->mailer->sendConfirmationEmailMessage($user);
     if (strlen($user->getPassword()) == 0) {
         $this->notificationsHelper->enforceEmptyPasswordNotification($user);
     }
 }