class AdminAccountSwitcher

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/DefaultContent/AdminAccountSwitcher.php \Drupal\Core\DefaultContent\AdminAccountSwitcher

@internal This API is experimental.

Hierarchy

Expanded class hierarchy of AdminAccountSwitcher

1 file declares its use of AdminAccountSwitcher
AdminAccountSwitcherTest.php in core/tests/Drupal/KernelTests/Core/DefaultContent/AdminAccountSwitcherTest.php

File

core/lib/Drupal/Core/DefaultContent/AdminAccountSwitcher.php, line 16

Namespace

Drupal\Core\DefaultContent
View source
final class AdminAccountSwitcher implements AccountSwitcherInterface {
  public function __construct(private readonly AccountSwitcherInterface $decorated, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly bool $isSuperUserAccessEnabled) {
  }
  
  /**
   * Switches to an administrative account.
   *
   * This will switch to the first available account with a role that has the
   * `is_admin` flag. If there are no such roles, or no such users, this will
   * try to switch to user 1 if superuser access is enabled.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The account that was switched to.
   *
   * @throws \Drupal\Core\Access\AccessException
   *   Thrown if there are no users with administrative roles.
   */
  public function switchToAdministrator() : AccountInterface {
    $admin_roles = $this->entityTypeManager
      ->getStorage('user_role')
      ->getQuery()
      ->condition('is_admin', TRUE)
      ->execute();
    $user_storage = $this->entityTypeManager
      ->getStorage('user');
    if ($admin_roles) {
      $accounts = $user_storage->getQuery()
        ->accessCheck(FALSE)
        ->condition('roles', $admin_roles, 'IN')
        ->condition('status', 1)
        ->sort('uid')
        ->range(0, 1)
        ->execute();
    }
    else {
      $accounts = [];
    }
    $account = $user_storage->load(reset($accounts) ?: 1);
    assert($account instanceof AccountInterface);
    if (array_intersect($account->getRoles(), $admin_roles) || (int) $account->id() === 1 && $this->isSuperUserAccessEnabled) {
      $this->switchTo($account);
      return $account;
    }
    throw new AccessException("There are no user accounts with administrative roles.");
  }
  
  /**
   * {@inheritdoc}
   */
  public function switchTo(AccountInterface $account) : AccountSwitcherInterface {
    $this->decorated
      ->switchTo($account);
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  public function switchBack() : AccountSwitcherInterface {
    $this->decorated
      ->switchBack();
    return $this;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AdminAccountSwitcher::switchBack public function Reverts to a previous account after switching. Overrides AccountSwitcherInterface::switchBack
AdminAccountSwitcher::switchTo public function Safely switches to another account. Overrides AccountSwitcherInterface::switchTo
AdminAccountSwitcher::switchToAdministrator public function Switches to an administrative account.
AdminAccountSwitcher::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.