0
我無法弄清楚用戶的id如何模仿用戶而不是Symfony中用戶的用戶名?如何在symfony中通過id代替用戶模擬用戶?
下面的技巧與用戶名的作品不能與ID工作,因爲symfony尋找用戶名:
?_switch_user={id}
問候。
PS:對不起,我的英語。
我無法弄清楚用戶的id如何模仿用戶而不是Symfony中用戶的用戶名?如何在symfony中通過id代替用戶模擬用戶?
下面的技巧與用戶名的作品不能與ID工作,因爲symfony尋找用戶名:
?_switch_user={id}
問候。
PS:對不起,我的英語。
這是不可能實現自己的防火牆偵聽器,因爲它從用戶提供者(它的接口中只有一個loadUserByUsername()方法)加載用戶的幕後。
但是,您可以實現自己的防火牆偵聽器,並通過查看中的代碼獲取靈感。有關實施您自己的身份驗證提供程序的詳細信息,請檢查cookbook article。
編輯:
一個可能的解決方案可能會註冊一個額外的請求收聽:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LookupSwitchUserListener implements EventSubscriberInterface
{
private $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['lookup', 12] // before the firewall
];
}
public function lookup(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->has('_switch_user') {
return; // do nothing if already a _switch_user param present
}
if (!$id = $request->query->has('_switch_user_by_id')) {
return; // do nothing if no _switch_user_by_id param
}
// lookup $username by $id using the repository here
$request->attributes->set('_switch_user', $username);
}
}
現在註冊這個監聽器在服務容器:
services:
my_listener:
class: LookupSwitchUserListener
tags:
- { name: kernel.event_subscriber }
調用與?_switch_user_by_id=xxx
一個url參數現在應該正確查找用戶名並進行設置,以便SwitchUserListener可以切換到指定的用戶。
很多很多謝謝。 –
如果使用FOS用戶,有沒有其他辦法? –
我建議只使用用戶名代替。您可以在兩個事物之間創建一個「管理員」控制器,通過它的ID查找用戶,並顯示具有正確'_switch_user'查詢參數的鏈接。 – Gerry