Пример #1
0
 /**
  * Register action.
  *
  * @param Application $app
  * @param Request $request
  * @return Response
  */
 public function registerAction(Application $app, Request $request)
 {
     if ($request->isMethod('POST')) {
         try {
             $user = $this->createUserFromRequest($request);
             if ($error = $this->userManager->validatePasswordStrength($user, $request->request->get('password'))) {
                 throw new InvalidArgumentException($error);
             }
             if ($this->isEmailConfirmationRequired) {
                 $user->setEnabled(false);
                 $user->setConfirmationToken($app['user.tokenGenerator']->generateToken());
             }
             $this->userManager->insert($user);
             if ($this->isEmailConfirmationRequired) {
                 // Send email confirmation.
                 $app['user.mailer']->sendConfirmationMessage($user);
                 // Render the "go check your email" page.
                 return $app['twig']->render($this->getTemplate('register-confirmation-sent'), array('layout_template' => $this->getTemplate('layout'), 'email' => $user->getEmail()));
             } else {
                 // Log the user in to the new account.
                 $this->userManager->loginAsUser($user);
                 $app['session']->getFlashBag()->set('alert', 'Account created.');
                 // Redirect to user's new profile page.
                 return $app->redirect($app['url_generator']->generate('user.view', array('id' => $user->getId())));
             }
         } catch (InvalidArgumentException $e) {
             $error = $e->getMessage();
         }
     }
     return $app['twig']->render($this->getTemplate('register'), array('layout_template' => $this->getTemplate('layout'), 'error' => isset($error) ? $error : null, 'name' => $request->request->get('name'), 'email' => $request->request->get('email'), 'username' => $request->request->get('username'), 'isUsernameRequired' => $this->isUsernameRequired));
 }
Пример #2
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;
 }
 public function testMigrateDown()
 {
     $username = '******';
     $isEnabled = true;
     $confirmationToken = 'toke';
     $timePasswordResetRequested = null;
     $this->migrator->up();
     $user = new User('*****@*****.**', 'password');
     $user->setUsername($username);
     $user->setEnabled($isEnabled);
     $user->setConfirmationToken($confirmationToken);
     $user->setTimePasswordResetRequested($timePasswordResetRequested);
     $this->userManager->insert($user);
     $userId = $user->getId();
     // echo implode(";\n", $this->migrator->sqlDown());
     $this->migrator->down();
     $this->assertEquals($username, $this->fetchCustomField($userId, 'username'));
     $this->assertEquals($isEnabled, $this->fetchCustomField($userId, 'su:isEnabled'));
     $this->assertEquals($confirmationToken, $this->fetchCustomField($userId, 'su:confirmationToken'));
     $this->assertEquals($timePasswordResetRequested, $this->fetchCustomField($userId, 'su:timePasswordResetRequested'));
 }
Пример #4
0
 public function testAfterUpdateEvents()
 {
     $this->dispatcher->addListener(UserEvents::AFTER_UPDATE, function (UserEvent $event) {
         $event->getUser()->setCustomField('foo', 'bar');
     });
     $user = $this->userManager->createUser('*****@*****.**', 'password');
     $this->userManager->insert($user);
     // After update, the custom field set by the listener is available on the existing user instance.
     $this->assertFalse($user->hasCustomField('foo'));
     $this->userManager->update($user);
     $this->assertEquals('bar', $user->getCustomField('foo'));
     // The user was NOT stored with the custom field (because we set it AFTER update).
     // We'd have to save it again from within the after listener for it to be stored.
     $this->userManager->clearIdentityMap();
     // Clear the cache to force a fresh lookup from the database.
     $storedUser = $this->userManager->getUser($user->getId());
     $this->assertFalse($storedUser->hasCustomField('foo'));
 }
Пример #5
0
 /**
  * Register action.
  *
  * @param Application $app
  * @param Request $request
  * @return Response
  */
 public function registerAction(Application $app, Request $request)
 {
     if ($request->isMethod('POST')) {
         try {
             $user = $this->createUserFromRequest($request);
             $this->userManager->insert($user);
             $app['session']->getFlashBag()->set('alert', 'Account created.');
             // Log the user in to the new account.
             if (null !== ($current_token = $app['security']->getToken())) {
                 $providerKey = method_exists($current_token, 'getProviderKey') ? $current_token->getProviderKey() : $current_token->getKey();
                 $token = new UsernamePasswordToken($user, null, $providerKey);
                 $app['security']->setToken($token);
             }
             return $app->redirect($app['url_generator']->generate('user.view', array('id' => $user->getId())));
         } catch (InvalidArgumentException $e) {
             $error = $e->getMessage();
         }
     }
     return $app['twig']->render($this->registerTemplate, array('layout_template' => $this->layoutTemplate, 'error' => isset($error) ? $error : null, 'name' => $request->request->get('name'), 'email' => $request->request->get('email')));
 }