E
E
EVOSandru62021-08-24 13:52:10
symfony
EVOSandru6, 2021-08-24 13:52:10

What is the right way to use CustomAuthentifer in symfony 5.3.6 security and not break the test loginUser?

Hello!

The problem of obtaining information about an authorized user.

When I remove the main block in security.yaml , the $this->client->loginUser($testUser) test snippet works properly
and AccountController::getUser() returns User - as it should.

But if the main block is uncommented, then the target route returns null instead of the expected user =(((

More details below:

There is a security.yaml config :

security:
  enable_authenticator_manager: true
  encoders:
    App\Model\User\Entity\User\User:
      algorithm: bcrypt
  providers:
    app_user_provider:
      entity:
        class: App\Model\User\Entity\User\User
        property: email
  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    main:
      provider: app_user_provider
      custom_authenticators:
        - App\Security\LoginFormAuthenticator
  access_control:


There is a custom authenticator: LoginFormAuthenticator :

namespace App\Security;

class LoginFormAuthenticator extends AbstractAuthenticator {
...


There is a test to get information about the user:

class UserinfoTest extends DbWebTestCase
{
    private const API_PREFIX = '/api/v1';

    protected function setUp(): void
    {
        $this->client = static::createClient();
        $this->client->disableReboot();
        $this->em = static::getContainer()->get('doctrine')->getManager();
    }

    public function test_userinfo_success()
    {
       
        $userRepository = $this->em->getRepository(User::class);
        $testUser = $userRepository->findOneBy(['email' => '[email protected]']);
        $this->client->loginUser($testUser);
        $this->client->request('GET', self::API_PREFIX . '/account/userinfo');
         ...
    }
}


And the AccountController itself, where the test goes:

namespace App\Controller\Api\V1\Account;

#[Route('api/v1/account')]
class AccountController extends BaseController
{
    #[Route('/userinfo')]
    public function userInfo(): JsonResponse
    {
        $user = $this->getUser();

        return $this->json([
            'code' => 200,
            'data' => [
                'user' => [
                    'username' => (string)$user->getUsername(),
                ]
            ]
        ], 200, []);
    }

    protected function getUser(): User
    {
        return parent::getUser();  // !!!!!!!!! ПРОБЛЕМА ТУТ
    }
}


And the User itself :

namespace App\Model\User\Entity\User;

class User implements UserInterface, UserLoaderInterface, UserProviderInterface {
...


Please tell me - how to save the authenticator and the ability to authorize the user in the test through loginUser ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question