具有不同用户角色的 Security.yaml

问题描述

我是 symfony 4.4 的新手,我正在努力处理 security.yaml 文件中的角色,我有 3 种类型的用户:Admin、recruter 和 user,我试图让 recruter 访问登录表单 security_login_recruteur,同时我希望用户可以访问他的登录表单 security_login

这是我的security.yaml文件

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt
        App\Entity\Recruteur:
            algorithm: bcrypt

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        users_in_memory: { memory: null }
        in_database:
            entity:
                class: App\Entity\User # here i'm trying to mention both of the entities
                       App\Entity\Recruteur
                property: email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: lazy
            provider: in_database
            form_login:
                login_path: security_login |security_login_recruteur #here i'm tring to put both of the paths
                check_path: security_login | security_login_recruteur
            logout:
                path: security_logout | security_logout_recruteur #also in the logout path
                target: home



            # 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: ^/recruteur,roles: ROLE_RECRUTEUR }
      #- { path: ^/admin,roles: ROLE_ADMIN }

我在尝试访问 /recruteur/connexion 后遇到此错误

无法为命名路由“security_login”生成 URL |security_login_recruteur",因为这样的路由不存在。

有人可以帮我吗?

解决方法

对于您的业务案例,您必须创建多个防火墙,每个“用户”(也是多个提供商)一个。

security:
    firewalls:
        ...

        main:
            form_login:
                login_path: security_login
            ...
            provider: user_provider #example
            lazy: true

        recruter:
            form_login:
                login_path: security_login_recruteur
            ...
            provider: recruter_provider #example
            lazy: true
        
         ... # add firewall for "Admin" for example

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...