You are here

class PaymentMethodAccessCheck in Commerce Core 8.2

Checks access for payment method routes.

Hierarchy

Expanded class hierarchy of PaymentMethodAccessCheck

See also

\Drupal\Core\Access\CustomAccessCheck

File

modules/payment/src/Access/PaymentMethodAccessCheck.php, line 14

Namespace

Drupal\commerce_payment\Access
View source
class PaymentMethodAccessCheck {

  /**
   * Checks access.
   *
   * Confirms that the user either has the 'administer commerce_payment_method'
   * permission, or the 'manage own commerce_payment_method' permission while
   * visiting their own payment method pages.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user account.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   */
  public function checkAccess(RouteMatchInterface $route_match, AccountInterface $account) {
    if ($account
      ->isAnonymous()) {

      // Anonymous users can't manage their payment methods.
      return AccessResult::forbidden()
        ->addCacheContexts([
        'user.roles:authenticated',
      ]);
    }
    $result = AccessResult::allowedIfHasPermissions($account, [
      'administer commerce_payment_method',
    ]);
    $current_user = $route_match
      ->getParameter('user');
    if ($result
      ->isNeutral() && $current_user
      ->id() == $account
      ->id()) {
      $result = AccessResult::allowedIfHasPermissions($account, [
        'manage own commerce_payment_method',
      ])
        ->cachePerUser();
    }
    return $result;
  }

}

Members