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@0
|
6 use Drupal\Core\Entity\ContentEntityInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
9 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@0
|
10 use Drupal\Core\Routing\Access\AccessInterface;
|
Chris@0
|
11 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
12 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
13 use Symfony\Component\Routing\Route;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Access check for entity translation CRUD operation.
|
Chris@0
|
17 */
|
Chris@0
|
18 class ContentTranslationManageAccessCheck implements AccessInterface {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The entity type manager.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $entityManager;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The language manager.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $languageManager;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Constructs a ContentTranslationManageAccessCheck object.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
|
Chris@0
|
38 * The entity type manager.
|
Chris@0
|
39 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
40 * The language manager.
|
Chris@0
|
41 */
|
Chris@0
|
42 public function __construct(EntityManagerInterface $manager, LanguageManagerInterface $language_manager) {
|
Chris@0
|
43 $this->entityManager = $manager;
|
Chris@0
|
44 $this->languageManager = $language_manager;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Checks translation access for the entity and operation on the given route.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param \Symfony\Component\Routing\Route $route
|
Chris@0
|
51 * The route to check against.
|
Chris@0
|
52 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
53 * The parametrized route.
|
Chris@0
|
54 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
55 * The currently logged in account.
|
Chris@0
|
56 * @param string $source
|
Chris@0
|
57 * (optional) For a create operation, the language code of the source.
|
Chris@0
|
58 * @param string $target
|
Chris@0
|
59 * (optional) For a create operation, the language code of the translation.
|
Chris@0
|
60 * @param string $language
|
Chris@0
|
61 * (optional) For an update or delete operation, the language code of the
|
Chris@0
|
62 * translation being updated or deleted.
|
Chris@0
|
63 * @param string $entity_type_id
|
Chris@0
|
64 * (optional) The entity type ID.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@0
|
67 * The access result.
|
Chris@0
|
68 */
|
Chris@0
|
69 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL) {
|
Chris@0
|
70 /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
|
Chris@0
|
71 if ($entity = $route_match->getParameter($entity_type_id)) {
|
Chris@0
|
72 $operation = $route->getRequirement('_access_content_translation_manage');
|
Chris@0
|
73 $language = $this->languageManager->getLanguage($language) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
|
Chris@0
|
74 $entity_type = $this->entityManager->getDefinition($entity_type_id);
|
Chris@0
|
75
|
Chris@0
|
76 if (in_array($operation, ['update', 'delete'])) {
|
Chris@0
|
77 // Translation operations cannot be performed on the default
|
Chris@0
|
78 // translation.
|
Chris@0
|
79 if ($language->getId() == $entity->getUntranslated()->language()->getId()) {
|
Chris@0
|
80 return AccessResult::forbidden()->addCacheableDependency($entity);
|
Chris@0
|
81 }
|
Chris@0
|
82 // Editors have no access to the translation operations, as entity
|
Chris@0
|
83 // access already grants them an equal or greater access level.
|
Chris@0
|
84 $templates = ['update' => 'edit-form', 'delete' => 'delete-form'];
|
Chris@0
|
85 if ($entity->access($operation) && $entity_type->hasLinkTemplate($templates[$operation])) {
|
Chris@0
|
86 return AccessResult::forbidden()->cachePerPermissions();
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 if ($account->hasPermission('translate any entity')) {
|
Chris@0
|
91 return AccessResult::allowed()->cachePerPermissions();
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 switch ($operation) {
|
Chris@0
|
95 case 'create':
|
Chris@0
|
96 /* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
|
Chris@0
|
97 $handler = $this->entityManager->getHandler($entity->getEntityTypeId(), 'translation');
|
Chris@0
|
98 $translations = $entity->getTranslationLanguages();
|
Chris@0
|
99 $languages = $this->languageManager->getLanguages();
|
Chris@0
|
100 $source_language = $this->languageManager->getLanguage($source) ?: $entity->language();
|
Chris@0
|
101 $target_language = $this->languageManager->getLanguage($target) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
|
Chris@0
|
102 $is_new_translation = ($source_language->getId() != $target_language->getId()
|
Chris@0
|
103 && isset($languages[$source_language->getId()])
|
Chris@0
|
104 && isset($languages[$target_language->getId()])
|
Chris@0
|
105 && !isset($translations[$target_language->getId()]));
|
Chris@0
|
106 return AccessResult::allowedIf($is_new_translation)->cachePerPermissions()->addCacheableDependency($entity)
|
Chris@0
|
107 ->andIf($handler->getTranslationAccess($entity, $operation));
|
Chris@0
|
108
|
Chris@0
|
109 case 'delete':
|
Chris@0
|
110 // @todo Remove this in https://www.drupal.org/node/2945956.
|
Chris@0
|
111 /** @var \Drupal\Core\Access\AccessResultInterface $delete_access */
|
Chris@0
|
112 $delete_access = \Drupal::service('content_translation.delete_access')->checkAccess($entity);
|
Chris@0
|
113 $access = $this->checkAccess($entity, $language, $operation);
|
Chris@0
|
114 return $delete_access->andIf($access);
|
Chris@0
|
115
|
Chris@0
|
116 case 'update':
|
Chris@0
|
117 return $this->checkAccess($entity, $language, $operation);
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 // No opinion.
|
Chris@0
|
122 return AccessResult::neutral();
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Performs access checks for the specified operation.
|
Chris@0
|
127 *
|
Chris@0
|
128 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
|
Chris@0
|
129 * The entity being checked.
|
Chris@0
|
130 * @param \Drupal\Core\Language\LanguageInterface $language
|
Chris@0
|
131 * For an update or delete operation, the language code of the translation
|
Chris@0
|
132 * being updated or deleted.
|
Chris@0
|
133 * @param string $operation
|
Chris@0
|
134 * The operation to be checked.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@0
|
137 * An access result object.
|
Chris@0
|
138 */
|
Chris@0
|
139 protected function checkAccess(ContentEntityInterface $entity, LanguageInterface $language, $operation) {
|
Chris@0
|
140 /* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
|
Chris@0
|
141 $handler = $this->entityManager->getHandler($entity->getEntityTypeId(), 'translation');
|
Chris@0
|
142 $translations = $entity->getTranslationLanguages();
|
Chris@0
|
143 $languages = $this->languageManager->getLanguages();
|
Chris@0
|
144 $has_translation = isset($languages[$language->getId()])
|
Chris@0
|
145 && $language->getId() != $entity->getUntranslated()->language()->getId()
|
Chris@0
|
146 && isset($translations[$language->getId()]);
|
Chris@0
|
147 return AccessResult::allowedIf($has_translation)->cachePerPermissions()->addCacheableDependency($entity)
|
Chris@0
|
148 ->andIf($handler->getTranslationAccess($entity, $operation));
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 }
|