ShortcutSetAccessControlHandler.php 1.71 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\shortcut\ShortcutSetAccessControlHandler.
6 7 8 9
 */

namespace Drupal\shortcut;

10
use Drupal\Core\Access\AccessResult;
11
use Drupal\Core\Entity\EntityInterface;
12
use Drupal\Core\Entity\EntityAccessControlHandler;
13
use Drupal\Core\Session\AccountInterface;
14 15

/**
16 17 18
 * Defines the access control handler for the shortcut set entity type.
 *
 * @see \Drupal\shortcut\Entity\ShortcutSet
19
 */
20
class ShortcutSetAccessControlHandler extends EntityAccessControlHandler {
21 22 23 24

  /**
   * {@inheritdoc}
   */
25
  protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) {
26
    switch ($operation) {
27
      case 'update':
28
        if ($account->hasPermission('administer shortcuts')) {
29
          return AccessResult::allowed()->cachePerPermissions();
30
        }
31
        if (!$account->hasPermission('access shortcuts')) {
32
          return AccessResult::neutral()->cachePerPermissions();
33
        }
34
        return AccessResult::allowedIf($account->hasPermission('customize shortcut links') && $entity == shortcut_current_displayed_set($account))->cachePerPermissions()->cacheUntilEntityChanges($entity);
35

36
      case 'delete':
37
        return AccessResult::allowedIf($account->hasPermission('administer shortcuts') && $entity->id() != 'default')->cachePerPermissions();
38 39 40

      default:
        // No opinion.
41
        return AccessResult::neutral();
42 43
    }
  }
44

45
  /**
46
   * {@inheritdoc}
47 48
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
49
    return AccessResult::allowedIfHasPermission($account, 'administer shortcuts')->orIf(AccessResult::allowedIfHasPermissions($account, ['access shortcuts', 'customize shortcut links'], 'AND'));
50 51
  }

52
}