20766528b774373a1fdb5a2aa673ab1654ce1e5b
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / src / EntityTestAccessControlHandler.php
1 <?php
2
3 namespace Drupal\entity_test;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityAccessControlHandler;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\entity_test\Entity\EntityTestLabel;
10
11 /**
12  * Defines the access control handler for the test entity type.
13  *
14  * @see \Drupal\entity_test\Entity\EntityTest
15  * @see \Drupal\entity_test\Entity\EntityTestBaseFieldDisplay
16  * @see \Drupal\entity_test\Entity\EntityTestCache
17  * @see \Drupal\entity_test\Entity\EntityTestMul
18  * @see \Drupal\entity_test\Entity\EntityTestMulRev
19  * @see \Drupal\entity_test\Entity\EntityTestRev
20  * @see \Drupal\entity_test\Entity\EntityTestWithBundle
21  * @see \Drupal\entity_test\Entity\EntityTestStringId
22  */
23 class EntityTestAccessControlHandler extends EntityAccessControlHandler {
24
25   /**
26    * Allows to grant access to just the labels.
27    *
28    * @var bool
29    */
30   protected $viewLabelOperation = TRUE;
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
36     /** @var \Drupal\entity_test\Entity\EntityTest $entity */
37
38     // Always forbid access to entities with the label 'forbid_access', used for
39     // \Drupal\system\Tests\Entity\EntityAccessControlHandlerTest::testDefaultEntityAccess().
40     if ($entity->label() == 'forbid_access') {
41       return AccessResult::forbidden();
42     }
43
44     if ($operation === 'view label' && $entity instanceof EntityTestLabel) {
45       // Viewing the label of the 'entity_test_label' entity type is allowed.
46       return AccessResult::allowed();
47     }
48     elseif (in_array($operation, ['view', 'view label'])) {
49       if (!$entity->isDefaultTranslation()) {
50         return AccessResult::allowedIfHasPermission($account, 'view test entity translations');
51       }
52       return AccessResult::allowedIfHasPermission($account, 'view test entity');
53     }
54     elseif (in_array($operation, ['update', 'delete'])) {
55       return AccessResult::allowedIfHasPermission($account, 'administer entity_test content');
56     }
57
58     // No opinion.
59     return AccessResult::neutral();
60
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
67     return AccessResult::allowedIfHasPermissions($account, [
68       'administer entity_test content',
69       'administer entity_test_with_bundle content',
70       'create ' . $entity_bundle . ' entity_test_with_bundle entities',
71     ], 'OR');
72   }
73
74 }