Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / BundleEntityAccessControlHandler.php
1 <?php
2
3 namespace Drupal\entity;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityAccessControlHandler as CoreEntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Session\AccountInterface;
9
10 /**
11  * Controls access to bundle entities.
12  *
13  * Allows the bundle entity label to be viewed if the account has
14  * access to view entities of that bundle.
15  */
16 class BundleEntityAccessControlHandler extends CoreEntityAccessControlHandler {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected $viewLabelOperation = TRUE;
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
27     if ($operation === 'view label') {
28       $bundle = $entity->id();
29       $entity_type_id = $this->entityType->getBundleOf();
30       $permissions = [
31         "administer $entity_type_id",
32         // View permissions provided by EntityPermissionProvider.
33         "view $entity_type_id",
34         "view $bundle $entity_type_id",
35         // View permissions provided by UncacheableEntityPermissionProvider.
36         "view own $entity_type_id",
37         "view any $entity_type_id",
38         "view own $bundle $entity_type_id",
39         "view any $bundle $entity_type_id",
40       ];
41
42       return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
43     }
44     else {
45       return parent::checkAccess($entity, $operation, $account);
46     }
47   }
48
49 }