3 namespace Drupal\entity;
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\user\EntityOwnerInterface;
9 * Provides generic entity permissions which are cached per user.
11 * Intended for content entity types, since config entity types usually rely
12 * on a single "administer" permission.
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
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.
32 * "access" = "Drupal\entity\UncacheableEntityAccessControlHandler",
33 * "permission_provider" = "Drupal\entity\UncacheableEntityPermissionProvider",
37 * @see \Drupal\entity\EntityAccessControlHandler
38 * @see \Drupal\entity\EntityPermissions
40 class UncacheableEntityPermissionProvider extends EntityPermissionProviderBase {
43 * Builds permissions for the entity_type granularity.
45 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
51 protected function buildEntityTypePermissions(EntityTypeInterface $entity_type) {
52 $permissions = parent::buildEntityTypePermissions($entity_type);
54 $entity_type_id = $entity_type->id();
55 $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
56 $plural_label = $entity_type->getPluralLabel();
59 $permissions["view any {$entity_type_id}"] = [
60 'title' => $this->t('View any @type', [
61 '@type' => $plural_label,
64 $permissions["view own {$entity_type_id}"] = [
65 'title' => $this->t('View own @type', [
66 '@type' => $plural_label,
71 $permissions["view {$entity_type_id}"] = [
72 'title' => $this->t('View @type', [
73 '@type' => $plural_label,
82 * Builds permissions for the bundle granularity.
84 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
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();
98 $permissions["view any {$entity_type_id}"] = [
99 'title' => $this->t('View any @type', [
100 '@type' => $plural_label,
103 $permissions["view own {$entity_type_id}"] = [
104 'title' => $this->t('View own @type', [
105 '@type' => $plural_label,
110 $permissions["view {$entity_type_id}"] = [
111 'title' => $this->t('View @type', [
112 '@type' => $plural_label,
117 foreach ($bundles as $bundle_name => $bundle_info) {
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,
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,
133 $permissions["view {$bundle_name} {$entity_type_id}"] = [
134 'title' => $this->t('@bundle: View @type', [
135 '@bundle' => $bundle_info['label'],
136 '@type' => $plural_label,