Skip to content

Commit

Permalink
Merge 5cf443a into a662ff1
Browse files Browse the repository at this point in the history
  • Loading branch information
stejes committed Jun 26, 2018
2 parents a662ff1 + 5cf443a commit e3b7476
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 56 deletions.
4 changes: 4 additions & 0 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ accounts_view_register_success:
methods: [GET]
requirements:
_locale: nl|fr
accounts_view_login:
path: /{_locale}/view/accounts/login
controller: VSV\GVQ_API\Account\Controllers\AccountViewController::login
methods: [GET, POST]
accounts_view_activation:
path: /{_locale}/view/accounts/activation/{urlSuffix}
controller: VSV\GVQ_API\Account\Controllers\AccountViewController::activation
Expand Down
57 changes: 57 additions & 0 deletions src/Account/Controllers/AccountViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\GroupSequence;
use VSV\GVQ_API\Account\Forms\LoginFormType;
use VSV\GVQ_API\Account\Forms\RegistrationFormType;
use VSV\GVQ_API\Company\Repositories\CompanyRepository;
use VSV\GVQ_API\Mail\Service\MailService;
use VSV\GVQ_API\Registration\Repositories\RegistrationRepository;
use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;
use VSV\GVQ_API\Registration\ValueObjects\UrlSuffixGenerator;
use VSV\GVQ_API\User\Repositories\UserRepository;
use VSV\GVQ_API\User\ValueObjects\Email;

class AccountViewController extends AbstractController
{
Expand All @@ -25,6 +27,11 @@ class AccountViewController extends AbstractController
*/
private $registrationFormType;

/**
* @var LoginFormType
*/
private $loginFormType;

/**
* @var TranslatorInterface
*/
Expand Down Expand Up @@ -95,6 +102,7 @@ public function __construct(
$this->logger = $logger;

$this->registrationFormType = new RegistrationFormType();
$this->loginFormType = new LoginFormType();
}

/**
Expand Down Expand Up @@ -158,6 +166,38 @@ public function success(): Response
return $this->render('accounts/register_success.html.twig');
}

/**
* @param Request $request
* @return Response
*/
public function login(Request $request): Response
{
$form = $this->createLoginForm();
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();

$user = $this->userRepository->getByEmail(new Email($data['email']));

if ($user && $user->getPassword() && $user->getPassword()->verifies($data['password'])) {
if ($user->isActive()) {
return $this->redirectToRoute('questions_view_index');
}
$this->addFlash('warning', $this->translator->trans('Account inactive'));
} else {
$this->addFlash('danger', $this->translator->trans('Invalid credentials'));
}
}

return $this->render(
'accounts/login.html.twig',
[
'form' => $form->createView(),
]
);
}

/**
* @param string $urlSuffix
* @return Response
Expand Down Expand Up @@ -206,4 +246,21 @@ private function createRegisterForm(): FormInterface

return $formBuilder->getForm();
}

/**
* @return FormInterface
*/
private function createLoginForm(): FormInterface
{
$formBuilder = $this->createFormBuilder();

$this->loginFormType->buildForm(
$formBuilder,
[
'translator' => $this->translator,
]
);

return $formBuilder->getForm();
}
}
69 changes: 69 additions & 0 deletions src/Account/Forms/LoginFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Account\Forms;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;

class LoginFormType extends AbstractType
{
/**
* @inheritdoc
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
/** @var TranslatorInterface $translator */
$translator = $options['translator'];

$builder
->add(
'email',
EmailType::class,
[
'constraints' => [
new NotBlank(
[
'message' => $translator->trans('Empty field'),
]
),
new Email(
[
'message' => $translator->trans('Invalid email pattern'),
]
),
],
]
)
->add(
'password',
PasswordType::class,
[
'constraints' => [
new NotBlank(
[
'message' => $translator->trans('Empty field'),
]
),
],
]
);
}

