Skip to content

Commit 1816d1c

Browse files
committed
minor #15347 Rewrite config form the Security bundle to use ConfigBuilders (Nyholm)
This PR was submitted for the 5.4 branch but it was merged into the 5.3 branch instead. Discussion ---------- Rewrite config form the Security bundle to use ConfigBuilders Commits ------- 730a4ca Rewrite config form the Security bundle
2 parents 3779ae5 + 730a4ca commit 1816d1c

24 files changed

+871
-917
lines changed

configuration/env_var_processors.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ Symfony provides the following env var processors:
234234
.. code-block:: php
235235
236236
// config/packages/security.php
237-
$container->setParameter('env(HEALTH_CHECK_METHOD)', 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD');
238-
$container->loadFromExtension('security', [
239-
'access_control' => [
240-
[
241-
'path' => '^/health-check$',
242-
'methods' => '%env(const:HEALTH_CHECK_METHOD)%',
243-
],
244-
],
245-
]);
237+
use Symfony\Component\DependencyInjection\ContainerBuilder;
238+
use Symfony\Config\SecurityConfig;
239+
240+
return static function (ContainerBuilder $container, SecurityConfig $security) {
241+
$container->setParameter('env(HEALTH_CHECK_METHOD)', 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD');
242+
$security->accessControl()
243+
->path('^/health-check$')
244+
->methods(['%env(const:HEALTH_CHECK_METHOD)%']);
245+
};
246246
247247
``env(base64:FOO)``
248248
Decodes the content of ``FOO``, which is a base64 encoded string.

reference/configuration/security.rst

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,34 @@ hashing algorithm. Also, each algorithm defines different config options:
215215
216216
// config/packages/security.php
217217
use App\Entity\User;
218+
use Symfony\Config\SecurityConfig;
218219
219-
$container->loadFromExtension('security', [
220+
return static function (SecurityConfig $security) {
220221
// ...
221-
'password_hashers' => [
222-
// auto hasher with default options
223-
User::class => [
224-
'algorithm' => 'auto',
225-
],
226-
227-
// auto hasher with custom options
228-
User::class => [
229-
'algorithm' => 'auto',
230-
'cost' => 15,
231-
],
232-
233-
// Sodium hasher with default options
234-
User::class => [
235-
'algorithm' => 'sodium',
236-
],
237-
238-
// Sodium hasher with custom options
239-
User::class => [
240-
'algorithm' => 'sodium',
241-
'memory_cost' => 16384, // Amount in KiB. (16384 = 16 MiB)
242-
'time_cost' => 2, // Number of iterations
243-
],
244-
245-
// MessageDigestPasswordHasher hasher using SHA512 hashing with default options
246-
User::class => [
247-
'algorithm' => 'sha512',
248-
],
249-
],
250-
]);
222+
223+
// auto hasher with default options
224+
$security->passwordHasher(User::class)
225+
->algorithm('auto');
226+
227+
// auto hasher with custom options
228+
$security->passwordHasher(User::class)
229+
->algorithm('auto')
230+
->cost(15);
231+
232+
// Sodium hasher with default options
233+
$security->passwordHasher(User::class)
234+
->algorithm('sodium');
235+
236+
// Sodium hasher with custom options
237+
$security->passwordHasher(User::class)
238+
->algorithm('sodium')
239+
->memoryCost(16384) // Amount in KiB. (16384 = 16 MiB)
240+
->timeCost(2); // Number of iterations
241+
242+
// MessageDigestPasswordHasher hasher using SHA512 hashing with default options
243+
$security->passwordHasher(User::class)
244+
->algorithm('sha512');
245+
};
251246
252247
.. versionadded:: 5.3
253248

@@ -310,18 +305,19 @@ hashing algorithm. Also, each algorithm defines different config options:
310305
311306
// config/packages/test/security.php
312307
use App\Entity\User;
308+
use Symfony\Config\SecurityConfig;
309+
310+
return static function (SecurityConfig $security) {
311+
// ...
313312
314-
$container->loadFromExtension('security', [
315-
'password_hashers' => [
316-
// Use your user class name here
317-
User::class => [
318-
'algorithm' => 'auto', // This should be the same value as in config/packages/security.yaml
319-
'cost' => 4, // Lowest possible value for bcrypt
320-
'time_cost' => 3, // Lowest possible value for argon
321-
'memory_cost' => 10, // Lowest possible value for argon
322-
]
323-
],
324-
]);
313+
// Use your user class name here
314+
$security->passwordHasher(User::class)
315+
->algorithm('auto') // This should be the same value as in config/packages/security.yaml
316+
->cost(4) // Lowest possible value for bcrypt
317+
->timeCost(2) // Lowest possible value for argon
318+
->memoryCost(10) // Lowest possible value for argon
319+
;
320+
};
325321
326322
.. _reference-security-sodium:
327323
.. _using-the-argon2i-password-encoder:
@@ -432,20 +428,20 @@ application:
432428
.. code-block:: php
433429
434430
// config/packages/security.php
431+
use Symfony\Config\SecurityConfig;
432+
433+
return static function (SecurityConfig $security) {
434+
// ...
435435
436-
// ...
437-
$container->loadFromExtension('security', [
438-
'firewalls' => [
439-
// 'main' is the name of the firewall (can be chosen freely)
440-
'main' => [
441-
// 'pattern' is a regular expression matched against the incoming
442-
// request URL. If there's a match, authentication is triggered
443-
'pattern' => '^/admin',
444-
// the rest of options depend on the authentication mechanism
445-
// ...
446-
],
447-
],
448-
]);
436+
// 'main' is the name of the firewall (can be chosen freely)
437+
$security->firewall('main')
438+
// 'pattern' is a regular expression matched against the incoming
439+
// request URL. If there's a match, authentication is triggered
440+
->pattern('^/admin')
441+
// the rest of options depend on the authentication mechanism
442+
// ...
443+
;
444+
};
449445
450446
.. seealso::
451447

@@ -807,18 +803,19 @@ multiple firewalls, the "context" could actually be shared:
807803
.. code-block:: php
808804
809805
// config/packages/security.php
810-
$container->loadFromExtension('security', [
811-
'firewalls' => [
812-
'somename' => [
813-
// ...
814-
'context' => 'my_context',
815-
],
816-
'othername' => [
817-
// ...
818-
'context' => 'my_context',
819-
],
820-
],
821-
]);
806+
use Symfony\Config\SecurityConfig;
807+
808+
return static function (SecurityConfig $security) {
809+
$security->firewall('somename')
810+
// ...
811+
->context('my_context')
812+
;
813+
814+
$security->firewall('othername')
815+
// ...
816+
->context('my_context')
817+
;
818+
};
822819
823820
.. note::
824821

0 commit comments

Comments
 (0)