diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index eb2e892..96b52ec 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -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 diff --git a/src/FormRequest/CreateUserRequest.php b/src/FormRequest/CreateUserRequest.php index 6016905..5e0b740 100644 --- a/src/FormRequest/CreateUserRequest.php +++ b/src/FormRequest/CreateUserRequest.php @@ -39,4 +39,9 @@ class CreateUserRequest * @Assert\Length(min="32", max="32") */ public $inviteCode; + + public function __construct(string $inviteCode = null) + { + $this->inviteCode = $inviteCode; + } } \ No newline at end of file diff --git a/src/User/UserManager.php b/src/User/UserManager.php index cb686de..ce033cd 100644 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -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(); } diff --git a/templates/User/register.html.twig b/templates/User/register.html.twig index 50491ff..600b184 100644 --- a/templates/User/register.html.twig +++ b/templates/User/register.html.twig @@ -1,7 +1,11 @@ {% extends 'base.html.twig' %} {% block content %} + {% if inviteInvalid %} +