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