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

Symfony 4 Security Redirect depending user type

我在一个网站上工作,这个网站会有两种类型的用户“client”(客户)和“employee”(员工) class 都在扩展我的用户 Class :

我的客户class

/**
 * @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;

我的员工class

/**
 * @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;

这是我在用户 class 中的继承映射:

/**
 * @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

role_hierarchy:
    ROLE_CUSTOMER:
    ROlE_IA :
    ROLE_ADV :
    ROLE_CM :
    ROLE_RT :
    ROLE_ADMIN:
    ROLE_SUPER_ADMIN: ROLE_ADMIN
# 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: ^/admin, roles: ROLE_ADMIN }
      - { path: ^/client, roles: ROLE_CUSTOMER }

如何在我的 LoginFormAuthenticator 中根据用户的类型重定向用户?

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
        return new RedirectResponse($targetPath);
    }

    // For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
    throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
}

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

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.
    }
}