我正在一个网站上工作,这个网站将有2种类型的用户"客户"(客户(和"雇员"(员工( 这两个类都在执行我的用户类:
我的客户端类
/**
* @ORMEntity(repositoryClass="AppRepositoryClientRepository")
*/
class Client extends User
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
protected $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $client_fonction;
/**
* @ORMOneToMany(targetEntity="AppEntityClientEmployee", mappedBy="client_id")
*/
private $client_id;
/**
* @ORMManyToOne(targetEntity=Site::class, inversedBy="clients")
*/
private $site;
我的就业类
/**
* @ORMEntity(repositoryClass="AppRepositoryEmployeRepository")
*/
class Employe extends User
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
protected $id;
/**
* @ORMColumn(type="integer", nullable=true)
*/
private $portablePro;
/**
* @ORMManyToOne(targetEntity="AppEntityAgence", inversedBy="agence_id")
* @ORMJoinColumn(nullable=false)
*/
private $agence_spie_id;
/**
* @ORMOneToMany(targetEntity="AppEntityClientEmployee", mappedBy="employe_id")
*/
private $employe_id;
这是我的用户类中的继承映射:
/**
* @ORMEntity(repositoryClass=UserRepository::class)
* @ORMInheritanceType("JOINED")
* @ORMDiscriminatorColumn(name="type", type="string")
* @ORMDiscriminatorMap({"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: AppEntityEmployeSpie
property: email
app_client_provider:
entity:
class: AppEntityClient
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 }
如何在我的登录表单身份验证器中,我可以根据用户的类型重定向用户?
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.
}
}