|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\UserBundle\Form\Type\ResettingFormType; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface; |
|
8
|
|
|
use Kunstmaan\AdminBundle\Service\AuthenticationMailer\SwiftmailerService; |
|
9
|
|
|
use Kunstmaan\AdminBundle\Service\AuthenticationMailer\SymfonyMailerService; |
|
10
|
|
|
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle; |
|
11
|
|
|
use Symfony\Component\Config\FileLocator; |
|
12
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
20
|
|
|
use Symfony\Component\Mailer\Mailer; |
|
21
|
|
|
use Kunstmaan\AdminBundle\DependencyInjection\Routes\FosRouteLoader; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This is the class that loads and manages your bundle configuration |
|
25
|
|
|
* |
|
26
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
27
|
|
|
*/ |
|
28
|
|
|
class KunstmaanAdminExtension extends Extension implements PrependExtensionInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
14 |
|
* Loads a specific configuration. |
|
32
|
|
|
* |
|
33
|
14 |
|
* @param array $configs An array of configuration values |
|
34
|
14 |
|
* @param ContainerBuilder $container A ContainerBuilder instance |
|
35
|
14 |
|
* |
|
36
|
|
|
* @throws InvalidArgumentException When provided tag is not defined in this extension |
|
37
|
14 |
|
*/ |
|
38
|
14 |
|
public function load(array $configs, ContainerBuilder $container) |
|
39
|
|
|
{ |
|
40
|
14 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
41
|
1 |
|
$loader->load('services.yml'); |
|
42
|
|
|
$loader->load('commands.yml'); |
|
43
|
14 |
|
|
|
44
|
1 |
|
$container->setParameter('version_checker.url', 'https://cms.kunstmaan.be/version-check'); |
|
45
|
|
|
$container->setParameter('version_checker.timeframe', 60 * 60 * 24); |
|
46
|
14 |
|
$container->setParameter('version_checker.enabled', true); |
|
47
|
14 |
|
|
|
48
|
|
|
$configuration = new Configuration(); |
|
49
|
14 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
50
|
14 |
|
|
|
51
|
|
|
if (\array_key_exists('dashboard_route', $config)) { |
|
52
|
14 |
|
$container->setParameter('kunstmaan_admin.dashboard_route', $config['dashboard_route']); |
|
53
|
|
|
} |
|
54
|
14 |
|
if (\array_key_exists('admin_password', $config)) { |
|
55
|
|
|
$container->setParameter('kunstmaan_admin.admin_password', $config['admin_password']); |
|
56
|
14 |
|
} |
|
57
|
14 |
|
|
|
58
|
14 |
|
$this->configureAuthentication($config, $container, $loader); |
|
59
|
14 |
|
|
|
60
|
|
|
$container->setParameter('kunstmaan_admin.admin_locales', $config['admin_locales']); |
|
61
|
14 |
|
$container->setParameter('kunstmaan_admin.default_admin_locale', $config['default_admin_locale']); |
|
62
|
14 |
|
|
|
63
|
14 |
|
$container->setParameter('kunstmaan_admin.session_security.ip_check', $config['session_security']['ip_check']); |
|
64
|
14 |
|
$container->setParameter('kunstmaan_admin.session_security.user_agent_check', $config['session_security']['user_agent_check']); |
|
65
|
14 |
|
|
|
66
|
14 |
|
$container->setParameter('kunstmaan_admin.admin_prefix', $this->normalizeUrlSlice($config['admin_prefix'])); |
|
67
|
14 |
|
|
|
68
|
14 |
|
$container->setParameter('kunstmaan_admin.admin_exception_excludes', $config['admin_exception_excludes']); |
|
69
|
|
|
|
|
70
|
14 |
|
$container->setParameter('kunstmaan_admin.google_signin.enabled', $config['google_signin']['enabled']); |
|
71
|
14 |
|
$container->setParameter('kunstmaan_admin.google_signin.client_id', $config['google_signin']['client_id']); |
|
72
|
|
|
$container->setParameter('kunstmaan_admin.google_signin.client_secret', $config['google_signin']['client_secret']); |
|
73
|
14 |
|
$container->setParameter('kunstmaan_admin.google_signin.hosted_domains', $config['google_signin']['hosted_domains']); |
|
74
|
14 |
|
|
|
75
|
14 |
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_digits', $config['password_restrictions']['min_digits']); |
|
76
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_uppercase', $config['password_restrictions']['min_uppercase']); |
|
77
|
14 |
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_special_characters', $config['password_restrictions']['min_special_characters']); |
|
78
|
14 |
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_length', $config['password_restrictions']['min_length']); |
|
79
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.max_length', $config['password_restrictions']['max_length']); |
|
80
|
|
|
$container->setParameter('kunstmaan_admin.enable_toolbar_helper', $config['enable_toolbar_helper']); |
|
81
|
14 |
|
$container->setParameter('kunstmaan_admin.toolbar_firewall_names', !empty($config['provider_keys']) ? $config['provider_keys'] : $config['toolbar_firewall_names']); |
|
82
|
1 |
|
$container->setParameter('kunstmaan_admin.admin_firewall_name', $config['admin_firewall_name']); |
|
83
|
|
|
$container->setParameter('kunstmaan_admin.user_class', $config['admin_user_class']); |
|
84
|
|
|
$container->setParameter('kunstmaan_admin.group_class', $config['admin_group_class']); |
|
85
|
14 |
|
|
|
86
|
14 |
|
$container->registerForAutoconfiguration(MenuAdaptorInterface::class) |
|
87
|
14 |
|
->addTag('kunstmaan_admin.menu.adaptor'); |
|
88
|
14 |
|
|
|
89
|
14 |
|
if (!empty($config['enable_console_exception_listener']) && $config['enable_console_exception_listener']) { |
|
90
|
|
|
$loader->load('console_listener.yml'); |
|
91
|
14 |
|
} |
|
92
|
|
|
|
|
93
|
14 |
|
if (0 !== \count($config['menu_items'])) { |
|
94
|
14 |
|
$this->addSimpleMenuAdaptor($container, $config['menu_items']); |
|
95
|
14 |
|
} |
|
96
|
|
|
|
|
97
|
14 |
|
$this->addWebsiteTitleParameter($container, $config); |
|
98
|
14 |
|
$this->addMultiLanguageParameter($container, $config); |
|
99
|
14 |
|
$this->addRequiredLocalesParameter($container, $config); |
|
100
|
14 |
|
$this->addDefaultLocaleParameter($container, $config); |
|
101
|
14 |
|
} |
|
102
|
14 |
|
|
|
103
|
|
|
public function prepend(ContainerBuilder $container) |
|
104
|
14 |
|
{ |
|
105
|
14 |
|
$fosUserOriginalConfig = $container->getExtensionConfig('fos_user'); |
|
106
|
14 |
|
if (!isset($fosUserOriginalConfig[0]['db_driver'])) { |
|
107
|
14 |
|
$fosUserConfig['db_driver'] = 'orm'; // other valid values are 'mongodb', 'couchdb' |
|
|
|
|
|
|
108
|
14 |
|
} |
|
109
|
14 |
|
$fosUserConfig['from_email']['address'] = '[email protected]'; |
|
|
|
|
|
|
110
|
|
|
$fosUserConfig['from_email']['sender_name'] = 'KunstmaanCMS'; |
|
111
|
14 |
|
$fosUserConfig['firewall_name'] = 'main'; |
|
112
|
14 |
|
$fosUserConfig['user_class'] = 'Kunstmaan\AdminBundle\Entity\User'; |
|
113
|
|
|
$fosUserConfig['group']['group_class'] = 'Kunstmaan\AdminBundle\Entity\Group'; |
|
114
|
|
|
$fosUserConfig['resetting']['token_ttl'] = 86400; |
|
115
|
14 |
|
// Use this node only if you don't want the global email address for the resetting email |
|
116
|
|
|
$fosUserConfig['resetting']['email']['from_email']['address'] = '[email protected]'; |
|
117
|
|
|
$fosUserConfig['resetting']['email']['from_email']['sender_name'] = 'KunstmaanCMS'; |
|
118
|
14 |
|
$fosUserConfig['resetting']['email']['template'] = '@FOSUser/Resetting/email.txt.twig'; |
|
119
|
14 |
|
$fosUserConfig['resetting']['form']['type'] = ResettingFormType::class; |
|
120
|
|
|
$fosUserConfig['resetting']['form']['name'] = 'fos_user_resetting_form'; |
|
121
|
|
|
$fosUserConfig['resetting']['form']['validation_groups'] = ['ResetPassword']; |
|
122
|
|
|
|
|
123
|
14 |
|
$fosUserConfig['service']['mailer'] = 'fos_user.mailer.twig_swift'; |
|
124
|
14 |
|
$container->prependExtensionConfig('fos_user', $fosUserConfig); |
|
125
|
14 |
|
|
|
126
|
|
|
// Manually register the KunstmaanAdminBundle folder as a FosUser override for symfony 4. |
|
127
|
|
|
if ($container->hasParameter('kernel.project_dir') && file_exists($container->getParameter('kernel.project_dir').'/templates/bundles/KunstmaanAdminBundle')) { |
|
128
|
|
|
$twigConfig['paths'][] = ['value' => '%kernel.project_dir%/templates/bundles/KunstmaanAdminBundle', 'namespace' => 'FOSUser']; |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
15 |
|
$twigConfig['paths'][] = ['value' => \dirname(__DIR__).'/Resources/views', 'namespace' => 'FOSUser']; |
|
|
|
|
|
|
131
|
|
|
$container->prependExtensionConfig('twig', $twigConfig); |
|
132
|
15 |
|
|
|
133
|
|
|
// NEXT_MAJOR: Remove templating dependency |
|
134
|
|
|
|
|
135
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
|
136
|
|
|
$this->processConfiguration(new Configuration(), $configs); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getNamespace() |
|
143
|
1 |
|
{ |
|
144
|
|
|
return 'http://bundles.kunstmaan.be/schema/dic/admin'; |
|
145
|
1 |
|
} |
|
146
|
1 |
|
|
|
147
|
1 |
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
1 |
|
*/ |
|
150
|
|
|
public function getXsdValidationBasePath() |
|
151
|
1 |
|
{ |
|
152
|
1 |
|
return __DIR__.'/../Resources/config/schema'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems) |
|
156
|
|
|
{ |
|
157
|
|
|
$definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [ |
|
158
|
|
|
new Reference('security.authorization_checker'), |
|
159
|
14 |
|
$menuItems, |
|
160
|
|
|
]); |
|
161
|
|
|
$definition->addTag('kunstmaan_admin.menu.adaptor'); |
|
162
|
14 |
|
|
|
163
|
|
|
$container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition); |
|
164
|
|
|
} |
|
165
|
14 |
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $urlSlice |
|
168
|
14 |
|
* |
|
169
|
|
|
* @return string |
|
170
|
14 |
|
*/ |
|
171
|
|
|
protected function normalizeUrlSlice($urlSlice) |
|
172
|
|
|
{ |
|
173
|
14 |
|
/* Get rid of exotic characters that would break the url */ |
|
174
|
|
|
$urlSlice = filter_var($urlSlice, FILTER_SANITIZE_URL); |
|
175
|
14 |
|
|
|
176
|
14 |
|
/* Remove leading and trailing slashes */ |
|
177
|
2 |
|
$urlSlice = trim($urlSlice, '/'); |
|
178
|
|
|
|
|
179
|
2 |
|
/* Make sure our $urlSlice is literally used in our regex */ |
|
180
|
|
|
$urlSlice = preg_quote($urlSlice); |
|
181
|
|
|
|
|
182
|
14 |
|
return $urlSlice; |
|
183
|
14 |
|
} |
|
184
|
|
|
|
|
185
|
14 |
View Code Duplication |
private function addWebsiteTitleParameter(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
186
|
|
|
{ |
|
187
|
14 |
|
$websiteTitle = $config['website_title']; |
|
188
|
14 |
|
if (null === $config['website_title']) { |
|
189
|
2 |
|
@trigger_error('Not providing a value for the "kunstmaan_admin.website_title" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
2 |
|
$websiteTitle = $container->hasParameter('websitetitle') ? $container->getParameter('websitetitle') : ''; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
14 |
|
$container->setParameter('kunstmaan_admin.website_title', $websiteTitle); |
|
195
|
14 |
|
} |
|
196
|
|
|
|
|
197
|
14 |
View Code Duplication |
private function addMultiLanguageParameter(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
14 |
|
$multilanguage = $config['multi_language']; |
|
200
|
14 |
|
if (null === $multilanguage) { |
|
201
|
2 |
|
@trigger_error('Not providing a value for the "kunstmaan_admin.multi_language" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
2 |
|
$multilanguage = $container->hasParameter('multilanguage') ? $container->getParameter('multilanguage') : ''; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
14 |
|
$container->setParameter('kunstmaan_admin.multi_language', $multilanguage); |
|
207
|
14 |
|
} |
|
208
|
14 |
|
|
|
209
|
|
View Code Duplication |
private function addRequiredLocalesParameter(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
210
|
14 |
|
{ |
|
211
|
|
|
$requiredLocales = $config['required_locales']; |
|
212
|
14 |
|
if (null === $config['required_locales']) { |
|
213
|
14 |
|
@trigger_error('Not providing a value for the "kunstmaan_admin.required_locales" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
214
|
2 |
|
|
|
215
|
|
|
$requiredLocales = $container->hasParameter('requiredlocales') ? $container->getParameter('requiredlocales') : ''; |
|
216
|
2 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
$container->setParameter('kunstmaan_admin.required_locales', $requiredLocales); |
|
219
|
14 |
|
$container->setParameter('requiredlocales', $requiredLocales); //Keep old parameter for to keep BC with routing config |
|
220
|
14 |
|
} |
|
221
|
|
|
|
|
222
|
|
View Code Duplication |
private function addDefaultLocaleParameter(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
223
|
|
|
{ |
|
224
|
|
|
$defaultLocale = $config['default_locale']; |
|
225
|
|
|
if (null === $config['default_locale']) { |
|
226
|
|
|
@trigger_error('Not providing a value for the "kunstmaan_admin.default_locale" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
$defaultLocale = $container->hasParameter('defaultlocale') ? $container->getParameter('defaultlocale') : ''; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$container->setParameter('kunstmaan_admin.default_locale', $defaultLocale); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
private function configureAuthentication(array $config, ContainerBuilder $container, LoaderInterface $loader) |
|
235
|
|
|
{ |
|
236
|
|
|
$enableCustomLogin = $config['authentication']['enable_new_authentication']; |
|
237
|
|
|
$container->setParameter('kunstmaan_admin.enable_new_cms_authentication', $enableCustomLogin); |
|
238
|
|
|
|
|
239
|
|
|
if (!$enableCustomLogin) { |
|
240
|
|
|
@trigger_error('Not setting "kunstmaan_admin.authentication.enable_new_authentication" to true is deprecated since KunstmaanAdminBundle 5.8, it will always be true in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
$loader->load('authentication.yml'); |
|
246
|
|
|
|
|
247
|
|
|
$fosRouteLoaderDefinition = $container->getDefinition(FosRouteLoader::class); |
|
248
|
|
|
$fosRouteLoaderDefinition->setArgument(0, $enableCustomLogin); |
|
249
|
|
|
|
|
250
|
|
|
$container->setAlias('kunstmaan_admin.authentication.mailer', $config['authentication']['mailer']['service']); |
|
251
|
|
|
|
|
252
|
|
|
// Validate mailer config |
|
253
|
|
|
if (!class_exists(SwiftmailerBundle::class) && !class_exists(Mailer::class)) { |
|
254
|
|
|
throw new LogicException('No mail integration found to enable the authentication mailer. Try running "composer require symfony/mailer" or "composer require symfony/swiftmailer-bundle".'); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
View Code Duplication |
if ($config['authentication']['mailer']['service'] === SymfonyMailerService::class && !class_exists(Mailer::class)) { |
|
|
|
|
|
|
258
|
|
|
throw new LogicException('Symfony mailer support for the authentication mailer cannot be enabled as the component is not installed. Try running "composer require symfony/mailer".'); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
View Code Duplication |
if ($config['authentication']['mailer']['service'] === SwiftmailerService::class && !class_exists(SwiftmailerBundle::class)) { |
|
|
|
|
|
|
262
|
|
|
throw new LogicException('Swiftmailer support for the authentication mailer cannot be enabled as the component is not installed. Try running "composer require symfony/swiftmailer-bundle".'); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// Cleanup mailer services |
|
266
|
|
|
if (!class_exists(SwiftmailerBundle::class)) { |
|
267
|
|
|
$container->removeDefinition(SwiftmailerService::class); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
if (!class_exists(Mailer::class)) { |
|
271
|
|
|
$container->removeDefinition(SymfonyMailerService::class); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
if ($container->hasDefinition(SwiftmailerService::class)) { |
|
275
|
|
|
$definition = $container->getDefinition(SwiftmailerService::class); |
|
276
|
|
|
$definition |
|
277
|
|
|
->setArgument(4, $config['authentication']['mailer']['from_address']) |
|
278
|
|
|
->setArgument(5, $config['authentication']['mailer']['from_name']) |
|
279
|
|
|
; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
$mailerAddress = $container->getDefinition('kunstmaan_admin.mailer.default_sender'); |
|
283
|
|
|
$mailerAddress->setArgument(0, $config['authentication']['mailer']['from_address']); |
|
284
|
|
|
$mailerAddress->setArgument(1, $config['authentication']['mailer']['from_name']); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.