annotate core/modules/content_translation/src/Access/ContentTranslationManageAccessCheck.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
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\EntityManagerInterface;
Chris@0 7 use Drupal\Core\Language\LanguageInterface;
Chris@0 8 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 9 use Drupal\Core\Routing\Access\AccessInterface;
Chris@0 10 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 11 use Drupal\Core\Session\AccountInterface;
Chris@0 12 use Symfony\Component\Routing\Route;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Access check for entity translation CRUD operation.
Chris@0 16 */
Chris@0 17 class ContentTranslationManageAccessCheck implements AccessInterface {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The entity type manager.
Chris@0 21 *
Chris@0 22 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 23 */
Chris@0 24 protected $entityManager;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The language manager.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@0 30 */
Chris@0 31 protected $languageManager;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Constructs a ContentTranslationManageAccessCheck object.
Chris@0 35 *
Chris@0 36 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
Chris@0 37 * The entity type manager.
Chris@0 38 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 39 * The language manager.
Chris@0 40 */
Chris@0 41 public function __construct(EntityManagerInterface $manager, LanguageManagerInterface $language_manager) {
Chris@0 42 $this->entityManager = $manager;
Chris@0 43 $this->languageManager = $language_manager;
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Checks translation access for the entity and operation on the given route.
Chris@0 48 *
Chris@0 49 * @param \Symfony\Component\Routing\Route $route
Chris@0 50 * The route to check against.
Chris@0 51 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 52 * The parametrized route.
Chris@0 53 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 54 * The currently logged in account.
Chris@0 55 * @param string $source
Chris@0 56 * (optional) For a create operation, the language code of the source.
Chris@0 57 * @param string $target
Chris@0 58 * (optional) For a create operation, the language code of the translation.
Chris@0 59 * @param string $language
Chris@0 60 * (optional) For an update or delete operation, the language code of the
Chris@0 61 * translation being updated or deleted.
Chris@0 62 * @param string $entity_type_id
Chris@0 63 * (optional) The entity type ID.
Chris@0 64 *
Chris@0 65 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 66 * The access result.
Chris@0 67 */
Chris@0 68 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL) {
Chris@0 69 /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
Chris@0 70 if ($entity = $route_match->getParameter($entity_type_id)) {
Chris@0 71 $operation = $route->getRequirement('_access_content_translation_manage');
Chris@0 72 $language = $this->languageManager->getLanguage($language) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
Chris@0 73 $entity_type = $this->entityManager->getDefinition($entity_type_id);
Chris@0 74
Chris@0 75 if (in_array($operation, ['update', 'delete'])) {
Chris@0 76 // Translation operations cannot be performed on the default
Chris@0 77 // translation.
Chris@0 78 if ($language->getId() == $entity->getUntranslated()->language()->getId()) {
Chris@0 79 return AccessResult::forbidden()->addCacheableDependency($entity);
Chris@0 80 }
Chris@0 81 // Editors have no access to the translation operations, as entity
Chris@0 82 // access already grants them an equal or greater access level.
Chris@0 83 $templates = ['update' => 'edit-form', 'delete' => 'delete-form'];
Chris@0 84 if ($entity->access($operation) && $entity_type->hasLinkTemplate($templates[$operation])) {
Chris@0 85 return AccessResult::forbidden()->cachePerPermissions();
Chris@0 86 }
Chris@0 87 }
Chris@0 88
Chris@0 89 if ($account->hasPermission('translate any entity')) {
Chris@0 90 return AccessResult::allowed()->cachePerPermissions();
Chris@0 91 }
Chris@0 92
Chris@0 93 /* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
Chris@0 94 $handler = $this->entityManager->getHandler($entity->getEntityTypeId(), 'translation');
Chris@0 95
Chris@0 96 // Load translation.
Chris@0 97 $translations = $entity->getTranslationLanguages();
Chris@0 98 $languages = $this->languageManager->getLanguages();
Chris@0 99
Chris@0 100 switch ($operation) {
Chris@0 101 case 'create':
Chris@0 102 $source_language = $this->languageManager->getLanguage($source) ?: $entity->language();
Chris@0 103 $target_language = $this->languageManager->getLanguage($target) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
Chris@0 104 $is_new_translation = ($source_language->getId() != $target_language->getId()
Chris@0 105 && isset($languages[$source_language->getId()])
Chris@0 106 && isset($languages[$target_language->getId()])
Chris@0 107 && !isset($translations[$target_language->getId()]));
Chris@0 108 return AccessResult::allowedIf($is_new_translation)->cachePerPermissions()->addCacheableDependency($entity)
Chris@0 109 ->andIf($handler->getTranslationAccess($entity, $operation));
Chris@0 110
Chris@0 111 case 'delete':
Chris@0 112 case 'update':
Chris@0 113 $has_translation = isset($languages[$language->getId()])
Chris@0 114 && $language->getId() != $entity->getUntranslated()->language()->getId()
Chris@0 115 && isset($translations[$language->getId()]);
Chris@0 116 return AccessResult::allowedIf($has_translation)->cachePerPermissions()->addCacheableDependency($entity)
Chris@0 117 ->andIf($handler->getTranslationAccess($entity, $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 }