/**
  * Action to switch the current user
  * @param string $username Username of the user to switch to
  * @return null
  */
 public function indexAction($username = null)
 {
     if ($username) {
         $this->securityManager->switchUser($username);
         $this->response->setRedirect($this->getReferer());
         return;
     }
     $basePath = $this->request->getBasePath();
     $form = new UserSwitchForm($basePath);
     $form->setAutoComplete($basePath . '/' . self::ACTION_AUTO_COMPLETE_USER, 2);
     if (!$form->isSubmitted()) {
         $this->setUserSwitchView($form);
         return;
     }
     if ($form->isCancelled()) {
         $this->response->setRedirect($this->request->getBaseUrl());
         return;
     }
     try {
         $form->validate();
         $username = $form->getUsername();
         $this->securityManager->switchUser($username);
         $this->response->setRedirect($this->request->getBaseUrl());
         return;
     } catch (UserNotFoundException $userNotFoundException) {
         $validationError = new ValidationError(self::TRANSLATION_ERROR_USER_NOT_FOUND, 'Could not find user %user%', array('user' => $username));
         $validationException = new ValidationException();
         $validationException->addErrors(UserSwitchForm::FIELD_USERNAME, array($validationError));
         $form->setValidationException($validationException);
     } catch (UserSwitchException $userSwitchException) {
         $validationError = new ValidationError(self::TRANSLATION_ERROR_USER_NOT_ALLOWED, 'You are not allowed to switch to %user%', array('user' => $username));
         $validationException = new ValidationException();
         $validationException->addErrors(UserSwitchForm::FIELD_USERNAME, array($validationError));
         $form->setValidationException($validationException);
     } catch (ValidationException $validationException) {
     }
     $this->setUserSwitchView($form);
 }