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.
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
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).
27 * Intended for content entity types, since config entity types usually rely
28 * on a single "administer" permission.
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 any {$entity_type_id}"] = [
72 'title' => $this->t('View any @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();
97 $permissions["view any {$entity_type_id}"] = [
98 'title' => $this->t('View any @type', [
99 '@type' => $plural_label,
103 $permissions["view own {$entity_type_id}"] = [
104 'title' => $this->t('View own @type', [
105 '@type' => $plural_label,
110 foreach ($bundles as $bundle_name => $bundle_info) {
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,
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,
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,