为了账号安全,请及时绑定邮箱和手机立即绑定

Symfony 4 安全重定向取决于用户类型

Symfony 4 安全重定向取决于用户类型

PHP
翻阅古今 2023-07-08 20:55:28
我正在一个网站上工作,该网站将有两种类型的用户“客户”(客户)和“雇员”(雇员)这两个类都扩展了我的用户类:我的客户班/** * @ORM\Entity(repositoryClass="App\Repository\ClientRepository") */class Client extends User{    /**     * @ORM\Id()     * @ORM\GeneratedValue()     * @ORM\Column(type="integer")     */    protected $id;    /**     * @ORM\Column(type="string", length=255)     */    private $client_fonction;    /**     * @ORM\OneToMany(targetEntity="App\Entity\ClientEmployee", mappedBy="client_id")     */    private $client_id;    /**     * @ORM\ManyToOne(targetEntity=Site::class, inversedBy="clients")     */    private $site;我的员工班级/** * @ORM\Entity(repositoryClass="App\Repository\EmployeRepository") */class Employe extends User{    /**     * @ORM\Id()     * @ORM\GeneratedValue()     * @ORM\Column(type="integer")     */    protected $id;    /**     * @ORM\Column(type="integer", nullable=true)     */    private $portablePro;    /**     * @ORM\ManyToOne(targetEntity="App\Entity\Agence", inversedBy="agence_id")     * @ORM\JoinColumn(nullable=false)     */    private $agence_spie_id;    /**     * @ORM\OneToMany(targetEntity="App\Entity\ClientEmployee", mappedBy="employe_id")     */    private $employe_id;这是我的 User 类中的继承映射:/** * @ORM\Entity(repositoryClass=UserRepository::class) * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({"Employe"="Employe", "Client"="Client"}) */abstract class User implements UserInterface我正在寻找方法:如果用户是“客户”-> 重定向到 /client 路由 如果用户是“雇员”-> 重定向到 /admin 路由。在我的 security.yaml 中,我设置了 2 个提供程序:providers:    chain_provider:        chain:            providers: [app_employe_provider, app_client_provider]    app_employe_provider:        entity:            class: App\Entity\EmployeSpie            property: email    app_client_provider:        entity:            class: App\Entity\Client            property: email如何在我的 LoginFormAuthenticator 中,我可以根据用户的类型重定向用户?
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

由于令牌作为参数传递,您可以从那里提取用户(类型)。


public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)

{

    $user = $token->getUser();

    if($user instanceof Employe) {

        // Do one thing

    } else if($user instanceof Client){ 

        // Do other thing.

    }

}


查看完整回答
反对 回复 2023-07-08
  • 1 回答
  • 0 关注
  • 66 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信