User registration

This commit is contained in:
Simon Vieille 2015-11-21 15:04:41 +01:00
parent 2f7864be0f
commit 5135fd7f53
9 changed files with 202 additions and 9 deletions

View File

@ -26,6 +26,10 @@ download:
path: /download/{gist}/{commit}
defaults: {_controller: Gist\Controller\ViewController::downloadAction, _locale: en, commit: 0}
login_register:
path: /login
defaults: {_controller: Gist\Controller\LoginController::registerAction, _locale: en}
revisions:
path: /revs/{gist}
defaults: {_controller: Gist\Controller\ViewController::revisionsAction, _locale: en}

View File

@ -27,6 +27,17 @@ date:
footer:
text: '<p>Powered by <a href="https://gitlab.deblan.org/deblan/gist">GIST</a>, it''s open source :) - <a href="https://gitlab.deblan.org/deblan/gist#api">API</a></p>'
login:
register:
title: 'New account'
already_exists: 'This username is already registred!'
registred: 'Congratulations, your account is created!'
form:
username:
placeholder: 'Username'
password:
placeholder: 'Password'
form:
error:
not_blank: 'This value should not be blank bro!'

View File

@ -27,6 +27,18 @@ date:
footer:
text: '<p>Propulsé par <a href="https://gitlab.deblan.org/deblan/gist">GIST</a>, c''est libre :) - <a href="https://gitlab.deblan.org/deblan/gist#api">API</a></p>'
login:
register:
title: 'Nouveau compte'
already_exists: 'Ce nom d''utilisateur est déjà enregistré'
registred: 'Félicitation, votre compte a bien été créé !'
form:
username:
placeholder: 'Nom d''utilisateur'
password:
placeholder: 'Mot de passe'
form:
error:
not_blank: 'Vous devez saisir cette donnée.'

View File

@ -40,6 +40,9 @@ class UserCreateCommand extends Command
$password = $helper->ask($input, $output, $question);
}
$userProvider->registerUser($username, $password);
$user = $userProvider->createUser();
$user->setUsername($username);
$userProvider->registerUser($user, $password);
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Gist\Controller;
use Gist\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Silex\Application;
use Gist\Model\User;
use Gist\Form\UserRegisterForm;
/**
* Class LoginController
* @author Simon Vieille <simon@deblan.fr>
*/
class LoginController extends Controller
{
public function registerAction(Request $request, Application $app)
{
$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');
}
}
}
return $app['twig']->render(
'Login/register.html.twig',
[
'form' => $form->createView(),
'error' => isset($error) ? $error : '',
'success' => isset($success) ? $success : '',
]
);
}
}

View File

@ -15,7 +15,7 @@ abstract class AbstractForm
protected $translator;
public function __construct(FormFactory $formFactory, Translator $translator, array $data = array(), $formFactoryOptions = array())
public function __construct(FormFactory $formFactory, Translator $translator, $data = null, $formFactoryOptions = array())
{
$this->translator = $translator;

View File

@ -0,0 +1,52 @@
<?php
namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class UserRegisterForm
* @author Simon Vieille <simon@deblan.fr>
*/
class UserRegisterForm extends AbstractForm
{
public function build(array $options = array())
{
$this->builder->add(
'username',
'text',
array(
'required' => true,
'attr' => array(
'class' => 'form-control',
'placeholder' => $this->translator->trans('login.register.form.username.placeholder'),
),
'constraints' => array(
new NotBlank(array(
'message' => $this->translator->trans('form.error.not_blank'),
)),
),
)
);
$this->builder->add(
'password',
'password',
array(
'required' => true,
'attr' => array(
'class' => 'form-control',
'placeholder' => $this->translator->trans('login.register.form.password.placeholder'),
),
'trim' => false,
'constraints' => array(
new NotBlank(array(
'message' => $this->translator->trans('form.error.not_blank'),
)),
),
)
);
return $this->builder;
}
}

View File

@ -0,0 +1,56 @@
{% extends 'base.html.twig' %}
{% block title %}
{{ 'login.register.title'|trans }}
{% endblock %}
{% block langs %}
{% endblock %}
{% block body %}
<div class="row">
{% if error %}
<div class="col-md-12">
<div class="alert alert-warning">
{{ error }}
</div>
</div>
{% endif %}
{% if success %}
<div class="col-md-12">
<div class="alert alert-success">
{{ success }}
</div>
</div>
{% endif %}
<form action="{{ path('login_register') }}" method="post" id="main-form">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
{{ 'login.register.title'|trans }}
</div>
<div class="panel-body">
<p>
{{ form_errors(form.username) }}
{{ form_widget(form.username) }}
</p>
<p>
{{ form_errors(form.password) }}
{{ form_widget(form.password) }}
</p>
<p>
<input type="submit" class="btn btn-primary" value="{{ 'form.submit'|trans }}">
</p>
{{ form_rest(form) }}
</div>
</div>
</div>
</form>
</div>
{% endblock %}

View File

@ -58,18 +58,17 @@ class UserProvider implements UserProviderInterface
->count() > 0;
}
public function registerUser($username, $password)
public function createUser()
{
$user = new User();
return new User();
}
$salt = $this->saltGenerator->generate(64);
public function registerUser(User $user, $password)
{
$user->setSalt($this->saltGenerator->generate(64));
$user
->setUsername($username)
->setRoles('ROLE_USER')
->setSalt($salt);
$user
->setPassword($this->encoder->encodePassword($user, $password))
->save();