createsUserAccount = $createsUserAccount; } /** * @param Request $request * @return JsonResponse * @throws InternalServerErrorException * @throws MalformedRequestException */ public function execute(Request $request) { // throws malformed request exception if the password is invalid if ($this->checkPasswordAndThrowIfInvalid($request->get('password')) === false) { throw new MalformedRequestException('Password strength is insufficient'); } $invitation = UserInvitation::where('email', $request->input('email')) ->where('hash', $request->input('hash')) ->where('is_complete', false)->first(); if(!$invitation){ throw new MalformedRequestException('invitation hash is invalid'); } $invite = new UserInvitationObject($invitation->role_id, $invitation->email, $invitation->hash); try { // the email doesn't exist in the database, so create the user $object = new UserObject($request->get('first_name'), $request->get('last_name'), $invite->getInviteeEmail(), $request->get('password'), $invite->getRole(), true); $user = $this->createsUserAccount->execute($object); UserInvitation::where('email', $invite->getInviteeEmail()) ->where('hash', $invite->getHash())->update(['is_complete' => true]); auth()->loginUsingId($user->id); return redirect()->route('dashboard'); } catch (CannotCreateUserException $exception) { throw new InternalServerErrorException("Failed to create user because {$exception->getMessage()}"); } } }