Exemple #1
0
 protected function eventListener()
 {
     $app = $this->app;
     $app->on(OAuthEvents::USER, function ($event) use($app) {
         $this->usrToken = $event->getToken();
         $this->email = $this->usrToken->getEmail();
         $this->name = $this->usrToken->getUsername();
         $this->usrId = $this->usrToken->getUid();
         try {
             //
             // Login user
             $user = $this->usrManager->loadUserByUsername($this->email ?: $this->name);
             $this->user = $this->usrManager->refreshUser($user);
             $this->usrToken->setUser($this->user);
             $this->user->setConfirmationToken(null);
             $this->user->setEnabled(true);
             $this->usrManager->loginAsUser($this->user);
             $this->logger(" Try to login user from " . $this->usrToken->getService());
         } catch (UsernameNotFoundException $e) {
             //
             // Register new user
             $this->generatePassword();
             $this->user = $this->usrManager->createUser($this->email, $this->password, $this->name);
             $this->usrToken->setUser($this->user);
             $this->user->setConfirmationToken(null);
             $this->user->setEnabled(true);
             $this->user->setUsername($this->usrId);
             $this->usrManager->insert($this->user);
             $this->usrManager->loginAsUser($this->user);
             $app['session']->getFlashBag()->set('alert', 'Account created.');
             $this->logger(" Try to register user from " . $this->usrToken->getService());
         }
         if ($this->app['user']) {
             $this->setSuccessHandler($this->usrToken->getService());
         }
         // TODO send an email with information about the user
         $app->after(function () use($app) {
             $this->usrManager->loginAsUser($this->user);
             if ($this->usrManager->isLoggedIn()) {
                 $newUrl = $app['url_generator']->generate('user.view', array('id' => $this->user->getId()));
                 return new RedirectResponse($newUrl);
             }
         });
     });
     return $app;
 }
 /**
  * @param Application $app
  * @param Request $request
  * @param string $token
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  */
 public function resetPasswordAction(Application $app, Request $request, $token)
 {
     if (!$this->isPasswordResetEnabled()) {
         throw new NotFoundHttpException('Password resetting is not enabled.');
     }
     $tokenExpired = false;
     $user = $this->userManager->findOneBy(array('confirmationToken' => $token));
     if (!$user) {
         $tokenExpired = true;
     } else {
         if ($user->isPasswordResetRequestExpired($app['user.options']['passwordReset']['tokenTTL'])) {
             $tokenExpired = true;
         }
     }
     if ($tokenExpired) {
         $app['session']->getFlashBag()->set('alert', 'Sorry, your password reset link has expired.');
         return $app->redirect($app['url_generator']->generate('user.login'));
     }
     $error = '';
     if ($request->isMethod('POST')) {
         // Validate the password
         $password = $request->request->get('password');
         if ($password != $request->request->get('confirm_password')) {
             $error = 'Passwords don\'t match.';
         } else {
             if ($error = $this->userManager->validatePasswordStrength($user, $password)) {
             } else {
                 // Set the password and log in.
                 $this->userManager->setUserPassword($user, $password);
                 $user->setConfirmationToken(null);
                 $user->setEnabled(true);
                 $this->userManager->update($user);
                 $this->userManager->loginAsUser($user);
                 $app['session']->getFlashBag()->set('alert', 'Your password has been reset and you are now signed in.');
                 return $app->redirect($app['url_generator']->generate('user.view', array('id' => $user->getId())));
             }
         }
     }
     return $app['twig']->render($this->getTemplate('reset-password'), array('layout_template' => $this->getTemplate('layout'), 'user' => $user, 'token' => $token, 'error' => $error));
 }