0
votes

I would like to make API-Platform Admin app available only for user with role "ROLE_ADMIN". I set up authentication according to doc: https://api-platform.com/docs/admin/authentication-support/ and everything works fine but now each logged user can have access to the admin section.

I've tried to restrict access to a single endpoint by "access_control" as below but then site displays only "Loading..." and I get:

GET https://localhost:8443/provinces 401
fetchResource.js:50 Uncaught (in promise) Error: Unreachable resource
    at fetchResource.js:50

api/config/packages/security.yaml

security:
    encoders:
        App\Entity\User:
            algorithm: argon2i

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            stateless: true
            anonymous: true
            provider: app_user_provider
            json_login:
                check_path: /authentication_token
                username_path: email
                password_path: password
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/provinces, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

Any suggestion?

1

1 Answers

0
votes

You need to handle Authentication in your Frontend app.

My suggestion: use JWT authentication mechanism as described in the docs. If you are not using a React frontend app, well, you need to adapt (I use Angular)