我正在尝试注册(通过电子邮件确认)并登录我的用户,而不使用FOSUserBundle。
我从这段视频中得到了这个主意:https://www.grafikart.fr/tutoriels/confirmation-compte-824
我现在所做的:
1-用户注册
2-向用户发送一封电子邮件以进行激活
3-如果用户点击链接,则激活Confirmation_token = null并 已启用= true
但是
我不知道如何测试登录功能并添加条件Confirmation_login = null,只允许激活的帐户登录。
此security.yml
"msg": "GeoIP [u'1.5.0']"
"msg": "GConf2 [u'3.2.6']"
这是我的registrationController:
encoders:
AppBundle\Entity\User: bcrypt
providers:
db_provider:
entity:
class: AppBundle\Entity\User
property: username
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
provider: db_provider
form_login:
login_path: login
check_path: login
logout:
path: /logout
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
这是电子邮件中用于激活帐户的链接:
/**
* @Route("/register")
* @param Request $request
* @Template()
* @return \Symfony\Component\HttpFoundation\Response
*/
public function registerAction(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
if ($form->isSubmitted() && $form->isValid()) {
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$em->persist($user);
$em->flush();
$confirmationToken = $user->getConfirmationToken();
$username = $user->getUsername();
$userId = $user->getId();
$subject = 'Account activation';
$email = $user->getEmail();
/* $renderedTemplate = $this->render('AppBundle:Register:registrationConfirmed.html.twig', array(
'username' => $username,
'confirmationToken' => $confirmationToken
));*/
$message = (new \Swift_Message('Hello Email'))
->setSubject($subject)
->setFrom($this->container->getParameter('mailer_user'))
->setTo($email)
->setBody(
$this->renderView(
'AppBundle:Register:registrationConfirmed.html.twig', array('id' => $userId,'username' => $username,'confirmationToken' => $confirmationToken)), 'text/html');
$this->get('mailer')->send($message);
$this->addFlash('success', 'Genus created!');
return $this->redirectToRoute('login');
}
return $this->render('AppBundle:Register:register.html.twig', array('form' => $form->createView()));
}
/**
* @Route("/confirm/{id}/{confirmationToken}", name="confirmation_path")
*/
public function confirmAction(Request $request, $id,$confirmationToken)
{
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AppBundle:User');
//$confirmationToken = $user->getConfirmationToken();
$user = $repository->findUserByIdAndConfirmationToken($id,$confirmationToken);
if (!$user)
{
throw $this->createNotFoundException('We couldn\'t find an account for that confirmation token');
}
else
{
$user= $repository->updateConfirmationTokenAfterValidation($id,$confirmationToken);
// $user->setEnabled(true);
// $user->setConfirmationToken(null);
//$em->persist($user);
// $em->flush();
}
return $this->redirectToRoute('login');
}
并且要登录,我使用了loginAction,所以如何在这里测试是否ConfirmToken = null以仅允许注册用户登录
<a href="{{ url('confirmation_path', {'id': id, 'confirmationToken': confirmationToken } ) }}">Activate</a>
我的login.html.twig
/**
* @Route("/login",name="login")
*/
public function loginAction()
{
$helper = $this->get('security.authentication_utils');
return $this->render('AppBundle:Login:login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
]);
}
更新
我已经实现了Userchecker并且可以工作(我不知道这是否是更好的方法)
<form action="{{ path('login') }}" method="post">
<div class="form-group">
<label for="username">{{ 'label.username'|trans }}</label>
<input type="text" id="username" name="_username" class="form-control"/>
</div>
<div class="form-group">
<label for="password">{{ 'label.password'|trans }}</label>
<input type="password" id="password" name="_password" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">
<i class="fa fa-sign-in" aria-hidden="true"></i> {{ 'action.sign_in'|trans }}
</button>
</form>