You are here

class TypeGroupAddAccessCheck in Booking and Availability Management Tools for Drupal 8

Determines access to for type group add pages.

Hierarchy

Expanded class hierarchy of TypeGroupAddAccessCheck

1 string reference to 'TypeGroupAddAccessCheck'
bat.services.yml in ./bat.services.yml
bat.services.yml
1 service uses TypeGroupAddAccessCheck
access_check.bat_type_group.add in ./bat.services.yml
Drupal\bat\Access\TypeGroupAddAccessCheck

File

src/Access/TypeGroupAddAccessCheck.php, line 19
Contains \Drupal\bat\Access\TypeGroupAddAccessCheck.

Namespace

Drupal\bat\Access
View source
class TypeGroupAddAccessCheck implements AccessInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a EntityCreateAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager) {
    $this->entityTypeManager = $entity_manager;
  }

  /**
   * Checks access to the type group add page for the type group bundle.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   * @param \Drupal\bat\TypeGroupBundleInterface $type_group_bundle
   *   (optional) The type group bundle. If not specified, access is allowed if there
   *   exists at least one type group bundle for which the user may create a type group.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account, TypeGroupBundleInterface $type_group_bundle = NULL) {
    $access_control_handler = $this->entityTypeManager
      ->getAccessControlHandler('bat_type_group');
    if ($account
      ->hasPermission('administer bat_type_group_bundle entities')) {

      // There are no type bundles defined that the user has permission to
      // create, but the user does have the permission to administer the content
      // types, so grant them access to the page anyway.
      return AccessResult::allowed();
    }
    if ($type_group_bundle) {
      return $access_control_handler
        ->createAccess($type_group_bundle
        ->id(), $account, [], TRUE);
    }
    $bundles = bat_type_group_get_bundles();
    foreach ($bundles as $bundle) {
      if (bat_entity_access(bat_type_group_create([
        'type' => $bundle
          ->id(),
        'uid' => 0,
      ]), 'create', $account
        ->getAccount())
        ->isAllowed()) {
        return AccessResult::allowed();
      }
    }
    return AccessResult::forbidden();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TypeGroupAddAccessCheck::$entityTypeManager protected property The entity manager.
TypeGroupAddAccessCheck::access public function Checks access to the type group add page for the type group bundle.
TypeGroupAddAccessCheck::__construct public function Constructs a EntityCreateAccessCheck object.