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; }
/** * Login action. * * @param Application $app * @param Request $request * @return Response */ public function loginAction(Application $app, Request $request) { $authException = $app['user.last_auth_exception']($request); if ($authException instanceof DisabledException) { // This exception is thrown if (!$user->isEnabled()) // Warning: Be careful not to disclose any user information besides the email address at this point. // The Security system throws this exception before actually checking if the password was valid. $user = $this->userManager->refreshUser($authException->getUser()); return $app['twig']->render($this->getTemplate('login-confirmation-needed'), array('layout_template' => $this->getTemplate('layout'), 'email' => $user->getEmail(), 'fromAddress' => $app['user.mailer']->getFromAddress(), 'resendUrl' => $app['url_generator']->generate('user.resend-confirmation'))); } return $app['twig']->render($this->getTemplate('login'), array('layout_template' => $this->getTemplate('layout'), 'error' => $authException ? $authException->getMessageKey() : null, 'last_username' => $app['session']->get('_security.last_username'), 'allowRememberMe' => isset($app['security.remember_me.response_listener']), 'allowPasswordReset' => $this->isPasswordResetEnabled())); }
public function testSupportsSubClass() { $this->userManager->setUserClass('\\SimpleUser\\Tests\\CustomUser'); $user = $this->userManager->createUser('*****@*****.**', 'password'); $supportsObject = $this->userManager->supportsClass(get_class($user)); $this->assertTrue($supportsObject); $this->userManager->insert($user); $freshUser = $this->userManager->refreshUser($user); $supportsRefreshedObject = $this->userManager->supportsClass(get_class($freshUser)); $this->assertTrue($supportsRefreshedObject); $this->assertTrue($freshUser instanceof CustomUser); }