@@ -91,6 +91,7 @@ Next, make sure you've configured a "user provider" for the user:
91
91
your_db_provider :
92
92
entity :
93
93
class : AppBundle:User
94
+ property : apiKey
94
95
95
96
# ...
96
97
@@ -159,17 +160,9 @@ This requires you to implement six methods::
159
160
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
160
161
use Symfony\Component\Security\Core\Exception\AuthenticationException;
161
162
use Symfony\Component\Security\Core\User\UserProviderInterface;
162
- use Doctrine\ORM\EntityManager;
163
163
164
164
class TokenAuthenticator extends AbstractGuardAuthenticator
165
165
{
166
- private $em;
167
-
168
- public function __construct(EntityManager $em)
169
- {
170
- $this->em = $em;
171
- }
172
-
173
166
/**
174
167
* Called on every request. Return whatever credentials you want,
175
168
* or null to stop authentication.
@@ -193,8 +186,7 @@ This requires you to implement six methods::
193
186
194
187
// if null, authentication will fail
195
188
// if a User object, checkCredentials() is called
196
- return $this->em->getRepository('AppBundle:User')
197
- ->findOneBy(array('apiKey' => $apiKey));
189
+ return $userProvider->loadUserByUsername($apiKey);
198
190
}
199
191
200
192
public function checkCredentials($credentials, UserInterface $user)
@@ -258,15 +250,12 @@ To finish this, register the class as a service:
258
250
services :
259
251
app.token_authenticator :
260
252
class : AppBundle\Security\TokenAuthenticator
261
- arguments : ['@doctrine.orm.entity_manager']
262
253
263
254
.. code-block :: xml
264
255
265
256
<!-- app/config/services.xml -->
266
257
<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" />
270
259
</services >
271
260
272
261
.. code-block :: php
@@ -275,10 +264,7 @@ To finish this, register the class as a service:
275
264
use Symfony\Component\DependencyInjection\Definition;
276
265
use Symfony\Component\DependencyInjection\Reference;
277
266
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'));
282
268
283
269
Finally, configure your ``firewalls `` key in ``security.yml `` to use this authenticator:
284
270
0 commit comments