FOSUserBundle 如何在登录时规范化用户名?
FOSUserBundle How to normalize UserName at login?
我在 Symfony 3.2 中使用 FOSUB
我希望 "normalize" 用户登录时的用户名。
通过 "normalize",我的意思是我有一个 "NormalizeService" 被调用 在 之前检查数据库(如果用户存在)。
该过程将是:
$normalizedUsername = $NormalizeService->normalize($providedUsername);
logUser($normalizedUsername, $providedPassword); // this is a simple login as usual
您认为最好的方法是什么?创建一个 custom UserProvider ?我不确定这是我需要的。
此致
您可以创建一个 PRE_SUBMIT FormEvent,在其中规范化用户名。
有关详细信息,请参阅 this。
就像 FOSUserBundle 的 documentation 说的那样:
FOSUserBundle stores canonicalized versions of the username and the email which are used when querying and checking for uniqueness.
您只需创建一个 class 实现 CanonicalizerInterface
namespace AppBundle\Util;
use FOS\UserBundle\Util\CanonicalizerInterface;
class MyCanonicalizer implements CanonicalizerInterface
{
private $normalizeService;
public function __construct(NormalizeService $normalizeService)
{
$this->normalizeService = $normalizeService;
}
public function canonicalize($string)
{
if (null === $string) {
return null;
}
return $this->normalizeService->normalize($string);
}
}
将其注册为服务:
# app/config/services.yml
services:
app.my_canonicalizer:
class: AppBundle\Util\MyCanonicalizer
arguments: ['@normalize_service'] # your NormalizeService
public: false
然后配置 FOSUserBundle 以使用它:
# app/config/config.yml
fos_user:
service:
email_canonicalizer: app.my_canonicalizer
我在 Symfony 3.2 中使用 FOSUB 我希望 "normalize" 用户登录时的用户名。 通过 "normalize",我的意思是我有一个 "NormalizeService" 被调用 在 之前检查数据库(如果用户存在)。 该过程将是:
$normalizedUsername = $NormalizeService->normalize($providedUsername);
logUser($normalizedUsername, $providedPassword); // this is a simple login as usual
您认为最好的方法是什么?创建一个 custom UserProvider ?我不确定这是我需要的。
此致
您可以创建一个 PRE_SUBMIT FormEvent,在其中规范化用户名。
有关详细信息,请参阅 this。
就像 FOSUserBundle 的 documentation 说的那样:
FOSUserBundle stores canonicalized versions of the username and the email which are used when querying and checking for uniqueness.
您只需创建一个 class 实现 CanonicalizerInterface
namespace AppBundle\Util;
use FOS\UserBundle\Util\CanonicalizerInterface;
class MyCanonicalizer implements CanonicalizerInterface
{
private $normalizeService;
public function __construct(NormalizeService $normalizeService)
{
$this->normalizeService = $normalizeService;
}
public function canonicalize($string)
{
if (null === $string) {
return null;
}
return $this->normalizeService->normalize($string);
}
}
将其注册为服务:
# app/config/services.yml
services:
app.my_canonicalizer:
class: AppBundle\Util\MyCanonicalizer
arguments: ['@normalize_service'] # your NormalizeService
public: false
然后配置 FOSUserBundle 以使用它:
# app/config/config.yml
fos_user:
service:
email_canonicalizer: app.my_canonicalizer