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