开发者

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

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:
        e开发者_运维百科ntityManager: "@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


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


I found a temporary workaround, but it's not very kosher. You can inject whole @service_container to your service and use just security.context from it. However, you have to set whole container as class property. If you take just security context in constructor, it'll still throw you circular reference exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