/**
  * Action to login a user with username and password authentication
  * @return null
  */
 public function loginAction()
 {
     $user = $this->securityManager->getUser();
     if ($user !== null) {
         $this->response->setRedirect($this->request->getBaseUrl());
         return;
     }
     $form = new AuthenticationForm($this->request->getBasePath() . Request::QUERY_SEPARATOR . self::ACTION_LOGIN);
     if (!$form->isSubmitted()) {
         $this->setSecurityReferer();
         $this->setAuthenticationView($form);
         return;
     }
     if ($form->isCancelled()) {
         $this->response->setRedirect($this->getSecurityReferer());
         return;
     }
     try {
         $form->validate();
         $username = $form->getUsername();
         $password = $form->getPassword();
         $this->securityManager->login($username, $password);
         $this->response->setRedirect($this->getSecurityReferer());
         return;
     } catch (AuthenticationException $exception) {
         if ($exception->getField() == null) {
             throw $exception;
         }
         $validationError = new ValidationError($exception->getTranslationKey(), $exception->getMessage());
         $validationException = new ValidationException();
         $validationException->addErrors($exception->getField(), array($validationError));
         $form->setValidationException($validationException);
     } catch (ValidationException $validationException) {
     }
     $this->setAuthenticationView($form);
 }
예제 #2
0
 /**
  * Action to show the authentication form and to process authentication
  * @return null
  */
 public function indexAction()
 {
     $securityManager = SecurityManager::getInstance();
     $session = $this->getSession();
     $redirect = $this->getRedirect();
     $user = $securityManager->getUser();
     if ($user) {
         // user is already logged in
         $redirectUrl = null;
         switch ($redirect) {
             case self::REDIRECT_HOME:
                 $redirectUrl = $this->request->getBaseUrl();
                 break;
             case self::REDIRECT_REFERER:
                 $redirectUrl = $this->getReferer();
                 break;
         }
         if ($redirectUrl) {
             $this->response->setRedirect($redirectUrl);
         }
         return;
     }
     // gets the general referer
     $referer = $session->get(AdminModule::SESSION_REFERER);
     if (!$referer || substr_compare($referer, $this->request->getBasePath(), 0, strlen($this->request->getBasePath())) == 1) {
         $referer = $this->request->getBaseUrl();
     }
     $form = new AuthenticationForm($this->request->getBasePath());
     if (!$form->isSubmitted()) {
         // the form is not submitted, store the general referer as the login referer
         $session->set(self::SESSION_REFERER, $referer);
         $this->setLoginView($form);
         return;
     }
     // gets the login referer
     $redirectUrl = $session->get(self::SESSION_REFERER, $referer);
     if ($form->isCancelled()) {
         // the form is cancelled, redirect to the login referer
         $this->response->setRedirect($redirectUrl);
         return;
     }
     try {
         // try to authenticate the user
         $form->validate();
         $username = $form->getValue(SecurityManager::USERNAME);
         $password = $form->getValue(SecurityManager::PASSWORD);
         $securityManager->login($username, $password);
         // get the redirect url
         $redirect = $this->getRedirect();
         switch ($redirect) {
             case self::REDIRECT_NO:
                 $redirectUrl = $this->request->getBasePath();
                 break;
             case self::REDIRECT_HOME:
                 $redirectUrl = $this->request->getBaseUrl();
                 break;
         }
         $this->response->setRedirect($redirectUrl);
         return;
     } catch (AuthenticationException $e) {
         // authentication error
         if ($e->getField() == null) {
             throw $e;
         }
         $error = new ValidationError($e->getTranslationKey(), $e->getMessage());
         $exception = new ValidationException();
         $exception->addErrors($e->getField(), array($error));
         $form->setValidationException($exception);
     } catch (ValidationException $exception) {
         // no username or password filled in, exception already set to the form
     }
     $this->setLoginView($form);
 }