forked from drupal/drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockContentAccessControlHandler.php
More file actions
43 lines (38 loc) · 1.53 KB
/
BlockContentAccessControlHandler.php
File metadata and controls
43 lines (38 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Drupal\block_content;
use Drupal\Core\Access\AccessDependentInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Session\AccountInterface;
/**
* Defines the access control handler for the custom block entity type.
*
* @see \Drupal\block_content\Entity\BlockContent
*/
class BlockContentAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'view') {
$access = AccessResult::allowedIf($entity->isPublished())->addCacheableDependency($entity)
->orIf(AccessResult::allowedIfHasPermission($account, 'administer blocks'));
}
else {
$access = parent::checkAccess($entity, $operation, $account);
}
/** @var \Drupal\block_content\BlockContentInterface $entity */
if ($entity->isReusable() === FALSE) {
if (!$entity instanceof AccessDependentInterface) {
throw new \LogicException("Non-reusable block entities must implement \Drupal\Core\Access\AccessDependentInterface for access control.");
}
$dependency = $entity->getAccessDependency();
if (empty($dependency)) {
return AccessResult::forbidden("Non-reusable blocks must set an access dependency for access control.");
}
$access->andIf($dependency->access($operation, $account, TRUE))->addCacheableDependency($access);
}
return $access;
}
}