|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Gbere\SimpleAuth\Repository\AdminUserRepository; |
|
9
|
|
|
use Gbere\SimpleAuth\Repository\UserRepository; |
|
10
|
|
|
use Gbere\SimpleAuth\Security\Constant; |
|
11
|
|
|
use Gbere\SimpleAuth\Security\LoginFormAuthenticator; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
17
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
18
|
|
|
|
|
19
|
|
|
class GbereSimpleAuthExtension extends Extension implements PrependExtensionInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array|null */ |
|
22
|
|
|
private $securityConfig; |
|
23
|
|
|
/** @var array|null */ |
|
24
|
|
|
private $twigConfig; |
|
25
|
|
|
/** @var array|null */ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws Exception |
|
30
|
|
|
*/ |
|
31
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
|
32
|
|
|
{ |
|
33
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
34
|
|
|
$loader->load('services.yaml'); |
|
35
|
|
|
|
|
36
|
|
|
$definition = $container->getDefinition(UserRepository::class); |
|
37
|
|
|
$definition->setArgument(1, new Reference($this->config['user']['entity'])); |
|
38
|
|
|
|
|
39
|
|
|
$definition = $container->getDefinition(AdminUserRepository::class); |
|
40
|
|
|
$definition->setArgument(1, new Reference($this->config['admin_user']['entity'])); |
|
41
|
|
|
|
|
42
|
|
|
$container->setParameter('simple_auth_sender_email', $this->config['sender']['email']); |
|
43
|
|
|
$container->setParameter('simple_auth_sender_name', $this->config['sender']['name']); |
|
44
|
|
|
$container->setParameter('simple_auth_confirm_registration', $this->config['confirm_registration']); |
|
45
|
|
|
$container->setParameter('simple_auth_remember_me', $this->config['remember_me_lifetime'] ? true : false); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function prepend(ContainerBuilder $container): void |
|
49
|
|
|
{ |
|
50
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
|
51
|
|
|
$this->config = $this->processConfiguration(new Configuration(), $configs); |
|
52
|
|
|
|
|
53
|
|
|
if ('test' === $container->getParameter('kernel.environment')) { |
|
54
|
|
|
$this->addSecurityTestingRoutesConfig(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->addSecurityEncodersConfig(); |
|
58
|
|
|
$this->addSecurityProvidersConfig(); |
|
59
|
|
|
$this->addSecurityFirewallConfig(); |
|
60
|
|
|
$this->updateSecurityExtensionConfig($container); |
|
61
|
|
|
|
|
62
|
|
|
$this->addTwigGlobalsConfig(); |
|
63
|
|
|
$this->updateTwigExtensionConfig($container); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function addSecurityTestingRoutesConfig(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->securityConfig['access_control'] = constant::TESTING_ROUTES; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function addSecurityEncodersConfig(): void |
|
72
|
|
|
{ |
|
73
|
|
|
if (isset($this->config['user'])) { |
|
74
|
|
|
$this->securityConfig['encoders'] = [ |
|
75
|
|
|
$this->config['user']['entity'] => [ |
|
76
|
|
|
'algorithm' => $this->config['user']['encoder_algorithm'], |
|
77
|
|
|
], |
|
78
|
|
|
$this->config['admin_user']['entity'] => [ |
|
79
|
|
|
'algorithm' => $this->config['admin_user']['encoder_algorithm'], |
|
80
|
|
|
], |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function addSecurityProvidersConfig(): void |
|
86
|
|
|
{ |
|
87
|
|
|
if (isset($this->config['user'])) { |
|
88
|
|
|
$this->securityConfig['providers'] = [ |
|
89
|
|
|
Constant::PROVIDER_NAME => [ |
|
90
|
|
|
'entity' => [ |
|
91
|
|
|
'class' => $this->config['user']['entity'], |
|
92
|
|
|
'property' => 'email', |
|
93
|
|
|
], |
|
94
|
|
|
], |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function addSecurityFirewallConfig(): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->securityConfig['firewalls'] = [ |
|
102
|
|
|
Constant::FIREWALL_NAME => [ |
|
103
|
|
|
'anonymous' => 'lazy', |
|
104
|
|
|
'provider' => Constant::PROVIDER_NAME, |
|
105
|
|
|
'guard' => [ |
|
106
|
|
|
'authenticators' => [LoginFormAuthenticator::class], |
|
107
|
|
|
], |
|
108
|
|
|
'logout' => [ |
|
109
|
|
|
'path' => 'simple_auth_logout', |
|
110
|
|
|
], |
|
111
|
|
|
], |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
if (isset($this->config['remember_me_lifetime']) && null != $this->config['remember_me_lifetime']) { |
|
115
|
|
|
$this->securityConfig['firewalls'][Constant::FIREWALL_NAME]['remember_me'] = [ |
|
116
|
|
|
'secret' => '%kernel.secret%', |
|
117
|
|
|
'lifetime' => $this->config['remember_me_lifetime'], |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function updateSecurityExtensionConfig(ContainerBuilder $container): void |
|
123
|
|
|
{ |
|
124
|
|
|
if (null === $this->securityConfig) { |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$extensionConfigsRefl = new \ReflectionProperty(ContainerBuilder::class, 'extensionConfigs'); |
|
129
|
|
|
$extensionConfigsRefl->setAccessible(true); |
|
130
|
|
|
$extensionConfigs = $extensionConfigsRefl->getValue($container); |
|
131
|
|
|
|
|
132
|
|
|
foreach ($this->securityConfig as $section => $configs) { |
|
133
|
|
|
if (isset($extensionConfigs['security'][0][$section])) { |
|
134
|
|
|
if ('firewalls' === $section) { |
|
135
|
|
|
// added after firewall->dev and before firewall->main |
|
136
|
|
|
if (isset($extensionConfigs['security'][0][$section]['main'])) { |
|
137
|
|
|
$configs['main'] = $extensionConfigs['security'][0][$section]['main']; |
|
138
|
|
|
unset($extensionConfigs['security'][0][$section]['main']); |
|
139
|
|
|
} |
|
140
|
|
|
$extensionConfigs['security'][0][$section] = array_merge($extensionConfigs['security'][0][$section], $configs); |
|
141
|
|
|
} else { |
|
142
|
|
|
$extensionConfigs['security'][0][$section] = array_merge($configs, $extensionConfigs['security'][0][$section]); |
|
143
|
|
|
} |
|
144
|
|
|
} else { |
|
145
|
|
|
$extensionConfigs['security'][0][$section] = $configs; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$extensionConfigsRefl->setValue($container, $extensionConfigs); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
private function addTwigGlobalsConfig(): void |
|
153
|
|
|
{ |
|
154
|
|
|
$this->twigConfig['globals']['simple_auth_form_logo'] = $this->config['style']['form_logo']; |
|
155
|
|
|
$this->twigConfig['globals']['simple_auth_accent_color'] = '#'.$this->config['style']['accent_color']; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
private function updateTwigExtensionConfig(ContainerBuilder $container): void |
|
159
|
|
|
{ |
|
160
|
|
|
$container->prependExtensionConfig('twig', $this->twigConfig); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|