php - Symfony4 SwitchUserSubscriber 不区分两个用户
问题描述
在我的 S4 应用程序中,我需要超级用户的切换用户“功能”。我有一个带有自动完成功能的表单输入来搜索用户(仅适用于 ROLE_SUPERADMIN),但我想禁止用户冒充自己。我已经实现了这个事件订阅者,但是对于 $currentUser 和 $targetUser,它返回相同的身份(目标身份)。我哪里错了?
<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
class SwitchUserSubscriber implements EventSubscriberInterface
{
public function onSecuritySwitchUser(SwitchUserEvent $event)
{
$currentUser = $event->getToken()->getUser();
$targetUser = $event->getTargetUser();
if($currentUser->getUsername() == $targetUser->getUsername()){
throw new UnsupportedUserException("You can't impersonate yourself");
}
}
public static function getSubscribedEvents()
{
return [
'security.switch_user' => 'onSecuritySwitchUser',
];
}
}
有没有更好的方法来实现这一目标?
解决方案
好的找到解决方案,阅读代码注释进行解释
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
class SwitchUserSubscriber implements EventSubscriberInterface
{
/**
* @param SwitchUserEvent $event
*/
public function onSecuritySwitchUser(SwitchUserEvent $event)
{
// Current user initialized to null
$currentUser = null;
// Gets all the roles in switching phase
$roles = $event->getToken()->getRoles();
// Cycles between roles
foreach($roles as $role) {
// SwitchUserRole it's a role of the impersonating user
// The if statement doesn't happen in switch exit
if ($role instanceof SwitchUserRole) {
// Recupera l'uente
$currentUser = $role->getSource()->getUser();
}
}
// Impersonated user
$targetUser = $event->getTargetUser();
// If you wann be yourself raises an exception
if(null !== $currentUser && ($currentUser->getUsername() == $targetUser->getUsername())){
throw new UnsupportedUserException("You can't impersnate yourself");
}
}
public static function getSubscribedEvents()
{
return [
'security.switch_user' => 'onSecuritySwitchUser',
];
}
}
推荐阅读
- python - Python matplotlib 代码,我不明白代码是做什么的
- java - java使用compuateifpresent计算哈希图不起作用
- mongodb - 您可以参考同一架构中的另一个字段来验证 mongodb 字段吗?
- python-3.x - pd.min() 函数不读取负值
- xcode - 如何使按钮在 alertController 中可见?
- asp.net-mvc - 为什么全球化日期格式在 Azure 中不起作用?
- excel - 有没有一种方法可以组合重复单元格是一列,然后添加它们相邻列的值
- php - 如何在 PHP 中 JSON 编码/解码后维护对象状态
- go - 如何初始化一次 zap 记录器并在其他 Go 文件中重用它?
- r - 在 Shiny 中,如何创建图形分数图表?