<?php
namespace Stimactiv\AdminBundle\Controller;
use App\Security\Authenticator\CollaborateurAuthenticator;
use Stimactiv\AdminBundle\Form\Handler\CollaborateurHandler;
use Stimactiv\AdminBundle\Form\Type\Security\PasswordChangeType;
use Stimactiv\AdminBundle\Form\Type\Security\ProfileType;
use Stimactiv\AdminBundle\Form\Type\Security\RequestType;
use Stimactiv\AdminBundle\Form\Type\Security\ResettingType;
use Stimactiv\AdminBundle\Model\CollaborateurManager;
use Stimactiv\ClientBundle\Entity\Collaborateur;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
/**
* @Route("/tableaudebord", name="backend_")
*/
class SecurityController extends AbstractController
{
/**
* @Route("/connexion", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('backend_home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('@StimactivAdmin/security/login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/infos", name="profile", methods={"GET","POST"})
*/
public function profile(CollaborateurHandler $formHandler): Response
{
/**
* @var Collaborateur $user
*/
$user = $this->getUser();
$form = $this->createForm(ProfileType::class, $user);
if ($formHandler->update($form, $user)) {
$this->addFlash('success', "Votre profil vient d'être mis à jour avec succès.");
return $this->redirectToRoute('backend_profile');
}
return $this->render('@StimactivAdmin/security/profile/index.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @Route("/infos/reset", name="profile_reset", methods={"GET","POST"})
*/
public function reset(CollaborateurHandler $formHandler): Response
{
/**
* @var Collaborateur $user
*/
$user = $this->getUser();
$form = $this->createForm(PasswordChangeType::class, $user);
if ($formHandler->resetPassword($form)) {
$this->addFlash('success', "Votre mot de passe vient d'être mis à jour avec succès.");
return $this->redirectToRoute('backend_profile_reset');
}
return $this->render('@StimactivAdmin/security/profile/reset.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @Route("/logout", name="logout")
*/
public function logout()
{
throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
}
/**
* @Route("/{slug}", name="request_reset_password", requirements={"slug"="oubli|premiere-connexion"})
*/
public function request(string $slug, CollaborateurHandler $formHandler, CollaborateurManager $collaborateurManager): Response
{
$options = [];
if ($slug === 'premiere-connexion') {
$options['block_prefix'] = 'register';
}
$form = $this->createForm(RequestType::class, null, $options);
if ($formHandler->requestResetPassword($form)) {
$duration = $collaborateurManager::$_resetLinkDuration < 60 ? $collaborateurManager::$_resetLinkDuration . ' minutes' : ($collaborateurManager::$_resetLinkDuration / 60) . 'h';
$this->addFlash('success', "Un mail va vous être envoyé <b>dans quelques instants</b> afin que vous puissiez " . ($slug === 'oubli' ? "renouveler" : "définir") . " votre mot de passe.<br>Le lien que vous recevrez sera valide pendant " . $duration . ".");
} else {
foreach ($form->getErrors() as $error) {
$this->addFlash('danger', $error->getMessage());
}
}
return $this->render('@StimactivAdmin/security/resetting/request.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/reset/{id}/{token}", name="resetting_reset_password", requirements={"id"="\d+"})
*/
public function resetting(Request $request, Collaborateur $user, string $token, CollaborateurHandler $formHandler, CollaborateurManager $contactManager, UserAuthenticatorInterface $userAuthenticator, CollaborateurAuthenticator $collaborateurAuthenticator): Response
{
// interdit l'accès à la page si:
// le token enregistré en base et le token présent dans l'url ne sont pas égaux
// le token date de plus de 15 minutes
if ($token !== $user->getToken() || !$contactManager->isRequestInTime($user)) {
$this->addFlash('warning', 'Le lien n\'existe plus.');
throw new AccessDeniedException();
}
$form = $this->createForm(ResettingType::class, $user);
if ($formHandler->resetPassword($form)) {
$this->addFlash('success', "Votre mot de passe a été renouvelé.");
$userAuthenticator->authenticateUser(
$user,
$collaborateurAuthenticator,
$request
);
return $this->redirectToRoute('backend_home');
}
return $this->render('@StimactivAdmin/security/resetting/reset.html.twig', [
'form' => $form->createView()
]);
}
}