Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\config_translation\Access;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\config_translation\ConfigMapperInterface;
|
Chris@0
|
6 use Drupal\config_translation\Exception\ConfigMapperLanguageException;
|
Chris@0
|
7 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@0
|
8 use Drupal\config_translation\ConfigMapperManagerInterface;
|
Chris@0
|
9 use Drupal\Core\Access\AccessResult;
|
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
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Checks access for displaying the configuration translation overview.
|
Chris@0
|
16 */
|
Chris@0
|
17 class ConfigTranslationOverviewAccess implements AccessInterface {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The mapper plugin discovery service.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\config_translation\ConfigMapperManagerInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $configMapperManager;
|
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 ConfigTranslationOverviewAccess object.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
|
Chris@0
|
37 * The mapper plugin discovery service.
|
Chris@0
|
38 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
39 * The language manager service.
|
Chris@0
|
40 */
|
Chris@0
|
41 public function __construct(ConfigMapperManagerInterface $config_mapper_manager, LanguageManagerInterface $language_manager) {
|
Chris@0
|
42 $this->configMapperManager = $config_mapper_manager;
|
Chris@0
|
43 $this->languageManager = $language_manager;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Checks access to the overview based on permissions and translatability.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
50 * The route_match to check against.
|
Chris@0
|
51 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
52 * The account to check access for.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@0
|
55 * The access result.
|
Chris@0
|
56 */
|
Chris@0
|
57 public function access(RouteMatchInterface $route_match, AccountInterface $account) {
|
Chris@0
|
58 $mapper = $this->getMapperFromRouteMatch($route_match);
|
Chris@0
|
59
|
Chris@0
|
60 try {
|
Chris@0
|
61 $langcode = $mapper->getLangcode();
|
Chris@0
|
62 }
|
Chris@0
|
63 catch (ConfigMapperLanguageException $exception) {
|
Chris@0
|
64 // ConfigTranslationController shows a helpful message if the language
|
Chris@0
|
65 // codes do not match, so do not let that prevent granting access.
|
Chris@0
|
66 $langcode = 'en';
|
Chris@0
|
67 }
|
Chris@0
|
68 $source_language = $this->languageManager->getLanguage($langcode);
|
Chris@0
|
69
|
Chris@0
|
70 return $this->doCheckAccess($account, $mapper, $source_language);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * Gets a configuration mapper using a route match.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
77 * The route match to populate the mapper with.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return \Drupal\config_translation\ConfigMapperInterface
|
Chris@0
|
80 * The configuration mapper.
|
Chris@0
|
81 */
|
Chris@0
|
82 protected function getMapperFromRouteMatch(RouteMatchInterface $route_match) {
|
Chris@0
|
83 $mapper = $this->configMapperManager->createInstance($route_match->getRouteObject()
|
Chris@0
|
84 ->getDefault('plugin_id'));
|
Chris@0
|
85 $mapper->populateFromRouteMatch($route_match);
|
Chris@0
|
86 return $mapper;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Checks access given an account, configuration mapper, and source language.
|
Chris@0
|
91 *
|
Chris@0
|
92 * Grants access if the proper permission is granted to the account, the
|
Chris@0
|
93 * configuration has translatable pieces, and the source language is not
|
Chris@0
|
94 * locked given it is present.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
97 * The account to check access for.
|
Chris@0
|
98 * @param \Drupal\config_translation\ConfigMapperInterface $mapper
|
Chris@0
|
99 * The configuration mapper to check access for.
|
Chris@0
|
100 * @param \Drupal\Core\Language\LanguageInterface|null $source_language
|
Chris@0
|
101 * The source language to check for, if any.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@0
|
104 * The result of the access check.
|
Chris@0
|
105 */
|
Chris@0
|
106 protected function doCheckAccess(AccountInterface $account, ConfigMapperInterface $mapper, $source_language = NULL) {
|
Chris@0
|
107 $access =
|
Chris@0
|
108 $account->hasPermission('translate configuration') &&
|
Chris@0
|
109 $mapper->hasSchema() &&
|
Chris@0
|
110 $mapper->hasTranslatable() &&
|
Chris@0
|
111 (!$source_language || !$source_language->isLocked());
|
Chris@0
|
112
|
Chris@0
|
113 return AccessResult::allowedIf($access)->cachePerPermissions();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 }
|