Skip to content

Commit 456dc05

Browse files
committed
[Guard] Improve clarity using the configured provider
1 parent f7ba37a commit 456dc05

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)