Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / EntityPermissionProviderBase.php
1 <?php
2
3 namespace Drupal\entity;
4
5 use Drupal\Core\Entity\EntityHandlerInterface;
6 use Drupal\Core\Entity\EntityPublishedInterface;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\user\EntityOwnerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * @internal
15  */
16 class EntityPermissionProviderBase implements EntityPermissionProviderInterface, EntityHandlerInterface {
17
18   use StringTranslationTrait;
19
20   /**
21    * The entity type bundle info.
22    *
23    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
24    */
25   protected $entityTypeBundleInfo;
26
27   /**
28    * Constructs a new EntityPermissionProvider object.
29    *
30    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
31    *   The entity type bundle info.
32    */
33   public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info) {
34     $this->entityTypeBundleInfo = $entity_type_bundle_info;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
41     return new static(
42       $container->get('entity_type.bundle.info')
43     );
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function buildPermissions(EntityTypeInterface $entity_type) {
50     $entity_type_id = $entity_type->id();
51     $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
52     $plural_label = $entity_type->getPluralLabel();
53
54     $permissions = [];
55     $permissions["administer {$entity_type_id}"] = [
56       'title' => $this->t('Administer @type', ['@type' => $plural_label]),
57       'restrict access' => TRUE,
58     ];
59     if ($entity_type->hasLinkTemplate('collection')) {
60       $permissions["access {$entity_type_id} overview"] = [
61         'title' => $this->t('Access the @type overview page', ['@type' => $plural_label]),
62       ];
63     }
64     if ($has_owner && $entity_type->entityClassImplements(EntityPublishedInterface::class)) {
65       $permissions["view own unpublished {$entity_type_id}"] = [
66         'title' => $this->t('View own unpublished @type', [
67           '@type' => $plural_label,
68         ]),
69       ];
70     }
71
72     // Generate the other permissions based on granularity.
73     if ($entity_type->getPermissionGranularity() === 'entity_type') {
74       $permissions += $this->buildEntityTypePermissions($entity_type);
75     }
76     else {
77       $permissions += $this->buildBundlePermissions($entity_type);
78     }
79
80     return $this->processPermissions($permissions, $entity_type);
81   }
82
83   /**
84    * Adds the provider and converts the titles to strings to allow sorting.
85    *
86    * @param array $permissions
87    *   The array of permissions.
88    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
89    *   The entity type.
90    *
91    * @return array
92    *   An array of processed permissions.
93    */
94   protected function processPermissions(array $permissions, EntityTypeInterface $entity_type) {
95     foreach ($permissions as $name => $permission) {
96       // Permissions are grouped by provider on admin/people/permissions.
97       $permissions[$name]['provider'] = $entity_type->getProvider();
98       // TranslatableMarkup objects don't sort properly.
99       $permissions[$name]['title'] = (string) $permission['title'];
100     }
101     return $permissions;
102   }
103
104   /**
105    * Builds permissions for the entity_type granularity.
106    *
107    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
108    *   The entity type.
109    *
110    * @return array
111    *   The permissions.
112    */
113   protected function buildEntityTypePermissions(EntityTypeInterface $entity_type) {
114     $entity_type_id = $entity_type->id();
115     $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
116     $singular_label = $entity_type->getSingularLabel();
117     $plural_label = $entity_type->getPluralLabel();
118
119     $permissions = [];
120     $permissions["create {$entity_type_id}"] = [
121       'title' => $this->t('Create @type', [
122         '@type' => $plural_label,
123       ]),
124     ];
125     if ($has_owner) {
126       $permissions["update any {$entity_type_id}"] = [
127         'title' => $this->t('Update any @type', [
128           '@type' => $singular_label,
129         ]),
130       ];
131       $permissions["update own {$entity_type_id}"] = [
132         'title' => $this->t('Update own @type', [
133           '@type' => $plural_label,
134         ]),
135       ];
136       $permissions["delete any {$entity_type_id}"] = [
137         'title' => $this->t('Delete any @type', [
138           '@type' => $singular_label,
139         ]),
140       ];
141       $permissions["delete own {$entity_type_id}"] = [
142         'title' => $this->t('Delete own @type', [
143           '@type' => $plural_label,
144         ]),
145       ];
146     }
147     else {
148       $permissions["update {$entity_type_id}"] = [
149         'title' => $this->t('Update @type', [
150           '@type' => $plural_label,
151         ]),
152       ];
153       $permissions["delete {$entity_type_id}"] = [
154         'title' => $this->t('Delete @type', [
155           '@type' => $plural_label,
156         ]),
157       ];
158     }
159
160     return $permissions;
161   }
162
163   /**
164    * Builds permissions for the bundle granularity.
165    *
166    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
167    *   The entity type.
168    *
169    * @return array
170    *   The permissions.
171    */
172   protected function buildBundlePermissions(EntityTypeInterface $entity_type) {
173     $entity_type_id = $entity_type->id();
174     $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
175     $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
176     $singular_label = $entity_type->getSingularLabel();
177     $plural_label = $entity_type->getPluralLabel();
178
179     $permissions = [];
180     foreach ($bundles as $bundle_name => $bundle_info) {
181       $permissions["create {$bundle_name} {$entity_type_id}"] = [
182         'title' => $this->t('@bundle: Create @type', [
183           '@bundle' => $bundle_info['label'],
184           '@type' => $plural_label,
185         ]),
186       ];
187
188       if ($has_owner) {
189         $permissions["update any {$bundle_name} {$entity_type_id}"] = [
190           'title' => $this->t('@bundle: Update any @type', [
191             '@bundle' => $bundle_info['label'],
192             '@type' => $singular_label,
193           ]),
194         ];
195         $permissions["update own {$bundle_name} {$entity_type_id}"] = [
196           'title' => $this->t('@bundle: Update own @type', [
197             '@bundle' => $bundle_info['label'],
198             '@type' => $plural_label,
199           ]),
200         ];
201         $permissions["delete any {$bundle_name} {$entity_type_id}"] = [
202           'title' => $this->t('@bundle: Delete any @type', [
203             '@bundle' => $bundle_info['label'],
204             '@type' => $singular_label,
205           ]),
206         ];
207         $permissions["delete own {$bundle_name} {$entity_type_id}"] = [
208           'title' => $this->t('@bundle: Delete own @type', [
209             '@bundle' => $bundle_info['label'],
210             '@type' => $plural_label,
211           ]),
212         ];
213       }
214       else {
215         $permissions["update {$bundle_name} {$entity_type_id}"] = [
216           'title' => $this->t('@bundle: Update @type', [
217             '@bundle' => $bundle_info['label'],
218             '@type' => $plural_label,
219           ]),
220         ];
221         $permissions["delete {$bundle_name} {$entity_type_id}"] = [
222           'title' => $this->t('@bundle: Delete @type', [
223             '@bundle' => $bundle_info['label'],
224             '@type' => $plural_label,
225           ]),
226         ];
227       }
228     }
229
230     return $permissions;
231   }
232
233 }