security.context service is missing in a Symfony3 app

118 Views Asked by At

I inherited a Symfony 3 application that appears to have a good, working installation of FOSUserBundle. However, when I try to inject @security.context into a service in order to retrieve the logged-in user, I get errors about @security.context being a missing service. I tried the checks I saw on a couple other threads -- making sure that services.yml is filled out and making sure that it's correctly included by my main config file -- and it looks like everything is done correctly. Here is my security.yml file:

security:
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
        api_key_user_provider:
            id: security.user.provider.api_key

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            admin
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:          true
            switch_user:        true
            remember_me:
                secret:      '%secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /

        # -> end custom configuration

        hwi:
            pattern: ^/hwi
            form_login: false
            oauth:
                resource_owners:
                    twitter:  "/hwi/check-twitter"
                    instagram:  "/hwi/check-instagram"
                    google:  "/hwi/check-google"
                    facebook:  "/hwi/check-facebook"
                    apple: "/hwi/check-apple"
                login_path:   "/hwi/login"
                check_path:   "/hwi/check"
                use_forward:  false
                failure_path: "/hwi/login"
                oauth_user_provider:
                    service: app.service.hwi


        api:
            pattern: ^/api/
            form_login: false
            stateless: true
            anonymous: true
            simple_preauth:
                authenticator: 'api_key_authenticator'
            provider: api_key_user_provider
            entry_point: api_key_authenticator


    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/api/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/doc, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

Logging into the system works well, as do other actions like logging out, resetting passwords, etc.,.

But @security.context is missing.

What am I doing wrong? Is this service deprecated or something?

1

There are 1 best solutions below

0
Mayor of the Plattenbaus On

I think I found out what I'm supposed to do in newer versions of Symfony.

It appears I can use @security.token_storage to get the token (and thus the user).