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 if ($entity_type->hasLinkTemplate('collection')) {
60 $permissions["access {$entity_type_id} overview"] = [
61 'title' => $this->t('Access the @type overview page', ['@type' => $plural_label]),
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,
72 // Generate the other permissions based on granularity.
73 if ($entity_type->getPermissionGranularity() === 'entity_type') {
74 $permissions += $this->buildEntityTypePermissions($entity_type);
77 $permissions += $this->buildBundlePermissions($entity_type);
80 return $this->processPermissions($permissions, $entity_type);
84 * Adds the provider and converts the titles to strings to allow sorting.
86 * @param array $permissions
87 * The array of permissions.
88 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
92 * An array of processed permissions.
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'];
105 * Builds permissions for the entity_type granularity.
107 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
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();
120 $permissions["create {$entity_type_id}"] = [
121 'title' => $this->t('Create @type', [
122 '@type' => $plural_label,
126 $permissions["update any {$entity_type_id}"] = [
127 'title' => $this->t('Update any @type', [
128 '@type' => $singular_label,
131 $permissions["update own {$entity_type_id}"] = [
132 'title' => $this->t('Update own @type', [
133 '@type' => $plural_label,
136 $permissions["delete any {$entity_type_id}"] = [
137 'title' => $this->t('Delete any @type', [
138 '@type' => $singular_label,
141 $permissions["delete own {$entity_type_id}"] = [
142 'title' => $this->t('Delete own @type', [
143 '@type' => $plural_label,
148 $permissions["update {$entity_type_id}"] = [
149 'title' => $this->t('Update @type', [
150 '@type' => $plural_label,
153 $permissions["delete {$entity_type_id}"] = [
154 'title' => $this->t('Delete @type', [
155 '@type' => $plural_label,
164 * Builds permissions for the bundle granularity.
166 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
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();
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,
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,
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,
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,
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,
215 $permissions["update {$bundle_name} {$entity_type_id}"] = [
216 'title' => $this->t('@bundle: Update @type', [
217 '@bundle' => $bundle_info['label'],
218 '@type' => $plural_label,
221 $permissions["delete {$bundle_name} {$entity_type_id}"] = [
222 'title' => $this->t('@bundle: Delete @type', [
223 '@bundle' => $bundle_info['label'],
224 '@type' => $plural_label,