-
Notifications
You must be signed in to change notification settings - Fork 7
/
RegistrationController.php
170 lines (147 loc) · 6.76 KB
/
RegistrationController.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* This file is part of the Tecnocreaciones package.
*
* (c) www.tecnocreaciones.com.ve
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tecnocreaciones\Bundle\AjaxFOSUserBundle\Controller;
use FOS\RestBundle\View\View;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use Tecnocreaciones\Bundle\AjaxFOSUserBundle\Event\GetResponseUserEvent;
use Tecnocreaciones\Bundle\AjaxFOSUserBundle\FOSUserEvents;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Manejador de registro de FOSUserBundle por Ajax
*
* @author Carlos Mendoza <inhack20@tecnocreaciones.com>
*/
class RegistrationController extends BaseController
{
public function registerAction(Request $request)
{
if($request->isXmlHttpRequest()){
$view = View::create();
$view->setFormat('json');
$response = new JsonResponse();
//Backward compatibility with Fos User 1.3
if(!class_exists('FOS\UserBundle\FOSUserEvents')){
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
$message = $this->trans('registration.check_email',array('%email%' => $user->getEmail()));
} else {
$authUser = true;
$route = 'fos_user_registration_confirmed';
$message = $this->trans('registration.confirmed',array('%username%' => $user->getUserName()));
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
$response = new JsonResponse();
$data = array(
'message' => $message,
'targetUrl' => $url,
'authUser' => $authUser,
);
$response->setData($data);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
}else{
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.registration.form.factory');
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');
$user = $userManager->createUser();
$user->setEnabled(true);
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form = $formFactory->createForm();
$form->setData($user);
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
if ($form->isValid()) {
$targetUrl = '';
if($this->getSession()){
$session = $this->getSession();
if(($token = $this->container->get('security.token_storage')->getToken()) && is_a($token, 'Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken')){
$targetUrl = $session->get('_security.' .$token->getProviderKey(). '.target_path');
}
}
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('fos_user_registration_confirmed');
$response = new JsonResponse();
$data = array(
'message' => $this->trans('registration.confirmed',array('%username%' => $user->getUserName())),
'targetUrl' => $targetUrl,
);
$response->setData($data);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
}
}
$view->setData($form);
return $this->handleView($view);
}else{
return parent::registerAction($request);
}
}
/**
* Convert view into a response object.
*
* Not necessary to use, if you are using the "ViewResponseListener", which
* does this conversion automatically in kernel event "onKernelView".
*
* @param View $view
*
* @return Response
*/
protected function handleView(View $view)
{
return $this->container->get('fos_rest.view_handler')->handle($view);
}
/**
* Sesion
* @return \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected function getSession()
{
return $this->container->get('session');
}
/**
*
* @param type $message
* @param array $params
* @return type
*/
private function trans($message, array $params = array())
{
return $this->container->get('translator')->trans($message, $params, 'FOSUserBundle');
}
}