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\FormRequest\CreateUserRequest;
use App\Repository\InviteRepository;
use App\User\Exception\InvalidInviteException;
use App\User\{InviteManager, UserManager};
use Doctrine\ORM\EntityManagerInterface;
@ -19,21 +20,27 @@ class UserController extends Controller
Request $request,
EntityManagerInterface $em,
UserManager $userManager,
InviteManager $inviteManager
InviteManager $inviteManager,
InviteRepository $inviteRepo
): Response {
$createUserRequest = new CreateUserRequest();
$createUserRequest->inviteCode = $inviteCode;
$createUserRequest = new CreateUserRequest($inviteCode);
$form = $this->createRegisterForm($createUserRequest, $inviteCode);
$inviteInvalid = false;
if (null === $invite = $inviteRepo->findOneBy(['code' => $inviteCode, 'usedBy' => null])) {
$inviteInvalid = true;
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$user = $userManager->createUserByInviteCode(
$user = $userManager->createUserByInvite(
$createUserRequest->username,
$createUserRequest->password,
$createUserRequest->email,
$createUserRequest->inviteCode
$invite
);
$inviteManager->createInvitesForUser($user);
@ -49,7 +56,10 @@ class UserController extends Controller
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

View file

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

View file

@ -43,10 +43,9 @@ class UserManager
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 = $this->inviteRepo->findOneBy(['code' => $inviteCode, 'usedBy' => null])) {
if (null !== $invite->getUsedBy()) {
throw new InvalidInviteException();
}

View file

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