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('LoginCidadaoCoreBundle: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->clientRepository->findOneBy(['uid' => $this->defaultClientUid]));
     $auth->setScope(explode(' ', $this->lcSupportedScopes));
     $this->em->persist($auth);
     $this->em->flush();
     $this->mailer->sendConfirmationEmailMessage($user);
     if (strlen($user->getPassword()) == 0) {
         // TODO: DEPRECATE NOTIFICATIONS
         // TODO: create an optional task offering users to set a password
         //$this->notificationsHelper->enforceEmptyPasswordNotification($user);
     }
     $this->registerRequestedScope->clearRequestedScope($event->getRequest());
 }