You are here

class ContextUserAuth in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8 src/Context/ContextUserAuth.php \Drupal\tmgmt_smartling\Context\ContextUserAuth
  2. 8.2 src/Context/ContextUserAuth.php \Drupal\tmgmt_smartling\Context\ContextUserAuth
  3. 8.3 src/Context/ContextUserAuth.php \Drupal\tmgmt_smartling\Context\ContextUserAuth

Hierarchy

Expanded class hierarchy of ContextUserAuth

1 file declares its use of ContextUserAuth
SendContextActionApproveForm.php in src/Form/SendContextActionApproveForm.php
1 string reference to 'ContextUserAuth'
tmgmt_smartling.services.yml in ./tmgmt_smartling.services.yml
tmgmt_smartling.services.yml
1 service uses ContextUserAuth
tmgmt_smartling.utils.context.user_auth in ./tmgmt_smartling.services.yml
Drupal\tmgmt_smartling\Context\ContextUserAuth

File

src/Context/ContextUserAuth.php, line 13

Namespace

Drupal\tmgmt_smartling\Context
View source
class ContextUserAuth {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $account;

  /**
   * The user storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $userStorage;

  /**
   * The session manager service.
   *
   * @var \Drupal\Core\Session\SessionManagerInterface
   */
  protected $sessionManager;

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;

  /**
   * @var LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a new SwitchUserController object
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $account
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManager $entity_manager
   *   The user storage.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The user storage.
   * @param \Drupal\Core\Session\SessionManagerInterface $session_manager
   *   The session manager service.
   * @param \Symfony\Component\HttpFoundation\Session\Session $session
   *   The session.
   * @param \Psr\Log\LoggerInterface $logger
   */
  public function __construct(AccountProxyInterface $account, EntityTypeManager $entity_manager, ModuleHandlerInterface $module_handler, SessionManagerInterface $session_manager, Session $session, LoggerInterface $logger) {
    $this->account = $account;
    $this->userStorage = $entity_manager
      ->getStorage('user');
    $this->moduleHandler = $module_handler;
    $this->sessionManager = $session_manager;
    $this->session = $session;
    $this->logger = $logger;
  }

  /**
   * Returns cookies of the needed user.
   *
   * @param string $name
   * @param bool $context_silent_user_switching
   * @return string
   * @throws \Drupal\tmgmt_smartling\Exceptions\WrongUsernameException
   */
  public function getCookies($name, $context_silent_user_switching = FALSE) {
    if ($this->account
      ->getAccountName() !== $name) {
      $this
        ->switchUser($name, $context_silent_user_switching);
    }
    return session_name() . "=" . session_id();
  }
  public function getCurrentAccount() {
    return $this->account;
  }

  /**
   * Switches to a different user.
   *
   * We don't call session_save_session() because we really want to change users.
   * Usually unsafe!
   *
   * @param string $name
   *   The username to switch to, or NULL to log out.
   * @param bool $context_silent_user_switching
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse.
   * A redirect response object.
   * @throws \Drupal\tmgmt_smartling\Exceptions\WrongUsernameException
   */
  public function switchUser($name = NULL, $context_silent_user_switching = FALSE) {
    $this->logger
      ->info('We are about to switch user from "@user1" to "@user2"', [
      '@user1' => $this->account
        ->getAccountName(),
      '@user2' => $name,
    ]);
    if (empty($name) || !($account = $this->userStorage
      ->loadByProperties([
      'name' => $name,
    ]))) {
      throw new WrongUsernameException(t('User with username "@username" was not found', [
        '@username' => $name,
      ]));
    }
    $account = reset($account);

    // Call logout hooks when switching from original user.
    if (empty($context_silent_user_switching)) {
      $this->moduleHandler
        ->invokeAll('user_logout', [
        $this->account,
      ]);
    }

    // Regenerate the session ID to prevent against session fixation attacks.
    $this->sessionManager
      ->regenerate();

    // Based off masquarade module as:
    // https://www.drupal.org/node/218104 doesn't stick and instead only
    // keeps context until redirect.
    $this->account
      ->setAccount($account);
    $this->session
      ->set('uid', $account
      ->id());

    // Call all login hooks when switching to masquerading user.
    if (empty($context_silent_user_switching)) {
      $this->moduleHandler
        ->invokeAll('user_login', [
        $account,
      ]);
    }
    $this->logger
      ->info('User was successfully switched to "@user" on IP = "@ip"', [
      '@user' => $name,
      '@ip' => $this
        ->getIP(),
    ]);
  }
  protected function getIP() {
    return $_SERVER['REMOTE_ADDR'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextUserAuth::$account protected property The current user.
ContextUserAuth::$logger protected property
ContextUserAuth::$session protected property The session.
ContextUserAuth::$sessionManager protected property The session manager service.
ContextUserAuth::$userStorage protected property The user storage.
ContextUserAuth::getCookies public function Returns cookies of the needed user.
ContextUserAuth::getCurrentAccount public function
ContextUserAuth::getIP protected function
ContextUserAuth::switchUser public function Switches to a different user.
ContextUserAuth::__construct public function Constructs a new SwitchUserController object