AdvancedUserInterface工具为User实体提供了isEnabled方法。但用户属性存储在会话中。在重新登录之前禁用用户将无法工作。
所以我需要按用户ID清楚的特定用户会话。 或者,我需要检查数据库来刷新序列化的用户数据。
正确的方法是什么,我该怎么做?
答案 0 :(得分:0)
我遇到了与FOSUserBundle相同的问题,我会禁用用户,但如果他们已登录,则可以在现有会话下继续。禁用仅在他们尝试再次登录时生效。
为了解决这个问题,我发现了一种使用Security Voter的不同方式。我创建了一个自定义选民,每次调用" - > isGranted"检查。我检查每个页面上的isGranted是否有各种ROLE级别。然后该选民检查用户是否已启用,如果他们不是选民投票失败并且不允许用户,则将他们返回登录屏幕。
我的自定义选民:
namespace AppBundle\Security;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Purpose: A Voter that checks if the user is still enabled. Default FOSUserBundle behavior allows disabled users to
* continue under their existing session. This voter is designed to prevent that
* Created by PhpStorm.
* User: Matt Emerson
* Date: 2/17/2018
* Time: 1:24 PM
*/
class userVoter extends Voter
{
protected function supports($attribute, $subject)
{
//this Voter is always available
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
} elseif ($user->isEnabled() === false) {
// the user is not enabled; deny access
return false;
}
//otherwise return true
return true;
}
}