Telling user when he's using invalid invite code before he filled the form.

This commit is contained in:
Alexey Skobkin 2018-06-29 02:14:36 +03:00
parent 866685c90c
commit e7d9fdaecd
4 changed files with 27 additions and 9 deletions

View file

@ -4,6 +4,7 @@ namespace App\Controller;
use App\Form\{CreateUserRequestType}; use App\Form\{CreateUserRequestType};
use App\FormRequest\CreateUserRequest; use App\FormRequest\CreateUserRequest;
use App\Repository\InviteRepository;
use App\User\Exception\InvalidInviteException; use App\User\Exception\InvalidInviteException;
use App\User\{InviteManager, UserManager}; use App\User\{InviteManager, UserManager};
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -19,21 +20,27 @@ class UserController extends Controller
Request $request, Request $request,
EntityManagerInterface $em, EntityManagerInterface $em,
UserManager $userManager, UserManager $userManager,
InviteManager $inviteManager InviteManager $inviteManager,
InviteRepository $inviteRepo
): Response { ): Response {
$createUserRequest = new CreateUserRequest(); $createUserRequest = new CreateUserRequest($inviteCode);
$createUserRequest->inviteCode = $inviteCode;
$form = $this->createRegisterForm($createUserRequest, $inviteCode); $form = $this->createRegisterForm($createUserRequest, $inviteCode);
$inviteInvalid = false;
if (null === $invite = $inviteRepo->findOneBy(['code' => $inviteCode, 'usedBy' => null])) {
$inviteInvalid = true;
}
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
try { try {
$user = $userManager->createUserByInviteCode( $user = $userManager->createUserByInvite(
$createUserRequest->username, $createUserRequest->username,
$createUserRequest->password, $createUserRequest->password,
$createUserRequest->email, $createUserRequest->email,
$createUserRequest->inviteCode $invite
); );
$inviteManager->createInvitesForUser($user); $inviteManager->createInvitesForUser($user);
@ -49,7 +56,10 @@ class UserController extends Controller
return $this->redirectToRoute('index'); return $this->redirectToRoute('index');
} }
return $this->render('User/register.html.twig', ['form' => $form->createView()]); return $this->render('User/register.html.twig', [
'form' => $form->createView(),
'inviteInvalid' => $inviteInvalid,
]);
} }
private function createRegisterForm(CreateUserRequest $createUserRequest, string $inviteCode): FormInterface private function createRegisterForm(CreateUserRequest $createUserRequest, string $inviteCode): FormInterface

View file

@ -39,4 +39,9 @@ class CreateUserRequest
* @Assert\Length(min="32", max="32") * @Assert\Length(min="32", max="32")
*/ */
public $inviteCode; public $inviteCode;
public function __construct(string $inviteCode = null)
{
$this->inviteCode = $inviteCode;
}
} }

View file

@ -43,10 +43,9 @@ class UserManager
return $user; return $user;
} }
public function createUserByInviteCode(string $username, string $password, string $email, string $inviteCode, array $roles = self::DEFAULT_ROLES): User public function createUserByInvite(string $username, string $password, string $email, Invite $invite, array $roles = self::DEFAULT_ROLES): User
{ {
/** @var Invite $invite */ if (null !== $invite->getUsedBy()) {
if (null === $invite = $this->inviteRepo->findOneBy(['code' => $inviteCode, 'usedBy' => null])) {
throw new InvalidInviteException(); throw new InvalidInviteException();
} }

View file

@ -1,7 +1,11 @@
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block content %} {% block content %}
{% if inviteInvalid %}
<h1>Invalid invite</h1>
{% else %}
<div id="form-register"> <div id="form-register">
{{ form(form) }} {{ form(form) }}
</div> </div>
{% endif %}
{% endblock %} {% endblock %}