src/Controller/Security/SecurityController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Entity\User;
  4. use App\Entity\RhSalary;
  5. use App\Entity\Establishment;
  6. use App\Entity\EstablishmentGroup;
  7. use App\Repository\UserRepository;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. class SecurityController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/login", name="app_login")
  18.      */
  19.     public function login(AuthenticationUtils $authenticationUtilsUserRepository $userRepositoryUserPasswordHasherInterface $userPasswordHasherInterface): Response
  20.     {
  21.         if (count($userRepository->findBy([],[])) <= 0) {
  22.             $entityManager $this->getDoctrine()->getManager();
  23.             /* Creation d'un groupe d'etablissement */
  24.             $etsGroup = new EstablishmentGroup();
  25.             $etsGroup->setCode("EG-001");
  26.             $etsGroup->setName("Main");
  27.             $entityManager->persist($etsGroup);
  28.             /* Creation d'un etablissement */
  29.             $ets = new Establishment();
  30.             $ets->setCode("ETS-001");
  31.             $ets->setName("Main ETS");
  32.             $ets->setType(Establishment::ESTABLISHMENT_PRESCOLAIRE_PRIMAIRE_TYPES);
  33.             $ets->setEstablishmentGroup($etsGroup);
  34.             $entityManager->persist($ets);
  35.             /* Creation d'un super admin */
  36.             $superAdmin = new User();
  37.             $superAdmin->setEstablishmentGroup($etsGroup);
  38.             $superAdmin->setIsEnabled(true);
  39.             $superAdmin->setIsPasswordChanged(false);
  40.             $superAdmin->setEstablishment($ets);
  41.             $superAdmin->setUsername('super-admin');
  42.             $superAdmin->setRoles(['ROLE_SUPER_ADMIN']);
  43.             $superAdmin->setPassword(
  44.                 $userPasswordHasherInterface->hashPassword(
  45.                         $superAdmin,
  46.                         'central-edu'
  47.                     )
  48.                 );
  49.             $entityManager->persist($superAdmin);
  50.             /* Creation d'un salarier */
  51.             $salary = new RhSalary();
  52.             $salary->setCode("S-000");
  53.             $salary->setLastName("Admin");
  54.             $salary->setFirstName("Super");
  55.             $salary->setUser($superAdmin);
  56.             $salary->setEstablishment($ets);
  57.             $entityManager->persist($salary);
  58.             try {
  59.                 $entityManager->flush();
  60.                 $this->addFlash('info'"Bienvenue sur CENTRAL-EDU");
  61.             } catch (\Throwable $th) {
  62.                 $this->addFlash('warning'$th->getMessage());
  63.             }
  64.         }
  65.         if ($this->getUser()) {
  66.             return $this->redirectToRoute('default');
  67.         }
  68.         // get the login error if there is one
  69.         $error $authenticationUtils->getLastAuthenticationError();
  70.         // last username entered by the user
  71.         $lastUsername $authenticationUtils->getLastUsername();
  72.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  73.     }
  74.     /**
  75.      * @Route("/logout", name="app_logout")
  76.      */
  77.     public function logout(): void
  78.     {
  79.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  80.     }
  81.     /**
  82.      * @Route("/changePassword/{id}", name="change_password", methods={"GET"})
  83.     */
  84.     public function changePassword(Request $requestUser $userUserPasswordHasherInterface $userPasswordHasherInterface): Response
  85.     {
  86.         
  87.         $oldpassword $request->get('oldPassword');
  88.         $newpassword $request->get('newPassword');
  89.         $confirmpassword $request->get('confirmPassword');
  90.         if ($userPasswordHasherInterface->isPasswordValid($user$oldpassword) == false) {
  91.             return $this->json(['code' => 500'message' => "l'ancien mot de passe est incorrect"], 200);
  92.         }
  93.         if ($newpassword != $confirmpassword) {
  94.             return $this->json(['code' => 500'message' => "les mots de passe doivent être identique"], 200);
  95.         }
  96.         
  97.         if (strlen($newpassword) < 6) {
  98.             return $this->json(['code' => 500'message' => "Le mot de passe doit faire au moins 6 caractères"], 200);
  99.         }
  100.         
  101.         $entityManager $this->getDoctrine()->getManager();
  102.         $user->setIsPasswordChanged(true);
  103.         $user->setPassword(
  104.             $userPasswordHasherInterface->hashPassword(
  105.                     $user,
  106.                     $newpassword
  107.                 )
  108.             );
  109.         try {
  110.             $entityManager->flush();
  111.             $this->addFlash('success'"Mot de passe modifié :)");
  112.             return $this->json(['code' => 200'message' => "Mot de passe modifié :)"], 200);
  113.         } catch (\Throwable $th) {
  114.             //$th->getMessage()
  115.             $this->addFlash('info'$th->getMessage());
  116.         }
  117.         
  118.         $this->addFlash('warning'"Une erreure s'est produite");
  119.         return $this->json(['code' => 500'message' => "Une erreure s'est produite"], 200);
  120.     }
  121.     /**
  122.      * @Route("/profil", name="app_user_profil", methods={"GET"})
  123.      */
  124.     public function profil(): Response
  125.     {
  126.         $user $this->getUser();
  127.         return $this->render('security/profil.html.twig', [
  128.             'user' => $user,
  129.         ]);
  130.     }
  131. }