Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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  * This includes:
12  *
13  * - administer $entity_type
14  * - access $entity_type overview
15  * - view an ($bundle) $entity_type
16  * - view own ($bundle) $entity_type
17  * - view own unpublished $entity_type
18  * - update (own|any) ($bundle) $entity_type
19  * - delete (own|any) ($bundle) $entity_type
20  * - create $bundle $entity_type
21  *
22  * As this class supports "view own ($bundle) $entity_type" it is just cacheable
23  * per user, which might harm performance of sites. Given that please use
24  * \Drupal\entity\EntityPermissionProvider unless you need the feature, or your
25  * entity type is not really user facing (commerce orders for example).
26  *
27  * Intended for content entity types, since config entity types usually rely
28  * on a single "administer" permission.
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 any {$entity_type_id}"] = [
72         'title' => $this->t('View any @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     $permissions["view any {$entity_type_id}"] = [
98       'title' => $this->t('View any @type', [
99         '@type' => $plural_label,
100       ]),
101     ];
102     if ($has_owner) {
103       $permissions["view own {$entity_type_id}"] = [
104         'title' => $this->t('View own @type', [
105           '@type' => $plural_label,
106         ]),
107       ];
108     }
109
110     foreach ($bundles as $bundle_name => $bundle_info) {
111       if ($has_owner) {
112         $permissions["view any {$bundle_name} {$entity_type_id}"] = [
113           'title' => $this->t('@bundle: View any @type', [
114             '@bundle' => $bundle_info['label'],
115             '@type' => $plural_label,
116           ]),
117         ];
118         $permissions["view own {$bundle_name} {$entity_type_id}"] = [
119           'title' => $this->t('@bundle: View own @type', [
120             '@bundle' => $bundle_info['label'],
121             '@type' => $plural_label,
122           ]),
123         ];
124       }
125       else {
126         $permissions["view any {$bundle_name} {$entity_type_id}"] = [
127           'title' => $this->t('@bundle: View any @type', [
128             '@bundle' => $bundle_info['label'],
129             '@type' => $plural_label,
130           ]),
131         ];
132       }
133     }
134
135     return $permissions;
136   }
137
138 }