1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use Silverback\ApiComponentBundle\Doctrine\Extension\ORM\TablePrefixExtension; |
18
|
|
|
use Silverback\ApiComponentBundle\Entity\Core\ComponentInterface; |
19
|
|
|
use Silverback\ApiComponentBundle\EventListener\Doctrine\UserListener; |
20
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\ChangeEmailVerificationEmailFactory; |
21
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\PasswordChangedEmailFactory; |
22
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\PasswordResetEmailFactory; |
23
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\UserEnabledEmailFactory; |
24
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\UsernameChangedEmailFactory; |
25
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\WelcomeEmailFactory; |
26
|
|
|
use Silverback\ApiComponentBundle\Factory\User\UserFactory; |
27
|
|
|
use Silverback\ApiComponentBundle\Form\FormTypeInterface; |
28
|
|
|
use Silverback\ApiComponentBundle\Form\Type\User\ChangePasswordType; |
29
|
|
|
use Silverback\ApiComponentBundle\Form\Type\User\NewEmailAddressType; |
30
|
|
|
use Silverback\ApiComponentBundle\Form\Type\User\UserRegisterType; |
31
|
|
|
use Silverback\ApiComponentBundle\Helper\PublishableHelper; |
32
|
|
|
use Silverback\ApiComponentBundle\Mailer\UserMailer; |
33
|
|
|
use Silverback\ApiComponentBundle\Manager\User\PasswordManager; |
34
|
|
|
use Silverback\ApiComponentBundle\Repository\User\UserRepository; |
35
|
|
|
use Silverback\ApiComponentBundle\Security\TokenAuthenticator; |
36
|
|
|
use Silverback\ApiComponentBundle\Security\UserChecker; |
37
|
|
|
use Silverback\ApiComponentBundle\Serializer\Normalizer\MetadataNormalizer; |
38
|
|
|
use Symfony\Component\Config\FileLocator; |
39
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
40
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
41
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
42
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @author Daniel West <[email protected]> |
46
|
|
|
*/ |
47
|
|
|
class SilverbackApiComponentExtension extends Extension implements PrependExtensionInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
1 |
|
public function load(array $configs, ContainerBuilder $container): void |
53
|
|
|
{ |
54
|
1 |
|
$configuration = new Configuration(); |
55
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
56
|
|
|
|
57
|
1 |
|
$this->loadServiceConfig($container); |
58
|
|
|
|
59
|
1 |
|
$definition = $container->getDefinition(TablePrefixExtension::class); |
60
|
1 |
|
$definition->setArgument('$prefix', $config['table_prefix']); |
61
|
|
|
|
62
|
1 |
|
$definition = $container->getDefinition(TokenAuthenticator::class); |
63
|
1 |
|
$definition->setArgument('$tokens', $config['security']['tokens']); |
64
|
|
|
|
65
|
1 |
|
$definition = $container->getDefinition(PasswordManager::class); |
66
|
1 |
|
$definition->setArgument('$tokenTtl', $config['user']['password_reset']['repeat_ttl_seconds']); |
67
|
|
|
|
68
|
1 |
|
$definition = $container->getDefinition(UserRepository::class); |
69
|
1 |
|
$definition->setArgument('$passwordRequestTimeout', $config['user']['password_reset']['request_timeout_seconds']); |
70
|
1 |
|
$definition->setArgument('$entityClass', $config['user']['class_name']); |
71
|
|
|
|
72
|
1 |
|
$definition = $container->getDefinition(PublishableHelper::class); |
73
|
1 |
|
$definition->setArgument('$permission', $config['publishable']['permission']); |
74
|
|
|
|
75
|
1 |
|
$definition = $container->getDefinition(MetadataNormalizer::class); |
76
|
1 |
|
$definition->setArgument('$metadataKey', $config['metadata_key']); |
77
|
|
|
|
78
|
1 |
|
$this->setEmailVerificationArguments($container, $config['user']['email_verification']); |
79
|
1 |
|
$this->setUserClassArguments($container, $config['user']['class_name']); |
80
|
1 |
|
$this->setMailerServiceArguments($container, $config); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
private function setEmailVerificationArguments(ContainerBuilder $container, array $emailVerificationConfig): void |
84
|
|
|
{ |
85
|
1 |
|
$definition = $container->getDefinition(UserChecker::class); |
86
|
1 |
|
$definition->setArgument('$denyUnverifiedLogin', $emailVerificationConfig['deny_unverified_login']); |
87
|
|
|
|
88
|
1 |
|
$definition = $container->getDefinition(UserListener::class); |
89
|
1 |
|
$definition->setArgument('$initialEmailVerifiedState', $emailVerificationConfig['default_value']); |
90
|
1 |
|
$definition->setArgument('$verifyEmailOnRegister', $emailVerificationConfig['verify_on_register']); |
91
|
1 |
|
$definition->setArgument('$verifyEmailOnChange', $emailVerificationConfig['verify_on_change']); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
private function setUserClassArguments(ContainerBuilder $container, string $userClass): void |
95
|
|
|
{ |
96
|
1 |
|
$definition = $container->getDefinition(UserFactory::class); |
97
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
98
|
|
|
|
99
|
1 |
|
$definition = $container->getDefinition(ChangePasswordType::class); |
100
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
101
|
|
|
|
102
|
1 |
|
$definition = $container->getDefinition(NewEmailAddressType::class); |
103
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
104
|
|
|
|
105
|
1 |
|
$definition = $container->getDefinition(UserRegisterType::class); |
106
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
1 |
|
private function setMailerServiceArguments(ContainerBuilder $container, array $config): void |
110
|
|
|
{ |
111
|
1 |
|
$definition = $container->getDefinition(UserMailer::class); |
112
|
1 |
|
$definition->setArgument('$context', [ |
113
|
1 |
|
'website_name' => $config['website_name'], |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
$mapping = [ |
117
|
1 |
|
PasswordChangedEmailFactory::class => 'password_changed', |
118
|
|
|
UserEnabledEmailFactory::class => 'user_enabled', |
119
|
|
|
UsernameChangedEmailFactory::class => 'username_changed', |
120
|
|
|
WelcomeEmailFactory::class => 'welcome', |
121
|
|
|
]; |
122
|
1 |
|
foreach ($mapping as $class => $key) { |
123
|
1 |
|
$definition = $container->getDefinition($class); |
124
|
1 |
|
$definition->setArgument('$subject', $config['user']['emails'][$key]['subject']); |
125
|
1 |
|
$definition->setArgument('$enabled', $config['user']['emails'][$key]['enabled']); |
126
|
1 |
|
if (WelcomeEmailFactory::class === $class) { |
127
|
1 |
|
$definition->setArgument('$defaultRedirectPath', $config['user']['email_verification']['email']['default_redirect_path']); |
128
|
1 |
|
$definition->setArgument('$redirectPathQueryKey', $config['user']['email_verification']['email']['redirect_path_query']); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$mapping = [ |
133
|
1 |
|
ChangeEmailVerificationEmailFactory::class => 'email_verification', |
134
|
|
|
PasswordResetEmailFactory::class => 'password_reset', |
135
|
|
|
]; |
136
|
1 |
|
foreach ($mapping as $class => $key) { |
137
|
1 |
|
$definition = $container->getDefinition($class); |
138
|
1 |
|
$definition->setArgument('$subject', $config['user'][$key]['email']['subject']); |
139
|
1 |
|
$definition->setArgument('$enabled', true); |
140
|
1 |
|
$definition->setArgument('$defaultRedirectPath', $config['user'][$key]['email']['default_redirect_path']); |
141
|
1 |
|
$definition->setArgument('$redirectPathQueryKey', $config['user'][$key]['email']['redirect_path_query']); |
142
|
|
|
} |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @throws Exception |
147
|
|
|
*/ |
148
|
1 |
|
private function loadServiceConfig(ContainerBuilder $container): void |
149
|
|
|
{ |
150
|
1 |
|
$container->registerForAutoconfiguration(FormTypeInterface::class) |
151
|
1 |
|
->addTag('silverback_api_component.form_type'); |
152
|
|
|
|
153
|
1 |
|
$container->registerForAutoconfiguration(ComponentInterface::class) |
154
|
1 |
|
->addTag('silverback_api_component.entity.component'); |
155
|
|
|
|
156
|
1 |
|
$loader = new PhpFileLoader( |
157
|
1 |
|
$container, |
158
|
1 |
|
new FileLocator(__DIR__ . '/../Resources/config') |
159
|
|
|
); |
160
|
1 |
|
$loader->load('services.php'); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
public function prepend(ContainerBuilder $container): void |
164
|
|
|
{ |
165
|
1 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
166
|
1 |
|
$configuration = new Configuration(); |
167
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
168
|
1 |
|
$srcBase = __DIR__ . '/..'; |
169
|
1 |
|
$configBasePath = $srcBase . '/Resources/config/api_platform'; |
170
|
1 |
|
$mappingPaths = [$srcBase . '/Entity/Core']; |
171
|
1 |
|
foreach ($config['enabled_components'] as $key => $enabled_component) { |
172
|
1 |
|
if (true === $enabled_component) { |
173
|
1 |
|
$mappingPaths[] = sprintf('%s/%s.yaml', $configBasePath, $key); |
174
|
|
|
} |
175
|
|
|
} |
176
|
1 |
|
$websiteName = $config['website_name']; |
177
|
1 |
|
$container->prependExtensionConfig( |
178
|
1 |
|
'api_platform', |
179
|
|
|
[ |
180
|
1 |
|
'title' => $websiteName, |
181
|
1 |
|
'description' => sprintf('API for %s', $websiteName), |
182
|
|
|
'collection' => [ |
183
|
|
|
'pagination' => [ |
184
|
|
|
'client_items_per_page' => true, |
185
|
|
|
'items_per_page_parameter_name' => 'perPage', |
186
|
|
|
'maximum_items_per_page' => 100, |
187
|
|
|
], |
188
|
|
|
], |
189
|
|
|
'mapping' => [ |
190
|
1 |
|
'paths' => $mappingPaths, |
191
|
|
|
], |
192
|
|
|
'swagger' => [ |
193
|
|
|
'api_keys' => [ |
194
|
|
|
'API Token' => [ |
195
|
|
|
'name' => 'X-AUTH-TOKEN', |
196
|
|
|
'type' => 'header', |
197
|
|
|
], |
198
|
|
|
'JWT (use prefix `Bearer`)' => [ |
199
|
|
|
'name' => 'Authorization', |
200
|
|
|
'type' => 'header', |
201
|
|
|
], |
202
|
|
|
], |
203
|
|
|
], |
204
|
|
|
] |
205
|
|
|
); |
206
|
1 |
|
} |
207
|
|
|
} |
208
|
|
|
|