FilterFormatAccessControlHandler.php 1.56 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\filter\FilterFormatAccessControlHandler.
6 7 8 9
 */

namespace Drupal\filter;

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

/**
16 17 18
 * Defines the access control handler for the filter format entity type.
 *
 * @see \Drupal\filter\Entity\FilterFormat
19
 */
20
class FilterFormatAccessControlHandler extends EntityAccessControlHandler {
21 22 23 24

  /**
   * {@inheritdoc}
   */
25
  protected function checkAccess(EntityInterface $filter_format, $operation, AccountInterface $account) {
26 27 28 29
    /** @var \Drupal\filter\FilterFormatInterface $filter_format */

    // All users are allowed to use the fallback filter.
    if ($operation == 'use') {
30 31 32 33 34 35
      if ($filter_format->isFallbackFormat()) {
        return AccessResult::allowed();
      }
      else {
        return AccessResult::allowedIfHasPermission($account, $filter_format->getPermissionName());
      }
36 37 38 39
    }

    // The fallback format may not be disabled.
    if ($operation == 'disable' && $filter_format->isFallbackFormat()) {
40
      return AccessResult::forbidden();
41
    }
42

43 44 45
    // We do not allow filter formats to be deleted through the UI, because that
    // would render any content that uses them unusable.
    if ($operation == 'delete') {
46
      return AccessResult::forbidden();
47
    }
48

49
    if (in_array($operation, array('disable', 'update'))) {
50
      return parent::checkAccess($filter_format, $operation, $account);
51
    }
52 53

    // No opinion.
54
    return AccessResult::neutral();
55 56 57
  }

}