简体   繁体   English

Symfony5 如何用不同的登录方式创建多种类型的用户 forms

[英]Symfony5 How to create multiple types of users with different login forms

I started learning Symfony and already know how to create a user and registration / login forms for him using php bin/console make:user php bin/console make:auth , but I need to have several types of users like Client , Company , Admin and what if I try go to example.com/company , we were thrown to the login window for the Company account (I'm now thrown to the login form for the User). I started learning Symfony and already know how to create a user and registration / login forms for him using php bin/console make:user php bin/console make:auth , but I need to have several types of users like Client , Company , Admin如果我尝试 go 到example.com/company怎么办,我们被扔到公司帐户的登录 window (我现在被扔到用户的登录表单)。

After searching I found these questions Symfony / Doctrine - Multiple Users Types and Symfony: Firewalls, multiple login forms , but I did not understand what to do next and how to combine them. After searching I found these questions Symfony / Doctrine - Multiple Users Types and Symfony: Firewalls, multiple login forms , but I did not understand what to do next and how to combine them. Please tell me.请告诉我。

I think better solution for You will be using roles to determine them;)我认为对你来说更好的解决方案是使用角色来确定它们;)

You can have one entity and make some getters like:您可以拥有一个实体并制作一些吸气剂,例如:

public function isAdmin():bool
{
    return in_array(self::ROLE_ADMIN, $this->roles, true);
}

And if you want split roles to different path prefixes you can have one firewall eg: app that matches all your application and next make correct access_controll rules like this:如果您想将角色拆分到不同的路径前缀,您可以拥有一个防火墙,例如:匹配您所有应用程序的应用程序,然后制定正确的 access_controll 规则,如下所示:

  access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/company, roles: ROLE_CLIENT }

And are You sure company is another user class or permission?你确定公司是另一个用户 class 还是许可? Maybe better assign admin to company and allow them to access this companies?也许更好地将管理员分配给公司并允许他们访问这些公司? ;) ;)

If you realy want to split them you could use doctrine discriminator, eg.如果你真的想拆分它们,你可以使用 doctrine 鉴别器,例如。 create Abstract user and next 3 classes extending them, you can configure security using that abstract class.创建抽象用户和扩展它们的下 3 个类,您可以使用该抽象 class 配置安全性。

Or create 3 providers, 3 firewalls, and same access_controll like above或者像上面一样创建 3 个提供者、3 个防火墙和相同的 access_controll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM