You are here

CountryListAccessControlHandler.php in Country, State and City Fields 8

File

src/CountryListAccessControlHandler.php
View source
<?php

namespace Drupal\country_state_city;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Access controller for the countrylist entity.
 */
class CountryListAccessControlHandler extends EntityAccessControlHandler {

  /**
   * {@inheritdoc}
   *
   * Link the activities to the permissions. checkAccess() is called with the
   * $operation as defined in the routing.yml file.
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

    // Check the admin_permission as defined in your @ContentEntityType
    // annotation.
    $admin_permission = $this->entityType
      ->getAdminPermission();
    if ($account
      ->hasPermission($admin_permission)) {
      return AccessResult::allowed();
    }
    switch ($operation) {
      case 'view':
        return AccessResult::allowedIfHasPermission($account, 'view countrylist entity');
      case 'update':
        return AccessResult::allowedIfHasPermission($account, 'edit countrylist entity');
      case 'delete':
        return AccessResult::allowedIfHasPermission($account, 'delete countrylist entity');
    }
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   *
   * Separate from the checkAccess because the entity does not yet exist. It
   * will be created during the 'add' process.
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {

    // Check the admin_permission as defined in your @ContentEntityType
    // annotation.
    $admin_permission = $this->entityType
      ->getAdminPermission();
    if ($account
      ->hasPermission($admin_permission)) {
      return AccessResult::allowed();
    }
    return AccessResult::allowedIfHasPermission($account, 'add countrylist entity');
  }

}

Classes

Namesort descending Description
CountryListAccessControlHandler Access controller for the countrylist entity.