gpt4 book ai didi

php - Symfony 3,填充 token 并刷新用户

转载 作者:行者123 更新时间:2023-12-03 01:44:17 24 4
gpt4 key购买 nike

repository with issue

我有一个带有电子邮件字段的实体用户表单:

->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
new Email([
'checkMX' => true,
])
],
'required' => true
])

当我编辑电子邮件到 test@gmail.com1 并提交表单时,它显示错误“此值不是有效的电子邮件地址。”好的,但是在 symfony 将错误的电子邮件填充到 token 中之后,当我转到任何其他页面或只是重新加载页面时,我得到了这个:

WARNING security Username could not be found in the selected user provider.

我认为问题是:为什么 symfony 会填充验证失败的错误电子邮件到 token 中,以及如何阻止它?

Controller :

public function meSettingsAction(Request $request)
{

$user = $this->getUser();
$userUnSubscribed = $this->getDoctrine()->getRepository('AppBundle:UserUnsubs')->findOneBy(
[
'email' => $user->getEmail(),
]
);

$form = $this->createForm(UserSettingsType::class, $user);
$form->get('subscribed')->setData(!(bool)$userUnSubscribed);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/**
* @var $user User
*/
$user = $form->getData();

/** @var UploadedFile $avatar */
$avatar = $request->files->get('user_settings')['photo'];

$em = $this->getDoctrine()->getManager();

if ($avatar) {
$avatar_content = file_get_contents($avatar->getRealPath());
$avatarName = uniqid().'.jpg';
$oldAvatar = $user->getPhoto();
$user
->setState(User::PHOTO_STATE_UNCHECKED)
->setPhoto($avatarName);
$gearmanClient = $this->get('gearman.client');
$gearmanClient->doBackgroundDependsOnEnv(
'avatar_content_upload',
serialize(['content' => $avatar_content, 'avatarName' => $avatarName, 'oldAvatar' => $oldAvatar])
);
}

$subscribed = $form->get('subscribed')->getData();
if ((bool)$userUnSubscribed && $subscribed) {
$em->remove($userUnSubscribed);
} elseif (!(bool)$userUnSubscribed && !$subscribed) {
$userUnSubscribed = new UserUnsubs();
$userUnSubscribed->setEmail($form->get('email')->getData())->setTs(time());
$em->persist($userUnSubscribed);
}
$user->setLastTs(time());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

$this->get('user.manager')->refresh($user);

return $this->redirectToRoute('me');
}

return $this->render(
':user:settings.html.twig',
[
'form' => $form->createView(),
]
);
}

UPD:如果我在 OAuthProvider 中进行更改,它就可以正常工作:

/**
* @param \Symfony\Component\Security\Core\User\UserInterface $user
*
* @return \Symfony\Component\Security\Core\User\UserInterface
*/
public function refreshUser(UserInterface $user)
{
return $this->loadUserByUsername($user->getName());
}

至:

/**
* @param \Symfony\Component\Security\Core\User\UserInterface $user
*
* @return \Symfony\Component\Security\Core\User\UserInterface
*/
public function refreshUser(UserInterface $user)
{
return $this->userManager($user->getId());
}

但这似乎是肮脏的黑客行为。

谢谢。

最佳答案

即使电子邮件限制停止刷新,您的用户 token 似乎也会通过表单进行更新。

您能检查一下您的表单是否通过了 isValid 函数吗?您可以尝试使用事件监听器或验证器来避免它。

通过事件 SUBMIT,您应该能够检查电子邮件完整性,然后添加 FormError 以避免刷新用户。

关于php - Symfony 3,填充 token 并刷新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433448/

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