|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Form\UserProfileCompleteType; |
|
6
|
|
|
use App\Repository\RegistrationCodeRepositoryInterface; |
|
7
|
|
|
use App\Security\Registration\CodeAlreadyRedeemedException; |
|
8
|
|
|
use App\Security\Registration\RegistrationCodeManager; |
|
9
|
|
|
use App\Settings\RegistrationSettings; |
|
10
|
|
|
use SchulIT\CommonBundle\Helper\DateHelper; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
15
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
#[Route(path: '/register')] |
|
18
|
|
|
class RegistrationController extends AbstractController { |
|
19
|
|
|
|
|
20
|
|
|
private const CSRF_TOKEN_KEY = '_csrf_token'; |
|
21
|
|
|
private const CSRF_TOKEN_ID = 'registration'; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(private readonly TranslatorInterface $translator) |
|
24
|
|
|
{ |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
#[Route(path: '/redeem', name: 'redeem_registration_code')] |
|
28
|
|
|
public function redeem(Request $request, RegistrationCodeRepositoryInterface $codeRepository, RegistrationCodeManager $manager, |
|
29
|
|
|
DateHelper $dateHelper, TranslatorInterface $translator): Response { |
|
30
|
|
|
if(!$request->isMethod('POST')) { |
|
31
|
|
|
return $this->redirectToRoute('login'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$csrfToken = $request->request->get(self::CSRF_TOKEN_KEY); |
|
35
|
|
|
|
|
36
|
|
|
if ($this->isCsrfTokenValid(self::CSRF_TOKEN_ID, $csrfToken) !== true) { |
|
37
|
|
|
$this->addFlash('error', $this->getCsrfTokenMessage()); |
|
38
|
|
|
return $this->redirectToRoute('login'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$registrationCode = $request->request->get('code'); |
|
42
|
|
|
|
|
43
|
|
|
if(empty($registrationCode)) { |
|
44
|
|
|
$this->addFlash('error', 'register.redeem.error.invalid_request'); |
|
45
|
|
|
return $this->redirectToRoute('login'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$code = $codeRepository->findOneByCode($registrationCode); |
|
49
|
|
|
|
|
50
|
|
|
if($code === null) { |
|
51
|
|
|
$this->addFlash('error', 'register.redeem.error.not_found'); |
|
52
|
|
|
return $this->redirectToRoute('login'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if($manager->isRedeemed($code)) { |
|
56
|
|
|
$this->addFlash('error', 'register.redeem.error.already_redeemed'); |
|
57
|
|
|
return $this->redirectToRoute('login'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if($code->getValidFrom() !== null && $code->getValidFrom() > $dateHelper->getToday()) { |
|
61
|
|
|
$this->addFlash('error', $translator->trans('register.redeem.error.not_yet_valid', [ |
|
62
|
|
|
'%date%' => $code->getValidFrom()->format($translator->trans('date.format')) |
|
63
|
|
|
], 'security')); |
|
64
|
|
|
return $this->redirectToRoute('login'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->render('register/redeem.html.twig', [ |
|
68
|
|
|
'code' => $code |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
#[Route(path: '/complete', name: 'register')] |
|
73
|
|
|
public function register(Request $request, RegistrationSettings $settings, RegistrationCodeRepositoryInterface $codeRepository, |
|
74
|
|
|
RegistrationCodeManager $manager): Response { |
|
75
|
|
|
$registrationCode = $request->request->get('code'); |
|
76
|
|
|
|
|
77
|
|
|
if(empty($registrationCode)) { |
|
78
|
|
|
$this->addFlash('error', 'register.redeem.error.invalid_request'); |
|
79
|
|
|
return $this->redirectToRoute('login'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$code = $codeRepository->findOneByCode($registrationCode); |
|
83
|
|
|
|
|
84
|
|
|
if($code === null) { |
|
85
|
|
|
$this->addFlash('error', 'register.redeem.error.not_found'); |
|
86
|
|
|
return $this->redirectToRoute('login'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$user = $manager->getTemplateUser(); |
|
90
|
|
|
$form = $this->createForm(UserProfileCompleteType::class, $user, [ |
|
91
|
|
|
'username_suffix' => sprintf('@%s', $settings->getUsernameSuffix()) |
|
92
|
|
|
]); |
|
93
|
|
|
$form->handleRequest($request); |
|
94
|
|
|
|
|
95
|
|
|
if($form->isSubmitted() && $form->isValid()) { |
|
96
|
|
|
try { |
|
97
|
|
|
$manager->complete($code, $user, $form->get('password')->getData()); |
|
98
|
|
|
$this->addFlash('success', 'register.completed'); |
|
99
|
|
|
} catch (CodeAlreadyRedeemedException) { |
|
100
|
|
|
$this->addFlash('error', 'register.redeem.error.already_redeemed'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->redirectToRoute('login'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->render('register/complete.html.twig', [ |
|
107
|
|
|
'code' => $code, |
|
108
|
|
|
'form' => $form->createView() |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function getCsrfTokenMessage(): string { |
|
113
|
|
|
return $this->translator->trans('Invalid CSRF token.', [], 'security'); |
|
114
|
|
|
} |
|
115
|
|
|
} |