/**
  * Masquerades the current user as a given user.
  *
  * Access to masquerade as the target user account has to checked by all callers
  * via masquerade_target_user_access() already.
  *
  * @param \Drupal\user\UserInterface $user
  *   The user account object to masquerade as.
  *
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  *   Redirect to previous page.
  *
  * @see this::getRedirectResponse()
  */
 public function switchTo(UserInterface $user)
 {
     // Store current user for messages.
     $account = $this->currentUser;
     $error = masquerade_switch_user_validate($user);
     if (empty($error)) {
         if ($this->masquerade->switchTo($user)) {
             drupal_set_message($this->t('You are now masquerading as @user.', array('@user' => $account->getDisplayName())));
         }
     } else {
         drupal_set_message($error, 'error');
     }
     return $this->getRedirectResponse();
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $user_id = $form_state->getValue('masquerade_as');
     if (empty($user_id)) {
         $form_state->setErrorByName('masquerade_as', $this->t('The user does not exist. Please enter a valid username.'));
         return;
     }
     $target_account = $this->entityManager->getStorage('user')->load($user_id);
     if ($error = masquerade_switch_user_validate($target_account)) {
         $form_state->setErrorByName('masquerade_as', $error);
     } else {
         $form_state->setValue('masquerade_target_account', $target_account);
     }
 }