@@ -1203,8 +1203,7 @@ Thanks to the SensioFrameworkExtraBundle, you can also secure your controller us
12031203 // ...
12041204 }
12051205
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 `_.
12081207
12091208Securing other Services
12101209~~~~~~~~~~~~~~~~~~~~~~~
@@ -1567,57 +1566,30 @@ is available by calling the PHP function :phpfunction:`hash_algos`.
15671566Determining the Hashed Password
15681567...............................
15691568
1570- .. versionadded :: 2.6
1571- The ``security.password_encoder `` service was introduced in Symfony 2.6.
1572-
15731569If you're storing users in the database and you have some sort of registration
15741570form for users, you'll need to be able to determine the hashed password so
15751571that you can set it on your user before inserting it. No matter what algorithm
15761572you configure for your user object, the hashed password can always be determined
15771573in the following way from a controller::
15781574
1575+ $factory = $this->get('security.encoder_factory');
15791576 $user = new Acme\UserBundle\Entity\User();
1580- $plainPassword = 'ryanpass';
1581- $encoded = $this->container->get('security.password_encoder')
1582- ->encodePassword($user, $plainPassword);
15831577
1584- $user->setPassword($encoded);
1578+ $encoder = $factory->getEncoder($user);
1579+ $password = $encoder->encodePassword('ryanpass', $user->getSalt());
1580+ $user->setPassword($password);
15851581
15861582In order for this to work, just make sure that you have the encoder for your
15871583user class (e.g. ``Acme\UserBundle\Entity\User ``) configured under the ``encoders ``
15881584key in ``app/config/security.yml ``.
15891585
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-
16001586.. caution ::
16011587
16021588 When you allow a user to submit a plaintext password (e.g. registration
16031589 form, change password form), you *must * have validation that guarantees
16041590 that the password is 4096 characters or less. Read more details in
16051591 :ref: `How to implement a simple Registration Form <cookbook-registration-password-max >`.
16061592
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-
16211593Retrieving the User Object
16221594~~~~~~~~~~~~~~~~~~~~~~~~~~
16231595
@@ -2331,6 +2303,7 @@ Learn more from the Cookbook
23312303* :doc: `/cookbook/security/remember_me `
23322304* :doc: `How to Restrict Firewalls to a Specific Request </cookbook/security/firewall_restriction >`
23332305
2306+ .. _`FrameworkExtraBundle documentation` : http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/security.html
23342307.. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
23352308.. _`implement the \S erializable interface` : http://php.net/manual/en/class.serializable.php
23362309.. _`Timing attack` : http://en.wikipedia.org/wiki/Timing_attack
0 commit comments