You are here

class BookingAddAccessCheck in Booking and Availability Management Tools for Drupal 8

Determines access to for unit add pages.

Hierarchy

Expanded class hierarchy of BookingAddAccessCheck

1 string reference to 'BookingAddAccessCheck'
bat_booking.services.yml in modules/bat_booking/bat_booking.services.yml
modules/bat_booking/bat_booking.services.yml
1 service uses BookingAddAccessCheck
access_check.bat_booking.add in modules/bat_booking/bat_booking.services.yml
Drupal\bat_booking\Access\BookingAddAccessCheck

File

modules/bat_booking/src/Access/BookingAddAccessCheck.php, line 19
Contains \Drupal\bat_booking\Access\BookingAddAccessCheck.

Namespace

Drupal\bat_booking\Access
View source
class BookingAddAccessCheck 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 unit add page for the unit type.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   * @param \Drupal\bat_booking\BookingBundleInterface $booking_bundle
   *   (optional) The unit bundle. If not specified, access is allowed if there
   *   exists at least one unit bundle for which the user may create a unit.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account, BookingBundleInterface $booking_bundle = NULL) {
    $access_control_handler = $this->entityTypeManager
      ->getAccessControlHandler('bat_booking');
    if ($account
      ->hasPermission('administer bat_booking_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 ($booking_bundle) {
      return $access_control_handler
        ->createAccess($booking_bundle
        ->id(), $account, [], TRUE);
    }
    $bundles = bat_booking_get_bundles();
    foreach ($bundles as $bundle) {
      if (bat_booking_access(bat_booking_create([
        'type' => $bundle
          ->id(),
        'uid' => 0,
      ]), 'create', $account
        ->getAccount())
        ->isAllowed()) {
        return AccessResult::allowed();
      }
    }
    return AccessResult::forbidden();
  }

}

Members

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