2015-11-21 15:04:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gist\Controller;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Gist\Model\User;
|
|
|
|
use Gist\Form\UserRegisterForm;
|
2015-11-21 18:28:48 +01:00
|
|
|
use Gist\Form\UserLoginForm;
|
2015-11-23 11:53:24 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2015-11-21 15:04:41 +01:00
|
|
|
|
|
|
|
/**
|
2016-11-13 00:44:23 +01:00
|
|
|
* Class LoginController.
|
|
|
|
*
|
2015-11-21 15:04:41 +01:00
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class LoginController extends Controller
|
|
|
|
{
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* Registration page.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
2018-08-20 17:05:36 +02:00
|
|
|
* @return Response
|
2016-11-13 00:44:23 +01:00
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function registerAction(Request $request)
|
2015-11-21 15:04:41 +01:00
|
|
|
{
|
2015-11-23 13:19:13 +01:00
|
|
|
$app = $this->getApp();
|
|
|
|
|
2017-06-20 16:46:10 +02:00
|
|
|
if (false === $app['settings']['security']['enable_registration']) {
|
2015-11-23 11:53:24 +01:00
|
|
|
return new Response('', 403);
|
|
|
|
}
|
|
|
|
|
2015-11-21 15:04:41 +01:00
|
|
|
$user = $app['user.provider']->createUser();
|
|
|
|
|
|
|
|
$form = new UserRegisterForm(
|
|
|
|
$app['form.factory'],
|
|
|
|
$app['translator'],
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
|
|
|
|
$form = $form->build()->getForm();
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
|
|
|
$form->submit($request);
|
|
|
|
|
|
|
|
if ($form->isValid()) {
|
|
|
|
if ($app['user.provider']->userExists($user->getUsername())) {
|
|
|
|
$error = $app['translator']->trans('login.register.already_exists');
|
|
|
|
} else {
|
|
|
|
$app['user.provider']->registerUser(
|
|
|
|
$user,
|
|
|
|
$user->getPassword()
|
|
|
|
);
|
|
|
|
|
|
|
|
$success = $app['translator']->trans('login.register.registred');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 09:56:51 +02:00
|
|
|
return $this->createResponse(
|
2015-11-21 15:04:41 +01:00
|
|
|
'Login/register.html.twig',
|
|
|
|
[
|
2016-11-13 00:44:23 +01:00
|
|
|
'form' => $form->createView(),
|
|
|
|
'error' => isset($error) ? $error : '',
|
2015-11-21 15:04:41 +01:00
|
|
|
'success' => isset($success) ? $success : '',
|
2018-09-11 13:15:12 +02:00
|
|
|
'no_cache' => true,
|
2015-11-23 13:19:13 +01:00
|
|
|
]
|
2015-11-21 15:04:41 +01:00
|
|
|
);
|
|
|
|
}
|
2015-11-23 13:19:13 +01:00
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* Login page.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
2018-08-20 17:05:36 +02:00
|
|
|
* @return Response
|
2016-11-13 00:44:23 +01:00
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function loginAction(Request $request)
|
2015-11-21 18:28:48 +01:00
|
|
|
{
|
2015-11-23 13:19:13 +01:00
|
|
|
$app = $this->getApp();
|
|
|
|
|
2017-06-20 16:46:10 +02:00
|
|
|
if (false === $app['settings']['security']['enable_login']) {
|
2015-11-23 11:53:24 +01:00
|
|
|
return new Response('', 403);
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:28:48 +01:00
|
|
|
$user = $app['user.provider']->createUser();
|
|
|
|
|
|
|
|
$form = new UserLoginForm(
|
|
|
|
$app['form.factory'],
|
|
|
|
$app['translator'],
|
|
|
|
$user,
|
|
|
|
['csrf_protection' => false]
|
|
|
|
);
|
|
|
|
|
|
|
|
$form = $form->build()->getForm();
|
|
|
|
|
2015-11-23 21:59:50 +01:00
|
|
|
if ($app['security.last_error']($request)) {
|
2015-11-21 18:28:48 +01:00
|
|
|
$error = $app['translator']->trans('login.login.invalid');
|
|
|
|
}
|
|
|
|
|
2018-08-21 09:56:51 +02:00
|
|
|
return $this->createResponse(
|
2015-11-21 18:28:48 +01:00
|
|
|
'Login/login.html.twig',
|
|
|
|
[
|
2016-11-13 00:44:23 +01:00
|
|
|
'form' => $form->createView(),
|
2015-11-21 18:28:48 +01:00
|
|
|
'error' => isset($error) ? $error : '',
|
2018-09-11 13:15:12 +02:00
|
|
|
'no_cache' => true,
|
2015-11-23 13:19:13 +01:00
|
|
|
]
|
2015-11-21 18:28:48 +01:00
|
|
|
);
|
|
|
|
}
|
2015-11-21 15:04:41 +01:00
|
|
|
}
|