3 namespace Drupal\entity;
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;
16 class EntityPermissionProviderBase implements EntityPermissionProviderInterface, EntityHandlerInterface {
18 use StringTranslationTrait;
21 * The entity type bundle info.
23 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
25 protected $entityTypeBundleInfo;
28 * Constructs a new EntityPermissionProvider object.
30 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
31 * The entity type bundle info.
33 public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info) {
34 $this->entityTypeBundleInfo = $entity_type_bundle_info;
40 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
42 $container->get('entity_type.bundle.info')
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();
55 $permissions["administer {$entity_type_id}"] = [
56 'title' => $this->t('Administer @type', ['@type' => $plural_label]),
57 'restrict access' => TRUE,
59 $permissions["access {$entity_type_id} overview"] = [
60 'title' => $this->t('Access the @type overview page', ['@type' => $plural_label]),
62 if ($has_owner && $entity_type->entityClassImplements(EntityPublishedInterface::class)) {
63 $permissions["view own unpublished {$entity_type_id}"] = [
64 'title' => $this->t('View own unpublished @type', [
65 '@type' => $plural_label,
70 // Generate the other permissions based on granularity.
71 if ($entity_type->getPermissionGranularity() === 'entity_type') {
72 $permissions += $this->buildEntityTypePermissions($entity_type);
75 $permissions += $this->buildBundlePermissions($entity_type);
78 return $this->processPermissions($permissions, $entity_type);
82 * Adds the provider and converts the titles to strings to allow sorting.
84 * @param array $permissions
85 * The array of permissions
86 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
90 * An array of processed permissions.
92 protected function processPermissions(array $permissions, EntityTypeInterface $entity_type) {
93 foreach ($permissions as $name => $permission) {
94 // Permissions are grouped by provider on admin/people/permissions.
95 $permissions[$name]['provider'] = $entity_type->getProvider();
96 // TranslatableMarkup objects don't sort properly.
97 $permissions[$name]['title'] = (string) $permission['title'];
103 * Builds permissions for the entity_type granularity.
105 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
111 protected function buildEntityTypePermissions(EntityTypeInterface $entity_type) {
112 $entity_type_id = $entity_type->id();
113 $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
114 $singular_label = $entity_type->getSingularLabel();
115 $plural_label = $entity_type->getPluralLabel();
118 $permissions["create {$entity_type_id}"] = [
119 'title' => $this->t('Create @type', [
120 '@type' => $plural_label,
124 $permissions["update any {$entity_type_id}"] = [
125 'title' => $this->t('Update any @type', [
126 '@type' => $singular_label,
129 $permissions["update own {$entity_type_id}"] = [
130 'title' => $this->t('Update own @type', [
131 '@type' => $plural_label,
134 $permissions["delete any {$entity_type_id}"] = [
135 'title' => $this->t('Delete any @type', [
136 '@type' => $singular_label,
139 $permissions["delete own {$entity_type_id}"] = [
140 'title' => $this->t('Delete own @type', [
141 '@type' => $plural_label,
146 $permissions["update {$entity_type_id}"] = [
147 'title' => $this->t('Update @type', [
148 '@type' => $plural_label,
151 $permissions["delete {$entity_type_id}"] = [
152 'title' => $this->t('Delete @type', [
153 '@type' => $plural_label,
162 * Builds permissions for the bundle granularity.
164 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
170 protected function buildBundlePermissions(EntityTypeInterface $entity_type) {
171 $entity_type_id = $entity_type->id();
172 $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
173 $has_owner = $entity_type->entityClassImplements(EntityOwnerInterface::class);
174 $singular_label = $entity_type->getSingularLabel();
175 $plural_label = $entity_type->getPluralLabel();
178 foreach ($bundles as $bundle_name => $bundle_info) {
179 $permissions["create {$bundle_name} {$entity_type_id}"] = [
180 'title' => $this->t('@bundle: Create @type', [
181 '@bundle' => $bundle_info['label'],
182 '@type' => $plural_label,
187 $permissions["update any {$bundle_name} {$entity_type_id}"] = [
188 'title' => $this->t('@bundle: Update any @type', [
189 '@bundle' => $bundle_info['label'],
190 '@type' => $singular_label,
193 $permissions["update own {$bundle_name} {$entity_type_id}"] = [
194 'title' => $this->t('@bundle: Update own @type', [
195 '@bundle' => $bundle_info['label'],
196 '@type' => $plural_label,
199 $permissions["delete any {$bundle_name} {$entity_type_id}"] = [
200 'title' => $this->t('@bundle: Delete any @type', [
201 '@bundle' => $bundle_info['label'],
202 '@type' => $singular_label,
205 $permissions["delete own {$bundle_name} {$entity_type_id}"] = [
206 'title' => $this->t('@bundle: Delete own @type', [
207 '@bundle' => $bundle_info['label'],
208 '@type' => $plural_label,
213 $permissions["update {$bundle_name} {$entity_type_id}"] = [
214 'title' => $this->t('@bundle: Update @type', [
215 '@bundle' => $bundle_info['label'],
216 '@type' => $plural_label,
219 $permissions["delete {$bundle_name} {$entity_type_id}"] = [
220 'title' => $this->t('@bundle: Delete @type', [
221 '@bundle' => $bundle_info['label'],
222 '@type' => $plural_label,