mardif
mardif

Reputation: 341

User null inside services with symfony 3.4.17

I want to get user inside a profile service, but using tokenStorage and SecurityContext, the user will be everytime null.

that's my services.yml file:

project.service.profiler:
        class: Project\Service\Profiler
        arguments:
            - "@security.helper"
            - "@=service('doctrine').getRepository('bundle:ProfileKey')"
            - "@=service('doctrine').getRepository('bundle:ProfileKeyUsers')"
            - "@=service('doctrine').getRepository('bundle:ProfileKeyRoles')"
            - "@logger"
            - "@security.token_storage"
            - "@security.authorization_checker"
            - "@fos_oauth_server.access_token_manager.default"

and that's my class Profiler

class Profiler
{

public function __construct(
        Security $security,
        ProfileKeyRepositoryInterface $profileKeyRepository,
        ProfileKeyUserRepositoryInterface $profileKeyUserRepository,
        ProfileKeyRoleRepositoryInterface $profileKeyRoleRepository,
        $logger,
        $tokenStorage,
        $authChecker,
        TokenManagerInterface $tokenManager
    ){


        if ($tokenStorage && $tokenStorage->getToken() && $tokenStorage->getToken()->getUser()) {
        $this->user = $tokenStorage->getToken()->getUser();
    }

}

}

The problem is that tokenStorage->getToken is always null (I'm sure, I'm logged in!). So, this profiler was called from a controller, where the user is present, then I suspect that when the profiler was called during the symfony loading flow, the user is not created yet.

finally, if I set this line of code:

$security->isGranted('IS_AUTHETICATED_FULLY'); --> thrown an Exception

or getToken method:

$security->getToken()  --> return null

I obtain everytime this error:

enter image description here

Why this behaviour? In previous symfony version (I mean 3.3) this problem never occurred.

Thanks a lot to anyone who helps me

Update 08/04/2019

Following the symfony3.2 docs (https://symfony.com/blog/new-in-symfony-3-2-firewall-config-class-and-profiler), this post says to check if the request is under firewall, otherwise the user token should be null.

But, in my case, I checked with debug toolbar that all it's ok.

Finally, I absolutely have no idea why the user token is null under my service

Here my security firewall section:

security:
        restricted_area:
            anonymous: ~
            access_denied_url: /unauthorized
            access_denied_handler: app.security.access_denied_handler
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
            logout:
                path: /logout
                target: /

enter image description here

Upvotes: 0

Views: 991

Answers (1)

mardif
mardif

Reputation: 341

Finally I've found the answer myself!!

So, the problem was in my class code inside the service class: I was trying to obtain the user directly in the constructor (in the symfony flow, services are loaded before token management), and here the TokenStorage->getToken was always null. So, when I need to obtain the user inside the service procedures, the tokenStorage->getToken() returns the correct value.

Hope that this answer can help someone with my same (old) problem.

Upvotes: 6

Related Questions