Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Guard] Improve clarity using the configured provider #6931

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions security/guard_authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Next, make sure you've configured a "user provider" for the user:
your_db_provider:
entity:
class: AppBundle:User
property: apiKey

# ...

Expand Down Expand Up @@ -159,17 +160,9 @@ This requires you to implement six methods::
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Doctrine\ORM\EntityManager;

class TokenAuthenticator extends AbstractGuardAuthenticator
{
private $em;

public function __construct(EntityManager $em)
{
$this->em = $em;
}

/**
* Called on every request. Return whatever credentials you want,
* or null to stop authentication.
Expand All @@ -193,8 +186,7 @@ This requires you to implement six methods::

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

public function checkCredentials($credentials, UserInterface $user)
Expand Down Expand Up @@ -258,15 +250,12 @@ To finish this, register the class as a service:
services:
app.token_authenticator:
class: AppBundle\Security\TokenAuthenticator
arguments: ['@doctrine.orm.entity_manager']

.. code-block:: xml

<!-- app/config/services.xml -->
<services>
<service id="app.token_authenticator" class="AppBundle\Security\TokenAuthenticator">
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
<service id="app.token_authenticator" class="AppBundle\Security\TokenAuthenticator" />
</services>

.. code-block:: php
Expand All @@ -275,10 +264,7 @@ To finish this, register the class as a service:
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$container->setDefinition('app.token_authenticator', new Definition(
'AppBundle\Security\TokenAuthenticator',
array(new Reference('doctrine.orm.entity_manager'))
));
$container->setDefinition('app.token_authenticator', new Definition('AppBundle\Security\TokenAuthenticator'));

Finally, configure your ``firewalls`` key in ``security.yml`` to use this authenticator:

Expand Down