You are here

class AddressBookAccessCheck in Commerce Core 8.2

Checks address book access.

Intended to be combined with more specific (overview / CRUD) access checks.

Requirements key: '_address_book_access'.

Hierarchy

Expanded class hierarchy of AddressBookAccessCheck

1 string reference to 'AddressBookAccessCheck'
commerce_order.services.yml in modules/order/commerce_order.services.yml
modules/order/commerce_order.services.yml
1 service uses AddressBookAccessCheck
access_check.address_book in modules/order/commerce_order.services.yml
Drupal\commerce_order\Access\AddressBookAccessCheck

File

modules/order/src/Access/AddressBookAccessCheck.php, line 17

Namespace

Drupal\commerce_order\Access
View source
class AddressBookAccessCheck implements AccessInterface {

  /**
   * The address book.
   *
   * @var \Drupal\commerce_order\AddressBookInterface
   */
  protected $addressBook;

  /**
   * Constructs a new AddressBookAccessCheck object.
   *
   * @param \Drupal\commerce_order\AddressBookInterface $address_book
   *   The address book.
   */
  public function __construct(AddressBookInterface $address_book) {
    $this->addressBook = $address_book;
  }

  /**
   * Checks address book access.
   *
   * Ensures that the user is logged in, and the address book UI is exposed.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account) {
    if ($account
      ->isAnonymous()) {

      // Anonymous users can't have an address book.
      return AccessResult::forbidden()
        ->addCacheContexts([
        'user.roles:authenticated',
      ]);
    }
    if (!$this->addressBook
      ->hasUi()) {

      // No UI available.
      return AccessResult::forbidden()
        ->addCacheTags([
        'config:profile_type_list',
      ]);
    }
    return AccessResult::allowed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddressBookAccessCheck::$addressBook protected property The address book.
AddressBookAccessCheck::access public function Checks address book access.
AddressBookAccessCheck::__construct public function Constructs a new AddressBookAccessCheck object.