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

LeJeuneDev

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

我的客户班

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

我的Employe课程

/**
 * @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中,我设置了两个提供程序:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章