You are here

function shortcut_set_switch_access in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/shortcut/shortcut.module \shortcut_set_switch_access()
  2. 7 modules/shortcut/shortcut.module \shortcut_set_switch_access()
  3. 9 core/modules/shortcut/shortcut.module \shortcut_set_switch_access()

Access callback for switching the shortcut set assigned to a user account.

Parameters

object $account: (optional) The user account whose shortcuts will be switched. If not set, permissions will be checked for switching the logged-in user's own shortcut set.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

2 calls to shortcut_set_switch_access()
shortcut_preprocess_page_title in core/modules/shortcut/shortcut.module
Implements hook_preprocess_HOOK() for page title templates.
SwitchShortcutSet::checkAccess in core/modules/shortcut/src/Form/SwitchShortcutSet.php
Checks access for the shortcut set switch form.
3 string references to 'shortcut_set_switch_access'
drupal7.php in core/modules/tracker/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.
drupal7.php in core/modules/rdf/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.
drupal7.php in core/modules/migrate_drupal/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.

File

core/modules/shortcut/shortcut.module, line 88
Allows users to manage customizable lists of shortcut links.

Code

function shortcut_set_switch_access($account = NULL) {
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('administer shortcuts')) {

    // Administrators can switch anyone's shortcut set.
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  if (!$user
    ->hasPermission('access shortcuts')) {

    // The user has no permission to use shortcuts.
    return AccessResult::neutral()
      ->cachePerPermissions();
  }
  if (!$user
    ->hasPermission('switch shortcut sets')) {

    // The user has no permission to switch anyone's shortcut set.
    return AccessResult::neutral()
      ->cachePerPermissions();
  }

  // Users with the 'switch shortcut sets' permission can switch their own
  // shortcuts sets.
  if (!isset($account)) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  elseif ($user
    ->id() == $account
    ->id()) {
    return AccessResult::allowed()
      ->cachePerPermissions()
      ->cachePerUser();
  }

  // No opinion.
  return AccessResult::neutral()
    ->cachePerPermissions();
}