Andresch Serj
Andresch Serj

Reputation: 37328

FOSUserBundle : security context exception, even thou i have a default main firewall

I'm uasing symfony2 and FOSUserBundle. I have one firewall for login, one for assets and a main one catching everything. Still i get the "no security.context" Exception thrown when a route is not covered by any firewall and you try to access it with "is_granted" (Seen and solved here). The route is mydomain/de_DE/area where the de_DE part obviously is my {_locale}. Here's my FOSUserBundle configuration from my config.yml.

  firewalls:
    login_firewall:
      pattern:    ^/(de_DE|de_CH)/(login|resetting)$
      anonymous:  true
      form_login:
        provider: fos_userbundle
        login_path: fos_user_security_login
        check_path: fos_user_security_check
        csrf_provider: form.csrf_provider
      logout:
        path:   fos_user_security_logout
    assets_localeless:
      pattern:    ^/(compiled|web|js|css|_wdt|_profiler)/$
      anonymous:  true
    main:
      pattern: ^/$
      anonymous:  false
      form_login:
        provider: fos_userbundle
        login_path: fos_user_security_login
        check_path: fos_user_security_check
        csrf_provider: form.csrf_provider
      logout:
        path:   fos_user_security_logout

  access_control:
    - { path: ^/(compiled|web|js|css|_wdt|_profiler)$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/(de_DE|de_CH)/(login|resetting)$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/(de_DE|de_CH)/(my-admin|admin), role: ROLE_ADMIN }
    - { path: ^/(de_DE|de_CH)/$, role: ROLE_USER }

  role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

EDIT / SOLUTION: My Problem was teh RegEx. I had a misunderstanding with the tutorial on that one. So the Pattern is in plain RegEx, which is why my firewalls all didn't work (see answer). The new setting is this:

  firewalls:
    main:
      pattern: .
      anonymous:  true
      form_login:
        provider: fos_userbundle
        login_path: fos_user_security_login
        check_path: fos_user_security_check
        csrf_provider: form.csrf_provider
      logout:
        path:   fos_user_security_logout

  access_control:
    - { path: '^/(compiled|web|js|css|_wdt|_profiler)([\w\d/_-]{0,})', role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: '^/([\w]{0,})/(login|resetting|sale|imprint|contact)([\w\d/_-]{0,})', role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: '^/([\w]{0,})/(my-admin|admin)([\w\d/_-]{0,})', role: ROLE_ADMIN }
    - { path: '^/([\w]{0,})/([\w\d/_-]{0,})', role: IS_AUTHENTICATED_FULLY }

Upvotes: 0

Views: 159

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

There is no firewall for mydomain/de_DE/area configured ... that's why you have no security.context for that route.

$ means end in a regex. That's why..

- { path: ^/(de_DE|de_CH)/$, role: ROLE_USER }

... will only match exactly ...

yourdomain/de_DE/
yourdomain/de_CH/

... and no other route.

Upvotes: 1

Related Questions