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

The firewall user shoyld be reloaded from the client entity manager #482

Open
soullivaneuh opened this issue Jan 14, 2019 · 1 comment
Open
Labels
Milestone

Comments

@soullivaneuh
Copy link
Contributor

The user used on makeClient with createUserToken is loaded from fixtures and is not managed by the client entity manager.

This cause issue during test because doctrine considers it a new not managed entity,

Preconditions

  1. Have a user on fixtures

Steps to reproduce

  1. Authenticate your client with loginAs and a user from your fixtures
  2. Create the client
  3. Make a request on a simple controller doing, for example, a refresh of the user got from the token.

Expected result

  1. The refresh should work and any save with user association should be ok.

Actual result

  1. An error telling the entity is not managed by doctrine.

If I add a line to reload the user from the client doctrine service, it works:

foreach ($this->firewallLogins as $firewallName => $user) {
    // OVERRIDE
    $user = $client->getContainer()->get('doctrine')->getRepository(User::class)->find($user->getId());
    // END OVERRIDE
    $token = $this->createUserToken($user, $firewallName);

    $tokenStorage = $client->getContainer()->get('security.token_storage');

    $tokenStorage->setToken($token);
    $session->set('_security_'.$firewallName, serialize($token));
}

But I don't know how to do it properly.

Three possible solutions:

  1. Make this bundle reloading the user from the client all the time
  2. If not possible for some cases, make an option for that
  3. If it's a too much specific case for you, make the override easier with a method like setupFirewalls with the client as parameter.

What do you think?

@soullivaneuh
Copy link
Contributor Author

soullivaneuh commented Jan 14, 2019

The quick and dirty workaround I found so far:

namespace App\Security\Authentication;

use AppBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

final class TokenStorage implements TokenStorageInterface
{
    /**
     * @var TokenStorageInterface
     */
    private $decorated;

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    public function __construct(TokenStorageInterface $decorated, EntityManagerInterface $entityManager)
    {
        $this->decorated = $decorated;
        $this->entityManager = $entityManager;
    }

    /**
     * {@inheritdoc}
     */
    public function getToken()
    {
        return $this->decorated->getToken();
    }

    /**
     * {@inheritdoc}
     */
    public function setToken(?TokenInterface $token = null)
    {
        $this->decorated->setToken($token);

        if (null === $token) {
            return;
        }

        $user = $token->getUser();

        // @see https://github.com/liip/LiipFunctionalTestBundle/issues/482
        if ($user instanceof User && !$this->entityManager->contains($user)) {
            $token->setUser($this->entityManager->getRepository(User::class)->find($user->getId()));
        }
    }
}

And the service decoration declaration (under autowiring):

services:
    App\Security\Authentication\TokenStorage:
        decorates: security.token_storage

@alexislefebvre alexislefebvre added this to the 2.0 milestone Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants