gpt4 book ai didi

Symfony 4 : allow only active accounts to login

转载 作者:行者123 更新时间:2023-12-01 11:14:11 25 4
gpt4 key购买 nike

所以我使用 symfony 4 并且我设法在用户的数据库表中添加了一个名为“Active”的字段,如果 active 等于 0,则该帐户未激活,反之亦然。
所以我想要做的是,每当帐户未激活的用户尝试登录时,都会显示一条消息或警报,并告诉他他的帐户当前处于非事件状态。活跃用户连接时没有任何问题。
我没有在这里使用 FOSUserBundle。那么我可以手动执行吗?

最佳答案

您可以使用 user checker 来完成此操作。 .
例如。:

namespace App\Security;

use App\Entity\User;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserChecker implements UserCheckerInterface
{
public function checkPreAuth(UserInterface $user)
{
if (!$user instanceof User) {
return;
}

if (!$user->isActive()) {
throw new CustomUserMessageAuthenticationException(
'Inactive account cannot log in'
);
}
}

public function checkPostAuth(UserInterface $user)
{
$this->checkPreAuth($user);
}
}
启用检查器:
security:
firewalls:
main:
// skipped for brevity
user_checker: App\Security\UserChecker

关于Symfony 4 : allow only active accounts to login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55164941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com