Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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     $permissions["access {$entity_type_id} overview"] = [
60       'title' => $this->t('Access the @type overview page', ['@type' => $plural_label]),
61     ];
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,
66         ]),
67       ];
68     }
69
70     // Generate the other permissions based on granularity.
71     if ($entity_type->getPermissionGranularity() === 'entity_type') {
72       $permissions += $this->buildEntityTypePermissions($entity_type);
73     }
74     else {
75       $permissions += $this->buildBundlePermissions($entity_type);
76     }
77
78     return $this->processPermissions($permissions, $entity_type);
79   }
80
81   /**
82    * Adds the provider and converts the titles to strings to allow sorting.
83    *
84    * @param array $permissions
85    *   The array of permissions
86    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
87    *   The entity type.
88    *
89    * @return array
90    *   An array of processed permissions.
91    */
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'];
98     }
99     return $permissions;
100   }
101
102   /**
103    * Builds permissions for the entity_type granularity.
104    *
105    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
106    *   The entity type.
107    *
108    * @return array
109    *   The permissions.
110    */
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();
116
117     $permissions = [];
118     $permissions["create {$entity_type_id}"] = [
119       'title' => $this->t('Create @type', [
120         '@type' => $plural_label,
121       ]),
122     ];
123     if ($has_owner) {
124       $permissions["update any {$entity_type_id}"] = [
125         'title' => $this->t('Update any @type', [
126           '@type' => $singular_label,
127         ]),
128       ];
129       $permissions["update own {$entity_type_id}"] = [
130         'title' => $this->t('Update own @type', [
131           '@type' => $plural_label,
132         ]),
133       ];
134       $permissions["delete any {$entity_type_id}"] = [
135         'title' => $this->t('Delete any @type', [
136           '@type' => $singular_label,
137         ]),
138       ];
139       $permissions["delete own {$entity_type_id}"] = [
140         'title' => $this->t('Delete own @type', [
141           '@type' => $plural_label,
142         ]),
143       ];
144     }
145     else {
146       $permissions["update {$entity_type_id}"] = [
147         'title' => $this->t('Update @type', [
148           '@type' => $plural_label,
149         ]),
150       ];
151       $permissions["delete {$entity_type_id}"] = [
152         'title' => $this->t('Delete @type', [
153           '@type' => $plural_label,
154         ]),
155       ];
156     }
157
158     return $permissions;
159   }
160
161   /**
162    * Builds permissions for the bundle granularity.
163    *
164    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
165    *   The entity type.
166    *
167    * @return array
168    *   The permissions.
169    */
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();
176
177     $permissions = [];
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,
183         ]),
184       ];
185
186       if ($has_owner) {
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,
191           ]),
192         ];
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,
197           ]),
198         ];
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,
203           ]),
204         ];
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,
209           ]),
210         ];
211       }
212       else {
213         $permissions["update {$bundle_name} {$entity_type_id}"] = [
214           'title' => $this->t('@bundle: Update @type', [
215             '@bundle' => $bundle_info['label'],
216             '@type' => $plural_label,
217           ]),
218         ];
219         $permissions["delete {$bundle_name} {$entity_type_id}"] = [
220           'title' => $this->t('@bundle: Delete @type', [
221             '@bundle' => $bundle_info['label'],
222             '@type' => $plural_label,
223           ]),
224         ];
225       }
226     }
227
228     return $permissions;
229   }
230
231 }