Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\content_translation\Access;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Access\AccessResult;
|
Chris@5
|
6 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
|
Chris@5
|
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Routing\Access\AccessInterface;
|
Chris@0
|
9 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
10 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Access check for entity translation overview.
|
Chris@0
|
14 */
|
Chris@0
|
15 class ContentTranslationOverviewAccess implements AccessInterface {
|
Chris@5
|
16 use DeprecatedServicePropertyTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@5
|
19 * {@inheritdoc}
|
Chris@5
|
20 */
|
Chris@5
|
21 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
|
Chris@5
|
22
|
Chris@5
|
23 /**
|
Chris@5
|
24 * The entity type manager service.
|
Chris@0
|
25 *
|
Chris@5
|
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@0
|
27 */
|
Chris@5
|
28 protected $entityTypeManager;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Constructs a ContentTranslationOverviewAccess object.
|
Chris@0
|
32 *
|
Chris@5
|
33 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@5
|
34 * The entity type manager service.
|
Chris@0
|
35 */
|
Chris@5
|
36 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
|
Chris@5
|
37 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Checks access to the translation overview for the entity and bundle.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
44 * The parametrized route.
|
Chris@0
|
45 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
46 * The currently logged in account.
|
Chris@0
|
47 * @param string $entity_type_id
|
Chris@0
|
48 * The entity type ID.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@0
|
51 * The access result.
|
Chris@0
|
52 */
|
Chris@0
|
53 public function access(RouteMatchInterface $route_match, AccountInterface $account, $entity_type_id) {
|
Chris@0
|
54 /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
|
Chris@0
|
55 $entity = $route_match->getParameter($entity_type_id);
|
Chris@0
|
56 if ($entity && $entity->isTranslatable()) {
|
Chris@0
|
57 // Get entity base info.
|
Chris@0
|
58 $bundle = $entity->bundle();
|
Chris@0
|
59
|
Chris@0
|
60 // Get entity access callback.
|
Chris@5
|
61 $definition = $this->entityTypeManager->getDefinition($entity_type_id);
|
Chris@0
|
62 $translation = $definition->get('translation');
|
Chris@0
|
63 $access_callback = $translation['content_translation']['access_callback'];
|
Chris@0
|
64 $access = call_user_func($access_callback, $entity);
|
Chris@0
|
65 if ($access->isAllowed()) {
|
Chris@0
|
66 return $access;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 // Check "translate any entity" permission.
|
Chris@0
|
70 if ($account->hasPermission('translate any entity')) {
|
Chris@0
|
71 return AccessResult::allowed()->cachePerPermissions()->inheritCacheability($access);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 // Check per entity permission.
|
Chris@0
|
75 $permission = "translate {$entity_type_id}";
|
Chris@0
|
76 if ($definition->getPermissionGranularity() == 'bundle') {
|
Chris@0
|
77 $permission = "translate {$bundle} {$entity_type_id}";
|
Chris@0
|
78 }
|
Chris@0
|
79 return AccessResult::allowedIfHasPermission($account, $permission)->inheritCacheability($access);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 // No opinion.
|
Chris@0
|
83 return AccessResult::neutral();
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 }
|