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

我正在一个网站上工作,该网站将有两种类型的用户“客户”(客户)和“雇员”(雇员)这两个类都扩展了我的用户类:


我的客户班


/**

 * @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 中,我可以根据用户的类型重定向用户?



翻阅古今
浏览 71回答 1
1回答

郎朗坤

由于令牌作为参数传递,您可以从那里提取用户(类型)。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.    }}
打开App,查看更多内容
随时随地看视频慕课网APP