1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/**
* User registration
*
* @Route("/security/registration/{shortForm}", name="security.registration")
*
* @param bool $shortForm
* @param Request $request
* @param EntityManagerInterface $manager
* @param UserPasswordEncoderInterface $encoder
* @param AuthorizationCheckerInterface $authChecker
* @param \Swift_Mailer $mailer
* @param UserRepository $userRepository
*
* @return Response
*/
public function registration(bool $shortForm = false, Request $request, EntityManagerInterface $manager, UserPasswordEncoderInterface $encoder,
AuthorizationCheckerInterface $authChecker, \Swift_Mailer $mailer, UserRepository $userRepository): Response
{
$this->session->set('errorFomRegistration', false);
$administrator = $userRepository->findOneBy(array('name' => 'administrator'));
$user = new User;
$isAdmin = false;
if($authChecker->isGranted('ROLE_ADMIN'))
{
$isAdmin = true;
}
if(! $shortForm)
{
$form = $this->createForm(UserType::class, $user, array(
'isAdmin' => $isAdmin,
'roles' => $this->getParameter('security.role_hierarchy.roles')
)
)
;
$template = 'security/registration.html.twig';
}
else
{
$form = $this->createForm(ShortRegistrationType::class, $user);
$template = 'security/shortRegistration.html.twig';
}
$form->handleRequest($request);
if ($form->isSubmitted())
{
if ($form->isValid())
{
$password = $user->getPassword();
$hash = $encoder->encodePassword($user, $password);
$user->setPassword($hash);
if (! $isAdmin)
{
$user->setRoles(['ROLE_USER']);
}
$manager->persist($user);
$manager->flush();
$mail = new Mail;
$mail->setReceiver($user)
->setSubject($this->getParameter('registration_email_subject'))
->setSender($administrator)
->setMessage('Account creation')
->setBody($this->renderView(
'security/registrationEmail.html.twig',
['user' => $user]
)
)
;
if ($mail->sendEmail($mailer))
{
$manager->persist($mail);
$manager->flush();
$this->addFlash('success', "An email was sent to the completed email address to activate your account.");
}
else
{
$this->addFlash('danger', "An email could not be sent to the completed email address to activate your account.");
}
}
else
{
$this->session->set('errorFomRegistration', true);
\dump($this->session->get('errorFomRegistration'));
}
}
return $this->render($template, array(
'current_menu' => 'registration',
'bodyId' => 'userRegistration',
'form' => $form->createView()
)
)
;
} |
Partager