<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\DateTime;
use App\Utils\Constants;
use App\Utils\Breadcrumbs;
use App\Utils\Utils;
use App\Entity\User;
use App\Entity\BusinessArea;
use App\Entity\Organisation;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormError;
use App\Form\CompanyNameType;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use App\Security\LoginFormAuthenticator;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use App\Service\CryptoService;
class ForgotPasswordController extends AbstractController
{
/**
* @Route("/forgot-password", name="forgot-password")
*/
public function index(Request $request, UserPasswordEncoderInterface $encoder, LoginFormAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler, \Swift_Mailer $mailer, CryptoService $crypto)
{
$currentUser = new User();
$form = $this->createFormBuilder($currentUser)->getForm();
$form->add('email', EmailType::class, ['label' => '', 'data' => $currentUser->getEmail(), 'required' => true, 'attr' => array('class' => 'form-control') ]);
$form->add('save', SubmitType::class, ['label' => 'Reset Password', 'attr' => array('class' => 'btn btn-success btn-md') ]);
$em = $this->getDoctrine()->getManager();
$form->handleRequest($request);
if ($form->isSubmitted() and $form->isValid()) {
$userRepo = $this->getDoctrine()->getRepository(User::class);
$users = $userRepo->findBy(
['status' => Constants::ACTIVE],
['date_updated' => 'DESC']
);
$userFound = false;
$thisUser = "";
foreach($users as $user) {
if($user->getEmail() == $currentUser->getEmail()) {
$userFound = true;
$thisUser = $user;
break;
}
}
if($userFound == false) {
$this->addFlash('error','User with email '.$currentUser->getEmail().' does not exist or is not active in the system.');
} else {
if($thisUser != "") {
$tempPassword = sha1(random_bytes(10));
$encoded = $encoder->encodePassword($thisUser, $tempPassword);
$thisUser->setPassword($encoded);
$thisUser->setDateUpdated(\DateTime::createFromFormat('U', time()));
$em->persist($thisUser);
$em->flush();
$this->addFlash('notice','Temporary Password reset for User '.$crypto->getCrypto()->Decrypt($thisUser->getEmail()));
//Password reset email
$result = Utils::sendEmail($mailer, $this, Constants::EMAIL_SUBJECT_PASSWORD_RESET, $tempPassword, $crypto->getCrypto()->Decrypt($thisUser->getEmail()), Constants::EMAIL_FROM_SYSTEM, Constants::EMAIL_TEMPLATE_FORGOT_PASSWORD_RESET);
if($result == "1") {
$this->addFlash('notice', 'Password reset email sent');
} else {
$this->addFlash('error','Error sending password reset email: '.$result);
}
} else {
$this->addFlash('error','Error finding selected user, please contact info@medtechmor.ie');
}
return $this->redirectToRoute('forgot-password');
}
}
return $this->render('security/forgot-password.html.twig', array(
'form' => $form->createView(),
));
}
}