@@ -1203,7 +1203,8 @@ Thanks to the SensioFrameworkExtraBundle, you can also secure your controller us
1203
1203
// ...
1204
1204
}
1205
1205
1206
- For more information, see the `FrameworkExtraBundle documentation `_.
1206
+ For more information, see the
1207
+ `FrameworkExtraBundle documentation <http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/security.html >`_.
1207
1208
1208
1209
Securing other Services
1209
1210
~~~~~~~~~~~~~~~~~~~~~~~
@@ -1566,30 +1567,57 @@ is available by calling the PHP function :phpfunction:`hash_algos`.
1566
1567
Determining the Hashed Password
1567
1568
...............................
1568
1569
1570
+ .. versionadded :: 2.6
1571
+ The ``security.password_encoder `` service was introduced in Symfony 2.6.
1572
+
1569
1573
If you're storing users in the database and you have some sort of registration
1570
1574
form for users, you'll need to be able to determine the hashed password so
1571
1575
that you can set it on your user before inserting it. No matter what algorithm
1572
1576
you configure for your user object, the hashed password can always be determined
1573
1577
in the following way from a controller::
1574
1578
1575
- $factory = $this->get('security.encoder_factory');
1576
1579
$user = new Acme\UserBundle\Entity\User();
1580
+ $plainPassword = 'ryanpass';
1581
+ $encoded = $this->container->get('security.password_encoder')
1582
+ ->encodePassword($user, $plainPassword);
1577
1583
1578
- $encoder = $factory->getEncoder($user);
1579
- $password = $encoder->encodePassword('ryanpass', $user->getSalt());
1580
- $user->setPassword($password);
1584
+ $user->setPassword($encoded);
1581
1585
1582
1586
In order for this to work, just make sure that you have the encoder for your
1583
1587
user class (e.g. ``Acme\UserBundle\Entity\User ``) configured under the ``encoders ``
1584
1588
key in ``app/config/security.yml ``.
1585
1589
1590
+ .. sidebar :: Get the User Encoder
1591
+
1592
+ In some cases, you need a specific encoder for a given user (e.g. ``Acme\UserBundle\Entity\User ``).
1593
+ You can use the ``EncoderFactory `` to get this encoder::
1594
+
1595
+ $factory = $this->get('security.encoder_factory');
1596
+ $user = new Acme\UserBundle\Entity\User();
1597
+
1598
+ $encoder = $factory->getEncoder($user);
1599
+
1586
1600
.. caution ::
1587
1601
1588
1602
When you allow a user to submit a plaintext password (e.g. registration
1589
1603
form, change password form), you *must * have validation that guarantees
1590
1604
that the password is 4096 characters or less. Read more details in
1591
1605
:ref: `How to implement a simple Registration Form <cookbook-registration-password-max >`.
1592
1606
1607
+ Validating a Plaintext Password
1608
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1609
+
1610
+ Sometimes you want to check if a plain password is valid for a given user::
1611
+
1612
+ // a user instance of some class which implements Symfony\Component\Security\Core\User\UserInterface
1613
+ $user = ...;
1614
+
1615
+ // the password that should be checked
1616
+ $plainPassword = ...;
1617
+
1618
+ $isValidPassword = $this->container->get('security.password_encoder')
1619
+ ->isPasswordValid($user, $plainPassword);
1620
+
1593
1621
Retrieving the User Object
1594
1622
~~~~~~~~~~~~~~~~~~~~~~~~~~
1595
1623
@@ -2303,7 +2331,6 @@ Learn more from the Cookbook
2303
2331
* :doc: `/cookbook/security/remember_me `
2304
2332
* :doc: `How to Restrict Firewalls to a Specific Request </cookbook/security/firewall_restriction >`
2305
2333
2306
- .. _`FrameworkExtraBundle documentation` : http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/security.html
2307
2334
.. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
2308
2335
.. _`implement the \S erializable interface` : http://php.net/manual/en/class.serializable.php
2309
2336
.. _`Timing attack` : http://en.wikipedia.org/wiki/Timing_attack
0 commit comments