Created
December 22, 2021 23:38
-
-
Save arcanisgk/1bd8ad91a924492bfb6b7a4515040f46 to your computer and use it in GitHub Desktop.
activation of user account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Entity\User; | |
use App\Form\AccountActivationType; | |
use Doctrine\Persistence\ManagerRegistry; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
use Symfony\Component\Routing\Annotation\Route; | |
class AccountActivationController extends AbstractController | |
{ | |
/** | |
* @Route("/account_activation", name="account_activation") | |
*/ | |
public function index( | |
Request $request, | |
ManagerRegistry $doctrine, | |
UserPasswordHasherInterface $passwordHasher | |
): Response { | |
$form = $this->CreateForm(AccountActivationType::class); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$email = $form['email']->getData(); | |
$plainPassword = $form['code']->getData(); | |
$em = $doctrine->getManager(); | |
$userObj = $doctrine->getRepository(User::class)->findOneBy(['email' => $email]); | |
$activation_hash = $userObj->getActivationHash(); | |
$user = new User(); | |
$user->setPassword($activation_hash); | |
if ($passwordHasher->isPasswordValid($user, $plainPassword)) { | |
$userObj->setActivated(true); | |
$userObj->setActivationHash(''); | |
$em->persist($userObj); | |
$em->flush(); | |
return $this->redirectToRoute('login'); | |
} else { | |
//add error flash | |
} | |
} | |
return $this->render('account_activation/index.html.twig', [ | |
'account_activation_form' => $form->createView(), | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment