<?php
namespace App\Controller\Security;
use App\Entity\User;
use App\Entity\RhSalary;
use App\Entity\Establishment;
use App\Entity\EstablishmentGroup;
use App\Repository\UserRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, UserRepository $userRepository, UserPasswordHasherInterface $userPasswordHasherInterface): Response
{
if (count($userRepository->findBy([],[])) <= 0) {
$entityManager = $this->getDoctrine()->getManager();
/* Creation d'un groupe d'etablissement */
$etsGroup = new EstablishmentGroup();
$etsGroup->setCode("EG-001");
$etsGroup->setName("Main");
$entityManager->persist($etsGroup);
/* Creation d'un etablissement */
$ets = new Establishment();
$ets->setCode("ETS-001");
$ets->setName("Main ETS");
$ets->setType(Establishment::ESTABLISHMENT_PRESCOLAIRE_PRIMAIRE_TYPES);
$ets->setEstablishmentGroup($etsGroup);
$entityManager->persist($ets);
/* Creation d'un super admin */
$superAdmin = new User();
$superAdmin->setEstablishmentGroup($etsGroup);
$superAdmin->setIsEnabled(true);
$superAdmin->setIsPasswordChanged(false);
$superAdmin->setEstablishment($ets);
$superAdmin->setUsername('super-admin');
$superAdmin->setRoles(['ROLE_SUPER_ADMIN']);
$superAdmin->setPassword(
$userPasswordHasherInterface->hashPassword(
$superAdmin,
'central-edu'
)
);
$entityManager->persist($superAdmin);
/* Creation d'un salarier */
$salary = new RhSalary();
$salary->setCode("S-000");
$salary->setLastName("Admin");
$salary->setFirstName("Super");
$salary->setUser($superAdmin);
$salary->setEstablishment($ets);
$entityManager->persist($salary);
try {
$entityManager->flush();
$this->addFlash('info', "Bienvenue sur CENTRAL-EDU");
} catch (\Throwable $th) {
$this->addFlash('warning', $th->getMessage());
}
}
if ($this->getUser()) {
return $this->redirectToRoute('default');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/changePassword/{id}", name="change_password", methods={"GET"})
*/
public function changePassword(Request $request, User $user, UserPasswordHasherInterface $userPasswordHasherInterface): Response
{
$oldpassword = $request->get('oldPassword');
$newpassword = $request->get('newPassword');
$confirmpassword = $request->get('confirmPassword');
if ($userPasswordHasherInterface->isPasswordValid($user, $oldpassword) == false) {
return $this->json(['code' => 500, 'message' => "l'ancien mot de passe est incorrect"], 200);
}
if ($newpassword != $confirmpassword) {
return $this->json(['code' => 500, 'message' => "les mots de passe doivent être identique"], 200);
}
if (strlen($newpassword) < 6) {
return $this->json(['code' => 500, 'message' => "Le mot de passe doit faire au moins 6 caractères"], 200);
}
$entityManager = $this->getDoctrine()->getManager();
$user->setIsPasswordChanged(true);
$user->setPassword(
$userPasswordHasherInterface->hashPassword(
$user,
$newpassword
)
);
try {
$entityManager->flush();
$this->addFlash('success', "Mot de passe modifié :)");
return $this->json(['code' => 200, 'message' => "Mot de passe modifié :)"], 200);
} catch (\Throwable $th) {
//$th->getMessage()
$this->addFlash('info', $th->getMessage());
}
$this->addFlash('warning', "Une erreure s'est produite");
return $this->json(['code' => 500, 'message' => "Une erreure s'est produite"], 200);
}
/**
* @Route("/profil", name="app_user_profil", methods={"GET"})
*/
public function profil(): Response
{
$user = $this->getUser();
return $this->render('security/profil.html.twig', [
'user' => $user,
]);
}
}