Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / UncacheableEntityPermissionProvider.php
1 <?php
2
3 namespace Drupal\entity;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\user\EntityOwnerInterface;
7
8 /**
9  * Provides generic entity permissions which are cached per user.
10  *
11  * Intended for content entity types, since config entity types usually rely
12  * on a single "administer" permission.
13  *
14  * Provided permissions:
15  * - administer $entity_type
16  * - access $entity_type overview
17  * - view own unpublished $entity_type
18  * - view (own|any) ($bundle) $entity_type
19  * - update (own|any) ($bundle) $entity_type
20  * - delete (own|any) ($bundle) $entity_type
21  * - create $bundle $entity_type
22  *
23  * Important:
24  * Provides "view own ($bundle) $entity_type" permissions, which require
25  * caching pages per user. This can significantly increase the size of caches,
26  * impacting site performance. Use \Drupal\entity\EntityPermissionProvider
27  * if those permissions are not necessary.
28  *
29  * Example annotation:
30  * @code
31  *  handlers = {
32  *    "access" = "Drupal\entity\UncacheableEntityAccessControlHandler",
33  *    "permission_provider" = "Drupal\entity\UncacheableEntityPermissionProvider",
34  *  }
35  * @endcode
36  *
37  * @see \Drupal\entity\EntityAccessControlHandler
38  * @see \Drupal\entity\EntityPermissions
39  */
40 class UncacheableEntityPermissionProvider extends EntityPermissionProviderBase {
41
42   /**
43    * Builds permissions for the entity_type granularity.
44    *
45    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
46    *   The entity type.
47    *
48    * @return array
49    *   The permissions.
50    */
51   protected function buildEntityTypePermissions(EntityTypeInterface $entity_type) {
52     $permissions = parent::buildEntityTypePermissions($entity_type);
53
54     $entity_type_id = $entity_type->id();
55     $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
56     $plural_label = $entity_type->getPluralLabel();
57
58     if ($has_owner) {
59       $permissions["view any {$entity_type_id}"] = [
60         'title' => $this->t('View any @type', [
61           '@type' => $plural_label,
62         ]),
63       ];
64       $permissions["view own {$entity_type_id}"] = [
65         'title' => $this->t('View own @type', [
66           '@type' => $plural_label,
67         ]),
68       ];
69     }
70     else {
71       $permissions["view {$entity_type_id}"] = [
72         'title' => $this->t('View @type', [
73           '@type' => $plural_label,
74         ]),
75       ];
76     }
77
78     return $permissions;
79   }
80
81   /**
82    * Builds permissions for the bundle granularity.
83    *
84    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
85    *   The entity type.
86    *
87    * @return array
88    *   The permissions.
89    */
90   protected function buildBundlePermissions(EntityTypeInterface $entity_type) {
91     $permissions = parent::buildBundlePermissions($entity_type);
92     $entity_type_id = $entity_type->id();
93     $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
94     $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
95     $plural_label = $entity_type->getPluralLabel();
96
97     if ($has_owner) {
98       $permissions["view any {$entity_type_id}"] = [
99         'title' => $this->t('View any @type', [
100           '@type' => $plural_label,
101         ]),
102       ];
103       $permissions["view own {$entity_type_id}"] = [
104         'title' => $this->t('View own @type', [
105           '@type' => $plural_label,
106         ]),
107       ];
108     }
109     else {
110       $permissions["view {$entity_type_id}"] = [
111         'title' => $this->t('View @type', [
112           '@type' => $plural_label,
113         ]),
114       ];
115     }
116
117     foreach ($bundles as $bundle_name => $bundle_info) {
118       if ($has_owner) {
119         $permissions["view any {$bundle_name} {$entity_type_id}"] = [
120           'title' => $this->t('@bundle: View any @type', [
121             '@bundle' => $bundle_info['label'],
122             '@type' => $plural_label,
123           ]),
124         ];
125         $permissions["view own {$bundle_name} {$entity_type_id}"] = [
126           'title' => $this->t('@bundle: View own @type', [
127             '@bundle' => $bundle_info['label'],
128             '@type' => $plural_label,
129           ]),
130         ];
131       }
132       else {
133         $permissions["view {$bundle_name} {$entity_type_id}"] = [
134           'title' => $this->t('@bundle: View @type', [
135             '@bundle' => $bundle_info['label'],
136             '@type' => $plural_label,
137           ]),
138         ];
139       }
140     }
141
142     return $permissions;
143   }
144
145 }