Esempio n. 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;
 }
Esempio n. 2
0
 /**
  * @param Request $request
  * @return User
  * @throws InvalidArgumentException
  */
 protected function createUserFromRequest(Request $request)
 {
     if ($request->request->get('password') != $request->request->get('confirm_password')) {
         throw new InvalidArgumentException('Passwords don\'t match.');
     }
     $user = $this->userManager->createUser($request->request->get('email'), $request->request->get('password'), $request->request->get('name') ?: null);
     $errors = $this->userManager->validate($user);
     if (!empty($errors)) {
         throw new InvalidArgumentException(implode("\n", $errors));
     }
     return $user;
 }
 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'));
 }