Version 1
[yaffs-website] / web / core / modules / config_translation / src / Access / ConfigTranslationFormAccess.php
1 <?php
2
3 namespace Drupal\config_translation\Access;
4
5 use Drupal\config_translation\ConfigMapperInterface;
6 use Drupal\config_translation\Exception\ConfigMapperLanguageException;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Core\Session\AccountInterface;
10
11 /**
12  * Checks access for displaying the translation add, edit, and delete forms.
13  */
14 class ConfigTranslationFormAccess extends ConfigTranslationOverviewAccess {
15
16   /**
17    * Checks access to the overview based on permissions and translatability.
18    *
19    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
20    *   The route_match to check against.
21    * @param \Drupal\Core\Session\AccountInterface $account
22    *   The account to check access for.
23    * @param string $langcode
24    *   The language code of the target language.
25    *
26    * @return \Drupal\Core\Access\AccessResultInterface
27    *   The access result.
28    */
29   public function access(RouteMatchInterface $route_match, AccountInterface $account, $langcode = NULL) {
30     $mapper = $this->getMapperFromRouteMatch($route_match);
31
32     try {
33       $source_langcode = $mapper->getLangcode();
34       $source_language = $this->languageManager->getLanguage($source_langcode);
35
36       $target_language = $this->languageManager->getLanguage($langcode);
37
38       return $this->doCheckAccess($account, $mapper, $source_language, $target_language);
39     }
40     catch (ConfigMapperLanguageException $exception) {
41       return AccessResult::forbidden();
42     }
43   }
44
45   /**
46    * Checks access given an account, configuration mapper, and source language.
47    *
48    * In addition to the checks performed by
49    * ConfigTranslationOverviewAccess::doCheckAccess() this makes sure the target
50    * language is not locked and the target language is not the source language.
51    *
52    * Although technically configuration can be overlaid with translations in the
53    * same language, that is logically not a good idea.
54    *
55    * @param \Drupal\Core\Session\AccountInterface $account
56    *   The account to check access for.
57    * @param \Drupal\config_translation\ConfigMapperInterface $mapper
58    *   The configuration mapper to check access for.
59    * @param \Drupal\Core\Language\LanguageInterface|null $source_language
60    *   The source language to check for, if any.
61    * @param \Drupal\Core\Language\LanguageInterface|null $target_language
62    *   The target language to check for, if any.
63    *
64    * @return \Drupal\Core\Access\AccessResultInterface
65    *   The result of the access check.
66    *
67    * @see \Drupal\config_translation\Access\ConfigTranslationOverviewAccess::doCheckAccess()
68    */
69   protected function doCheckAccess(AccountInterface $account, ConfigMapperInterface $mapper, $source_language = NULL, $target_language = NULL) {
70     $base_access_result = parent::doCheckAccess($account, $mapper, $source_language);
71
72     $access =
73       $target_language &&
74       !$target_language->isLocked() &&
75       (!$source_language || ($target_language->getId() !== $source_language->getId()));
76
77     return $base_access_result->andIf(AccessResult::allowedIf($access));
78
79   }
80
81 }