PHP code example of eltharin / invitations

1. Go to this page and download the library: Download eltharin/invitations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eltharin / invitations example snippets


class ForgetPasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class, [
				'label' => 'Type your email'
            ])
        ;
    }
}

class ResetPasswordType extends AbstractType
{
	public function buildForm(FormBuilderInterface $builder, array $options): void
	{
		$builder
			->add('password', PasswordType::class, [
				'label' => 'Type the new password',
				'help_html' => true,
			])
		;
	}
}



class ResetPassword extends AbstractInvitation
{
	public function setMailContent(TemplatedEmail $email, InvitationEntityManager $invitationEntityManagerManager): void
	{
		$email->subject('Reset password')
			->htmlTemplate('mail/invitations/resetpassword.html.twig');
	}

	public function resolve(InvitationEntityManager $invitationEntityManagerManager): bool
	{
	    // nothing here all is treated in controller
		return true;
	}

	public function getResolvePath(InvitationEntityManager $invitationEntityManagerManager) : array
	{
		return [
			'path' => 'app_reset_password',
			'args' => ['id' => $invitationEntityManagerManager->getInvitation()->getId(), 'token' => $invitationEntityManagerManager->getInvitation()->getToken()],
		];
	}

	public function resendMailIfAlreadyExists(): bool
	{
		return true;
	}
}

	#[Route(path: '/forget-password', name: 'app_forget_password')]
	public function forgetPassword(
		Request $request,
		UserRepository $userRepository,
		InvitationManager $invitationManager
	): Response
	{
		$form = $this->createForm(ForgetPasswordType::class);
		$form->handleRequest($request);

		if($form->isSubmitted() && $form->isValid())
		{
			$user = $userRepository->findOneByEmail($form->get('email')->getData());

			if($user != null)
			{
				$invitationManager->create(ResetPassword::class, $user, $form->get('email')->getData(),0);
			}

			return $this->render('message.html.twig', [
				'message' => 'If you mail is on our base, we sent you a message for reset your password',
			]);
		}

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


	#[Route(path: '/reset-password', name: 'app_reset_password')]
	public function resetPassword(
		Request $request,
		InvitationManager $invitationManager,
		EntityManagerInterface $em,
		UserPasswordHasherInterface $passwordHasher
	): Response
	{
		$invitation = $invitationManager->getInvit($request->query->get('id'), ['type' => [ResetPassword::class], 'token' => $request->query->get('token')]);
		if($invitation == null)
		{
			throw new ItemNotFoundException('Unkwnon Invitation');
		}

		$user = $invitation->getInvitation()->getUser();
		$form = $this->createForm(ResetPasswordType::class, $user, ['action' => $invitation->getResolvePath(true)]);
		$form->handleRequest($request);

		if($form->isSubmitted() && $form->isValid())
		{
			$user->setPassword($passwordHasher->hashPassword($user,$user->getPassword()));
			$em->flush();

			$invitation->resolve(true);

			$this->addFlash('success', 'Your password has been changed.');
			return $this->redirectToRoute('app_login');
		}

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