|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AWurth\SilexUser\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AWurth\SilexUser\Entity\UserInterface; |
|
6
|
|
|
use AWurth\SilexUser\Event\FilterUserResponseEvent; |
|
7
|
|
|
use AWurth\SilexUser\Event\FormEvent; |
|
8
|
|
|
use AWurth\SilexUser\Event\GetResponseUserEvent; |
|
9
|
|
|
use AWurth\SilexUser\Event\Events; |
|
10
|
|
|
use AWurth\SilexUser\Form\RegistrationFormType; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Silex User Registration Controller. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Alexis Wurth <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class RegistrationController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
public function registerAction(Request $request) |
|
23
|
|
|
{ |
|
24
|
|
|
$userManager = $this->getUserManager(); |
|
25
|
|
|
$dispatcher = $this->getDispatcher(); |
|
26
|
|
|
|
|
27
|
|
|
$user = $userManager->createUser(); |
|
28
|
|
|
$user->setEnabled(true); |
|
29
|
|
|
|
|
30
|
|
|
$event = new GetResponseUserEvent($user, $request); |
|
31
|
|
|
$dispatcher->dispatch(Events::REGISTRATION_INITIALIZE, $event); |
|
32
|
|
|
|
|
33
|
|
|
if (null !== $event->getResponse()) { |
|
34
|
|
|
return $event->getResponse(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$form = $this->createForm(RegistrationFormType::class, $user); |
|
38
|
|
|
|
|
39
|
|
|
$form->handleRequest($request); |
|
40
|
|
|
|
|
41
|
|
|
if ($form->isSubmitted()) { |
|
42
|
|
|
if ($form->isValid()) { |
|
43
|
|
|
$event = new FormEvent($form, $request); |
|
44
|
|
|
$dispatcher->dispatch(Events::REGISTRATION_SUCCESS, $event); |
|
45
|
|
|
|
|
46
|
|
|
$userManager->updateUser($user); |
|
47
|
|
|
|
|
48
|
|
|
$response = $event->getResponse(); |
|
|
|
|
|
|
49
|
|
|
if (null === $response) { |
|
50
|
|
|
$response = $this->redirect('silex_user.registration_confirmed'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$dispatcher->dispatch(Events::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
return $response; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$event = new FormEvent($form, $request); |
|
59
|
|
|
$dispatcher->dispatch(Events::REGISTRATION_FAILURE, $event); |
|
60
|
|
|
|
|
61
|
|
|
$response = $event->getResponse(); |
|
|
|
|
|
|
62
|
|
|
if (null !== $response) { |
|
63
|
|
|
return $response; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->render('silex_user/registration/register.twig', [ |
|
68
|
|
|
'form' => $form->createView() |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function checkEmailAction() |
|
73
|
|
|
{ |
|
74
|
|
|
$session = $this->getSession(); |
|
75
|
|
|
$email = $session->get('silex_user_confirmation_email'); |
|
76
|
|
|
|
|
77
|
|
|
if (empty($email)) { |
|
78
|
|
|
return $this->redirect('silex_user.register'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$session->remove('silex_user_confirmation_email'); |
|
82
|
|
|
$user = $this->getUserManager()->findUserByEmail($email); |
|
83
|
|
|
|
|
84
|
|
|
if (null === $user) { |
|
85
|
|
|
throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->render('silex_user/registration/check_email.twig', [ |
|
89
|
|
|
'user' => $user |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function confirmAction(Request $request, $token) |
|
94
|
|
|
{ |
|
95
|
|
|
$userManager = $this->getUserManager(); |
|
96
|
|
|
|
|
97
|
|
|
$user = $userManager->findUserByConfirmationToken($token); |
|
98
|
|
|
|
|
99
|
|
|
if (null === $user) { |
|
100
|
|
|
throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$dispatcher = $this->getDispatcher(); |
|
104
|
|
|
|
|
105
|
|
|
$user->setConfirmationToken(null); |
|
106
|
|
|
$user->setEnabled(true); |
|
107
|
|
|
|
|
108
|
|
|
$event = new GetResponseUserEvent($user, $request); |
|
109
|
|
|
$dispatcher->dispatch(Events::REGISTRATION_CONFIRM, $event); |
|
110
|
|
|
|
|
111
|
|
|
$userManager->updateUser($user); |
|
112
|
|
|
|
|
113
|
|
|
$response = $event->getResponse(); |
|
114
|
|
|
if (null === $response) { |
|
115
|
|
|
$response = $this->redirect('silex_user.registration_confirmed'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$dispatcher->dispatch(Events::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user, $request, $response)); |
|
119
|
|
|
|
|
120
|
|
|
return $response; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function confirmedAction() |
|
124
|
|
|
{ |
|
125
|
|
|
$user = $this->getUser(); |
|
126
|
|
|
if (!is_object($user) || !$user instanceof UserInterface) { |
|
127
|
|
|
throw new AccessDeniedException('This user does not have access to this section.'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this->render('silex_user/registration/confirmed.twig'); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.