class WishlistItemDetailsAccessCheck in Commerce Wishlist 8.3
Access check for the wishlist item details_form route.
Hierarchy
- class \Drupal\commerce_wishlist\Access\WishlistItemDetailsAccessCheck implements AccessInterface
Expanded class hierarchy of WishlistItemDetailsAccessCheck
1 string reference to 'WishlistItemDetailsAccessCheck'
1 service uses WishlistItemDetailsAccessCheck
File
- src/
Access/ WishlistItemDetailsAccessCheck.php, line 14
Namespace
Drupal\commerce_wishlist\AccessView source
class WishlistItemDetailsAccessCheck implements AccessInterface {
/**
* The wishlist session.
*
* @var \Drupal\commerce_wishlist\WishlistSessionInterface
*/
protected $wishlistSession;
/**
* Constructs a new WishlistItemDetailsAccessCheck object.
*
* @param \Drupal\commerce_wishlist\WishlistSessionInterface $wishlist_session
* The wishlist session.
*/
public function __construct(WishlistSessionInterface $wishlist_session) {
$this->wishlistSession = $wishlist_session;
}
/**
* Checks access to the wishlist item details form.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(RouteMatchInterface $route_match, AccountInterface $account) {
if ($account
->hasPermission('administer commerce_wishlist')) {
// Administrators can modify anyone's wishlst.
$access = AccessResult::allowed()
->cachePerPermissions();
}
else {
// Users can modify their own wishlists.
/** @var \Drupal\commerce_wishlist\Entity\WishlistItemInterface $wishlist_item */
$wishlist_item = $route_match
->getParameter('commerce_wishlist_item');
$user = $wishlist_item
->getWishlist()
->getOwner();
if ($account
->isAuthenticated()) {
$access = AccessResult::allowedIf($user
->id() === $account
->id())
->addCacheableDependency($wishlist_item)
->cachePerUser();
}
else {
$access = AccessResult::allowedIf($this->wishlistSession
->hasWishlistId($wishlist_item
->getWishlistId()))
->addCacheableDependency($wishlist_item)
->addCacheContexts([
'wishlist',
]);
}
}
return $access;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
WishlistItemDetailsAccessCheck:: |
protected | property | The wishlist session. | |
WishlistItemDetailsAccessCheck:: |
public | function | Checks access to the wishlist item details form. | |
WishlistItemDetailsAccessCheck:: |
public | function | Constructs a new WishlistItemDetailsAccessCheck object. |