Skip to content

Commit fa8d09f

Browse files
committed
minor #6931 [Guard] Improve clarity using the configured provider (chalasr)
This PR was merged into the 2.8 branch. Discussion ---------- [Guard] Improve clarity using the configured provider Recently at work, a colleague trying to discover Guard said me how it was confused by the given example: > Does Guard require to configure an user provider, or can I simply inject the entity manager? Otherwise, what is the point of configuring this provider? I think I understand why this example was made as is, showing the flexibility of Guard and its ability to answer different needs in different authentication ways without going deeper into things that are already documented somewhere else. On the other hand I can understand it's confusing and IMHO remove the use of the entity manager can make it clearer and easier to understand. Note that the current example shouldn't work as is, as don't set the `property` key of an entity provider involves to create a doctrine repository implementing `UserLoaderInterface` for the corresponding `User` entity (if the provider comes to be used of course). What do you think? ping @weaverryan Commits ------- 456dc05 [Guard] Improve clarity using the configured provider
2 parents 0878b32 + 456dc05 commit fa8d09f

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

security/guard_authentication.rst

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Next, make sure you've configured a "user provider" for the user:
9191
your_db_provider:
9292
entity:
9393
class: AppBundle:User
94+
property: apiKey
9495
9596
# ...
9697
@@ -159,17 +160,9 @@ This requires you to implement six methods::
159160
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
160161
use Symfony\Component\Security\Core\Exception\AuthenticationException;
161162
use Symfony\Component\Security\Core\User\UserProviderInterface;
162-
use Doctrine\ORM\EntityManager;
163163

164164
class TokenAuthenticator extends AbstractGuardAuthenticator
165165
{
166-
private $em;
167-
168-
public function __construct(EntityManager $em)
169-
{
170-
$this->em = $em;
171-
}
172-
173166
/**
174167
* Called on every request. Return whatever credentials you want,
175168
* or null to stop authentication.
@@ -193,8 +186,7 @@ This requires you to implement six methods::
193186

194187
// if null, authentication will fail
195188
// if a User object, checkCredentials() is called
196-
return $this->em->getRepository('AppBundle:User')
197-
->findOneBy(array('apiKey' => $apiKey));
189+
return $userProvider->loadUserByUsername($apiKey);
198190
}
199191

200192
public function checkCredentials($credentials, UserInterface $user)
@@ -258,15 +250,12 @@ To finish this, register the class as a service:
258250
services:
259251
app.token_authenticator:
260252
class: AppBundle\Security\TokenAuthenticator
261-
arguments: ['@doctrine.orm.entity_manager']
262253
263254
.. code-block:: xml
264255
265256
<!-- app/config/services.xml -->
266257
<services>
267-
<service id="app.token_authenticator" class="AppBundle\Security\TokenAuthenticator">
268-
<argument type="service" id="doctrine.orm.entity_manager"/>
269-
</service>
258+
<service id="app.token_authenticator" class="AppBundle\Security\TokenAuthenticator" />
270259
</services>
271260
272261
.. code-block:: php
@@ -275,10 +264,7 @@ To finish this, register the class as a service:
275264
use Symfony\Component\DependencyInjection\Definition;
276265
use Symfony\Component\DependencyInjection\Reference;
277266
278-
$container->setDefinition('app.token_authenticator', new Definition(
279-
'AppBundle\Security\TokenAuthenticator',
280-
array(new Reference('doctrine.orm.entity_manager'))
281-
));
267+
$container->setDefinition('app.token_authenticator', new Definition('AppBundle\Security\TokenAuthenticator'));
282268
283269
Finally, configure your ``firewalls`` key in ``security.yml`` to use this authenticator:
284270

0 commit comments

Comments
 (0)