/**
* @inheritdoc
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'translator' => null,
]
);
}
}
35 changes: 35 additions & 0 deletions templates/accounts/login.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends 'base.html.twig' %}

{% block title %}{% trans %}Inloggen{% endtrans %}{% endblock %}

{% block content %}
<div class="card">
<div class="card-header">
<div class="card-title">{% trans %}Inloggen{% endtrans %}</div>
</div>

<div class="card-body">
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
<div class="form-group">
<label for="form_email">{% trans %}E-mail{% endtrans %}</label>
{{ form_widget(form.email) }}
{{ form_errors(form.email) }}
</div>

<div class="form-group">
<label for="form_password">{% trans %}Wachtwoord{% endtrans %}</label>
{{ form_widget(form.password) }}
{{ form_errors(form.password) }}
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">{% trans %}Inloggen{% endtrans %}</button>
</div>
{{ form_end(form) }}
</div>

<div class="card-footer">
<div class="card-link"><a href="#">{% trans %}Wachtwoord vergeten{% endtrans %}</a></div>
</div>
</div>
{% endblock %}
26 changes: 26 additions & 0 deletions tests/Account/Forms/LoginFormTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Account\Forms;

use VSV\GVQ_API\Common\Forms\ExtensionsAwareTypeTestCase;

class LoginFormTypeTest extends ExtensionsAwareTypeTestCase
{
/**
* @test
*/
public function it_can_be_created(): void
{
$form = $this->factory->create(
LoginFormType::class,
null,
[
'translator' => $this->translator,
]
);

$form->submit([]);

$this->assertTrue($form->isSynchronized());
}
}
4 changes: 2 additions & 2 deletions tests/Account/Forms/RegistrationFormTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace VSV\GVQ_API\Account\Forms;

use VSV\GVQ_API\Common\Forms\AbstractFormTypeTest;
use VSV\GVQ_API\Common\Forms\ExtensionsAwareTypeTestCase;

class RegistrationFormTypeTest extends AbstractFormTypeTest
class RegistrationFormTypeTest extends ExtensionsAwareTypeTestCase
{
/**
* @test
Expand Down
54 changes: 0 additions & 54 deletions tests/Common/Forms/AbstractFormTypeTest.php

This file was deleted.

5 changes: 5 additions & 0 deletions translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Registration success extra: '__Het kan enkele minuten duren voor u de mail ontva
Naar GVQ site: '_Naar GVQ site'
Employees not empty: '__Het aantal medewerkers mag niet leeg zijn.'
Employees positive: '__Het aantal medewerkers moet {{ limit }} of groter zijn.'
Inloggen: __Inloggen
Wachtwoord vergeten: '__Wachtwoord vergeten?'
Account inactive: '__Gelieve uw account te activeren via de link in de bevestigingsmail van uw registratie.'
Invalid credentials: '__De gegevens die u opgaf zijn niet juist.'
Login failed: '__Er liep iets mis'
Activation.mail.subject: '__Activatie Grote Verkeersquiz 2018'
Activation: __Activatie account
Activation.success.title: '__Activatie gelukt!'
Expand Down
5 changes: 5 additions & 0 deletions translations/messages.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Registration success extra: 'Het kan enkele minuten duren voor u de mail ontvang
Naar GVQ site: 'Naar GVQ site'
Employees not empty: 'Het aantal medewerkers mag niet leeg zijn.'
Employees positive: 'Het aantal medewerkers moet {{ limit }} of groter zijn.'
Inloggen: Inloggen
Wachtwoord vergeten: 'Wachtwoord vergeten?'
Account inactive: 'Gelieve uw account te activeren via de link in de bevestigingsmail van uw registratie.'
Invalid credentials: 'De gegevens die u opgaf zijn niet juist.'
Login failed: 'Er liep iets mis.'
Activation.mail.subject: 'Activatie Grote Verkeersquiz 2018'
Activation: Activatie account
Activation.success.title: 'Activatie gelukt!'
Expand Down

0 comments on commit e3b7476

Please sign in to comment.