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