Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the user object from a service in Symfony2

Tags:

symfony

My site is using a third party service for authentication as well as other bits of functionality so I have setup a service that does all the API calls for me and it's this service that I need to be able to access the user object. I've tried injecting the security.context service into my API service but I get a ServiceCircularReferenceException because my user authentication provider references the API service (it has to in order to authenticate the user). So I get a chain of

security.context -> authentication provider -> 
user provider -> API service -> security.context

I'm struggling to this of another way of getting the user object and I can't see any obvious way of splitting up this chain.

My configs are all defined in config.yml, here are the relevant bits

myapp.database:
    class: Pogo\MyAppBundle\Service\DatabaseService
    arguments:
        siteid: %siteid%
        entityManager: "@doctrine.orm.entity_manager"
myapp.apiservice:
    class: Pogo\MyAppBundle\Service\TicketingService
    arguments:
        entityManager: "@myapp.database"
myapp.user_provider:
    class: Pogo\MyAppBundle\Service\APIUserProvider
    arguments:
        entityManager: "@myapp.database"
        ticketingAdapter: "@myapp.apiservice"
        securityContext: "@security.context"
myapp.authenticationprovider:
    class: Pogo\MyAppBundle\Service\APIAuthenticationProvider
    arguments:
        userChecker: "@security.user_checker"
        encoderFactory: "@security.encoder_factory"
        userProvider: "@myapp.user_provider"

myapp.user_provider is the service that I've defined as my user provider service in security.yml which I presume is how it's being referenced by security.context

like image 896
pogo Avatar asked Jun 17 '11 15:06

pogo


1 Answers

The security.context service has too many responsibilities. This is why it was deprecated in Symfony 2.6 and two new services took its place; security.token_storage and security.authorization_checker. Updating might solve some of your headaches.

Read more here: http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

And aslo: Why does the user provider need a security context? A UserProvider should only load a user by its username. Nothing more... Read more about custom user providers here: http://symfony.com/doc/current/cookbook/security/custom_provider.html

like image 83
Tobias Nyholm Avatar answered Oct 08 '22 08:10

Tobias Nyholm