Skip to content

Commit 8ccfe85

Browse files
committed
feature #3491 Added feature doc for named encoders (tamirvs)
This PR was merged into the master branch. Discussion ---------- Added feature doc for named encoders | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes, PR symfony/symfony#10005 | Applies to | 2.5+ | Fixed tickets | - Please check the _xml_ config example as I'm not too confident with _xml_. Commits ------- 8cd63d0 Added feature doc for named encoders
2 parents 46377b2 + 8cd63d0 commit 8ccfe85

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

book/security.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,79 @@ or via some online tool.
14341434
Supported algorithms for this method depend on your PHP version. A full list
14351435
is available by calling the PHP function :phpfunction:`hash_algos`.
14361436

1437+
Named encoders
1438+
..............
1439+
1440+
.. versionadded:: 2.5
1441+
Named encoders were introduced in Symfony 2.5
1442+
1443+
Another option is to set the encoder dynamically on an instance basis.
1444+
In the previous example, you've set the ``sha512`` algorithm for ``Acme\UserBundle\Entity\User``.
1445+
This may be secure enough for a regular user, but what if you want your admins to have
1446+
a stronger algorithm? Let's say ``bcrypt``. This can be done with named encoders:
1447+
1448+
.. configuration-block::
1449+
1450+
.. code-block:: yaml
1451+
1452+
# app/config/security.yml
1453+
security:
1454+
# ...
1455+
encoders:
1456+
harsh:
1457+
algorithm: bcrypt
1458+
cost: 15
1459+
1460+
.. code-block:: xml
1461+
1462+
<!-- app/config/security.xml -->
1463+
<?xml version="1.0" encoding="UTF-8" ?>
1464+
<srv:container xmlns="http://symfony.com/schema/dic/security"
1465+
xmlns:srv="http://symfony.com/schema/dic/services">
1466+
1467+
<config>
1468+
<!-- ... -->
1469+
<encoder class="harsh"
1470+
algorithm="bcrypt"
1471+
cost="15" />
1472+
</config>
1473+
</srv:container>
1474+
1475+
.. code-block:: php
1476+
1477+
// app/config/security.php
1478+
$container->loadFromExtension('security', array(
1479+
// ...
1480+
'encoders' => array(
1481+
'harsh' => array(
1482+
'algorithm' => 'bcrypt',
1483+
'cost' => '15'
1484+
),
1485+
),
1486+
));
1487+
1488+
Now you've created an encoder named ``harsh``. In order for a ``User`` instance to use it,
1489+
It must implement ``EncoderAwareInterface`` and have a method ``getEncoderName`` which returns the
1490+
name of the encoder to use::
1491+
1492+
// src/Acme/UserBundle/Entity/User.php
1493+
namespace Acme\UserBundle\Entity;
1494+
1495+
use Symfony\Component\Security\Core\User\UserInterface;
1496+
use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
1497+
1498+
class User implements UserInterface, EncoderAwareInterface
1499+
{
1500+
public function getEncoderName()
1501+
{
1502+
if ($this->isAdmin()) {
1503+
return 'harsh';
1504+
}
1505+
1506+
return null; // use the default encoder
1507+
}
1508+
}
1509+
14371510
Determining the Hashed Password
14381511
...............................
14391512

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy