LibraryItemAccessControlHandler.php in Paragraphs 8
File
modules/paragraphs_library/src/LibraryItemAccessControlHandler.php
View source
<?php
namespace Drupal\paragraphs_library;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class LibraryItemAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $library_item, $operation, AccountInterface $account) {
$access = AccessResult::allowed()
->addCacheableDependency($library_item);
if ($operation === 'view' && !$library_item
->isPublished()) {
$access = $access
->andIf(AccessResult::allowedIfHasPermission($account, $this->entityType
->getAdminPermission()));
}
if ($operation === 'update') {
$access = $access
->andIf(AccessResult::allowedIfHasPermissions($account, [
'edit paragraph library item',
$this->entityType
->getAdminPermission(),
], 'OR'));
}
if ($operation === 'delete') {
$access = $access
->andIf(AccessResult::allowedIfHasPermission($account, $this->entityType
->getAdminPermission()));
}
if ($referenced_paragraph = $library_item->paragraphs->entity) {
$access = $access
->andIf($referenced_paragraph
->access($operation, $account, TRUE));
}
else {
$access = $access
->andIf(AccessResult::neutral());
}
return $access;
}
public function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'create paragraph library item')
->orIf(parent::checkCreateAccess($account, $context, $entity_bundle));
}
}