<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Mailer\MailerInterface;
use App\Model\Globals;
use App\Entity\DicGouvernorat;
use App\Entity\DicDelegation;
use App\Entity\DicImada;
use App\Entity\DicCircuit;
use App\Entity\Eleveur;
use App\Entity\Inseminateur;
use App\Entity\AgentOep;
use App\Entity\Agent;
use App\Entity\User;
use App\Entity\Partenaire;
use App\Form\EleveurType;
use App\Form\InseminateurType;
use App\Form\AgentOepType;
use App\Form\AgentType;
use App\Form\UserType;
use Psr\Log\LoggerInterface;
/**
* @Route("/{_locale}", requirements={"_locale": "ar|fr"})
*/
class ProfilController extends AbstractController
{
private $httpClient;
public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
private function isPasswordStrong($password): bool
{
$pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/';
return preg_match($pattern, $password);
}
#[Route('/eleveur/register', name: 'eleveur_register', methods: ['GET', 'POST'])]
public function registerEleveur(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, LoggerInterface $logger): Response
{
$logger->info('eleveur register ');
$em = $doctrine->getManager();
$user = new User();
$eleveur = new Eleveur();
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$delegations = $doctrine->getRepository(DicDelegation::class)->findAll();
$imadas = $doctrine->getRepository(DicImada::class)->findAll();
$form = $this->createForm(EleveurType::class, $eleveur);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted() && $form->isValid()) {
$logger->info('eleveur register post valid ');
if ($request->request->has('g-recaptcha-response')) {
// dump($request);die;
/**** verification recaptcha avec siteverify ****************/
$recapResp = $request->get('g-recaptcha-response');
$logger->info('eleveur register post '.$recapResp);
if ($recapResp && $recapResp != '') {
$recapResp = $request->get('g-recaptcha-response');
$recaptchaSecret = '6LdCul0hAAAAALOxxC6Dz8Uqyh77-Q8zw4NhSAf7';
$verificationResponse = $this->httpClient->request(
'POST',
'https://www.google.com/recaptcha/api/siteverify',
[
'body' => [
'secret' => $recaptchaSecret,
'response' => $recapResp,
],
]
);
$verificationData = $verificationResponse->toArray();
//dump($recapResp, $verificationData);die;
$logger->info('eleveur secret post '.$recaptchaSecret);
$logger->info('recap response '.$recapResp);
$logger->info('eleveur register repsonse post ' . json_encode($verificationData));
/**** end verification avec google recaptcha verify */
/*if (isset($verificationData['success']) && $verificationData['success'] === true) {*/
$isExist = $doctrine->getRepository(Eleveur::class)->findOneBy(['codElv' => $request->request->get('username')]);
if ($isExist != null) {
try {
//$isExistUser= $doctrine->getRepository(User::class)->isExist($request->request->get('email'),$request->request->get('username'));
$isExistUser = $doctrine->getRepository(User::class)->isExist($request->request->get('username'));
if (count($isExistUser) == 0) {
$password = $request->request->get('password');
// Server-side password validation
if (!$this->isPasswordStrong($password)) {
// $this->addFlash('pass_error', 'Le mot de passe doit contenir au moins 8 caractères, une majuscule, une minuscule et un chiffre.');
//dump($password);die;
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('eleveur_register');
}
$user = new User();
$user->setEmail($request->request->get('email'));
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('password')
)
);
$user->setRoles(["ROLE_ELEVEUR"]);
$user->setUsername($request->request->get('username'));
$user->setEtat(1);
$user->setCreatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$isExist->setUser($user);
$isExist->setImada($eleveur->getImada());
$isExist->setDelegation($isExist->getDelegation());
$isExist->setGouvernorat($isExist->getGouvernorat());
$isExist->setNprElvFr($eleveur->getNprElvFr());
$isExist->setNprElvAr($eleveur->getNprElvAr());
$isExist->setAdresse($eleveur->getAdresse());
$isExist->setNumTel($eleveur->getNumTel());
$isExist->setNumFax($eleveur->getNumFax());
$isExist->setTypeElv($eleveur->getTypeElv());
$isExist->setIdentElv($eleveur->getIdentElv());
$isExist->setUpdatedAt(new \DateTime());
$em->persist($isExist);
$em->flush();
$em->flush();
$this->addFlash('success', 'Votre inscription a été effectuée avec succès !');
return $this->redirectToRoute('login');
} else {
$this->addFlash(
'failure_register_user_exist',
'true'
);
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
return $this->render('profil/register_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'eleveur' => $eleveur,
'user' => $user,
'form' => $form->createView(),
]);
}
} catch (Exception $e) {
return $this->redirectToRoute('eleveur_register');
}
} else {
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register_no_eleveur',
'true'
);
return $this->render('profil/register_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'eleveur' => $eleveur,
'user' => $user,
'form' => $form->createView(),
]);
} //end is exist user
/*}*/
}
//captcha is null
}
$this->addFlash(
'failure_register',
'true'
);
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
return $this->render('profil/register_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'eleveur' => $eleveur,
'user' => $user,
'form' => $form->createView(),
]);
} //end submit form
return $this->render('profil/register_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'eleveur' => $eleveur,
'user' => $user,
'form' => $form->createView(),
]);
}
#[Route('/eleveur/rest_password', name: 'eleveur_reset_password', methods: ['GET', 'POST'])]
public function resetPasswordEleveur(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, MailerInterface $mailer): Response
{
$em = $doctrine->getManager();
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$userExist = $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
if ($userExist !== null) {
try {
$password = (string)(random_int(100000, 999999));
$userExist->setPassword(
$passwordHasher->hashPassword(
$userExist,
$password
)
);
$em->persist($userExist);
$em->flush();
$to = $user->getEmail();
//$to='ryahyaoui@progress.tn';
$sub = '[OEP] Votre mot de passe';
$msg = '<p>Bonjour ' . $user->getUsername() . ', <br/> Votre mot de passe est ' . $password . '.</p>';
$globals = new Globals();
$globals->sendEmail($mailer, $to, $sub, $msg);
return $this->redirectToRoute('login');
} catch (Exception $e) {
return $this->redirectToRoute('eleveur_reset_password');
}
} else {
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_register',
'true'
);
return $this->render('security/reset_password_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end user exist
}
}
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_recaptcha',
'true'
);
return $this->render('security/reset_password_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('security/reset_password_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_ELEVEUR")
*/
#[Route('/eleveur/profil', name: 'eleveur_profil', methods: ['GET', 'POST'])]
public function profilEleveur(Request $request, ManagerRegistry $doctrine): Response
{
if ($this->isGranted('ROLE_ELEVEUR') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
$eleveur = $doctrine->getRepository(Eleveur::class)->findOneBy(['user' => $user]);
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$delegations = null;
$imadas = null;
if ($eleveur->getGouvernorat() != null) {
$delegations = $doctrine->getRepository(DicDelegation::class)->findBy(['gouvernorat' => $eleveur->getGouvernorat()]);
}
if ($eleveur->getDelegation() != null) {
$imadas = $doctrine->getRepository(DicImada::class)->findBy(['delegation' => $eleveur->getDelegation()]);
}
$form = $this->createForm(EleveurType::class, $eleveur);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
$eleveur->setUpdatedAt(new \DateTime());
$em->persist($eleveur);
$em->flush();
return $this->redirectToRoute('eleveur_profil');
}
return $this->render('profil/profil_eleveur.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'eleveur' => $eleveur,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_ELEVEUR")
*/
#[Route('/eleveur/password/edit', name: 'eleveur_edit_password', methods: ['GET', 'POST'])]
public function passwordEleveur(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_ELEVEUR') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('eleveur_profil');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('eleveur_profil');
}
/*====================================== Inseminateur ========================================*/
#[Route('/inseminateur/register', name: 'inseminateur_register', methods: ['GET', 'POST'])]
public function registerInseminateur(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
$em = $doctrine->getManager();
$user = new User();
$inseminateur = new Inseminateur();
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$qb = $em->createQueryBuilder();
$query = $qb->select('c')
->from('App:DicCircuit', 'c')
->orderBy('c.desCirLt', 'ASC');
$circuits = $query->getQuery()->getResult();
$form = $this->createForm(InseminateurType::class, $inseminateur);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted() && $form->isValid()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$isExist = $doctrine->getRepository(inseminateur::class)->findOneBy(['codIns' => $request->request->get('username')]);
if ($isExist != null) {
try {
$isExistUser = $doctrine->getRepository(User::class)->isExist($request->request->get('email'), $request->request->get('username'));
$circuitId = $request->request->get('circuitt');
$dicCircuit = $doctrine->getRepository(DicCircuit::class)->find($circuitId);
if (count($isExistUser) == 0) {
$password = $request->request->get('password');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('inseminateur_register');
}
$user = new User();
$user->setEmail($request->request->get('email'));
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('password')
)
);
$user->setRoles(["ROLE_INSEMINATEUR"]);
$user->setUsername($request->request->get('username'));
$user->setEtat(1);
$user->setCreatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$isExist->setUser($user);
$isExist->setDicCircuit($isExist->getDicCircuit());
$isExist->setDicGouvernorat($isExist->getDicGouvernorat());
$isExist->setNprInsFr($inseminateur->getNprInsFr());
$isExist->setNprInsAr($inseminateur->getNprInsAr());
$isExist->setAdresse($inseminateur->getAdresse());
$isExist->setNumTel($inseminateur->getNumTel());
$isExist->setNumFax($isExist->getNumFax());
$isExist->setTypeIns($isExist->getTypeIns());
$isExist->setIdentIns($isExist->getIdentIns());
$isExist->setUpdatedAt(new \DateTime());
$em->persist($isExist);
$em->flush();
$this->addFlash('success', 'Votre inscription a été effectuée avec succès !');
return $this->redirectToRoute('login');
} else {
$this->addFlash(
'failure_register_user_exist',
'true'
);
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
return $this->render('profil/register_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'inseminateur' => $inseminateur,
'circuit' => $circuits,
'user' => $user,
'form' => $form->createView(),
]);
}
} catch (Exception $e) {
return $this->redirectToRoute('inseminateur_register');
}
} else {
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register_no_ins',
'true'
);
return $this->render('profil/register_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'inseminateur' => $inseminateur,
'circuit' => $circuits,
'user' => $user,
'form' => $form->createView(),
]);
} //end user exist
} //has captcha
} //captcha is null
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register',
'true'
);
return $this->render('profil/register_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'inseminateur' => $inseminateur,
'user' => $user,
'circuit' => $circuits,
'form' => $form->createView(),
]);
} //end submit
$qb = $em->createQueryBuilder();
$query = $qb->select('c')
->from('App:DicCircuit', 'c')
->orderBy('c.desCirLt', 'ASC');
$circuits = $query->getQuery()->getResult();
//dump($circuits);die;
return $this->render('profil/register_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'inseminateur' => $inseminateur,
'user' => $user,
'circuit' => $circuits,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_INSEMINATEUR")
*/
#[Route('/inseminateur/rest_password', name: 'inseminateur_reset_password', methods: ['GET', 'POST'])]
public function resetPasswordInseminateur(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, MailerInterface $mailer): Response
{
$em = $doctrine->getManager();
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$userExist = $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
if ($userExist !== null) {
try {
$password = (string)(random_int(100000, 999999));
$userExist->setPassword(
$passwordHasher->hashPassword(
$userExist,
$password
)
);
$em->persist($userExist);
$em->flush();
$to = $user->getEmail();
$sub = '[OEP] Votre mot de passe';
$msg = '<p>Bonjour ' . $user->getUsername() . ', <br/> Votre mot de passe est ' . $password . '.</p>';
$globals = new Globals();
$globals->sendEmail($mailer, $to, $sub, $msg);
return $this->redirectToRoute('login');
} catch (Exception $e) {
return $this->redirectToRoute('inseminateur_reset_password');
}
} else {
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_register',
'true'
);
return $this->render('security/reset_password_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end exist user
}
}
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_recaptcha',
'true'
);
return $this->render('security/reset_password_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('security/reset_password_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_INSEMINATEUR")
*/
#[Route('/inseminateur/profil', name: 'inseminateur_profil', methods: ['GET', 'POST'])]
public function profilInseminateur(Request $request, ManagerRegistry $doctrine): Response
{
if ($this->isGranted('ROLE_INSEMINATEUR') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$locale = $request->getLocale();
$em = $doctrine->getManager();
$user = $this->getUser();
$inseminateur = $doctrine->getRepository(Inseminateur::class)->findOneBy(['user' => $user]);
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$form = $this->createForm(InseminateurType::class, $inseminateur, ['locale' => $locale]);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
$inseminateur->setUpdatedAt(new \DateTime());
$em->persist($inseminateur);
$em->flush();
return $this->redirectToRoute('inseminateur_profil');
}
return $this->render('profil/profil_inseminateur.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'inseminateur' => $inseminateur,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_INSEMINATEUR")
*/
#[Route('/inseminateur/password/edit', name: 'inseminateur_edit_password', methods: ['GET', 'POST'])]
public function passwordInseminateur(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_INSEMINATEUR') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('inseminateur_profil');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('inseminateur_profil');
}
/*====================================== Agent Oep ========================================*/
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/agentOEP/register', name: 'agent_oep_register', methods: ['GET', 'POST'])]
public function registerAgentOep(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
$em = $doctrine->getManager();
$user = new User();
$agent = new AgentOep();
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$delegations = $doctrine->getRepository(DicDelegation::class)->findAll();
$imadas = $doctrine->getRepository(DicImada::class)->findAll();
$form = $this->createForm(AgentOepType::class, $agent);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$isExist = $doctrine->getRepository(AgentOep::class)->findOneBy(['codAgent' => $request->request->get('username')]);
if ($isExist != null) {
try {
$isExistUser = $doctrine->getRepository(User::class)->isExist($request->request->get('email'), $request->request->get('username'));
if (count($isExistUser) == 0) {
$user = new User();
$user->setEmail($request->request->get('email'));
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('password')
)
);
$user->setRoles(["ROLE_AGENT_OEP"]);
$user->setUsername($request->request->get('username'));
$user->setEtat(1);
$user->setCreatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$isExist->setUser($user);
$isExist->setImada($agent->getImada());
$isExist->setDelegation($agent->getDelegation());
$isExist->setGouvernorat($agent->getGouvernorat());
$isExist->setNprAgentFr($agent->getNprAgentFr());
$isExist->setNprAgentAr($agent->getNprAgentAr());
$isExist->setAdresse($agent->getAdresse());
$isExist->setNumTel($agent->getNumTel());
$isExist->setNumFax($agent->getNumFax());
$isExist->getTypeAgent($agent->getTypeAgent());
$isExist->setIdentAgent($agent->getIdentAgent());
$isExist->setUpdatedAt(new \DateTime());
$em->persist($isExist);
$em->flush();
return $this->redirectToRoute('login');
} else {
$this->addFlash(
'failure_register_user_exist',
'true'
);
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
return $this->render('profil/register_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
}
} catch (Exception $e) {
return $this->redirectToRoute('agent_oep_register');
}
} else {
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register_no_agent',
'true'
);
return $this->render('profil/register_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
} //end exist
} //has captcha
} //captcha is null
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register',
'true'
);
return $this->render('profil/register_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('profil/register_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
}
#[Route('/agentOEP/rest_password', name: 'agent_oep_reset_password', methods: ['GET', 'POST'])]
public function resetPasswordAgentOEP(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, MailerInterface $mailer): Response
{
$em = $doctrine->getManager();
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$userExist = $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
if ($userExist !== null) {
try {
$password = (string)(random_int(100000, 999999));
$userExist->setPassword(
$passwordHasher->hashPassword(
$userExist,
$password
)
);
$em->persist($userExist);
$em->flush();
$to = $user->getEmail();
$sub = '[OEP] Votre mot de passe';
$msg = '<p>Bonjour ' . $user->getUsername() . ', <br/> Votre mot de passe est ' . $password . '.</p>';
$globals = new Globals();
$globals->sendEmail($mailer, $to, $sub, $msg);
return $this->redirectToRoute('login');
} catch (Exception $e) {
return $this->redirectToRoute('agent_oep_reset_password');
}
} else {
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_register',
'true'
);
return $this->render('security/reset_password_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end exist user
}
}
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_recaptcha',
'true'
);
return $this->render('security/reset_password_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('security/reset_password_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_AGENT_OEP")
*/
#[Route('/agentOEP/profil', name: 'agent_oep_profil', methods: ['GET', 'POST'])]
public function profilAgentOep(Request $request, ManagerRegistry $doctrine): Response
{
if ($this->isGranted('ROLE_AGENT_OEP') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
$agent = $doctrine->getRepository(AgentOep::class)->findOneBy(['user' => $user]);
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$delegations = $doctrine->getRepository(DicDelegation::class)->findBy(['gouvernorat' => $agent->getGouvernorat()]);
$imadas = $doctrine->getRepository(DicImada::class)->findBy(['delegation' => $agent->getDelegation()]);
$form = $this->createForm(AgentOepType::class, $agent);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
$agent->setUpdatedAt(new \DateTime());
$em->persist($agent);
$em->flush();
return $this->redirectToRoute('agent_oep_profil');
}
return $this->render('profil/profil_agent_oep.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_AGENT_OEP")
*/
#[Route('/agentOEP/password/edit', name: 'agent_oep_edit_password', methods: ['GET', 'POST'])]
public function passwordAgentOep(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_AGENT_OEP') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('agent_oep_profil');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('agent_oep_profil');
}
/*====================================== Agent ========================================*/
#[Route('/agent/register', name: 'agent_register', methods: ['GET', 'POST'])]
public function registerAgent(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
$em = $doctrine->getManager();
$user = new User();
$agent = new Agent();
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$delegations = $doctrine->getRepository(DicDelegation::class)->findAll();
$imadas = $doctrine->getRepository(DicImada::class)->findAll();
$form = $this->createForm(AgentType::class, $agent);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted() && $form->isValid()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$isExist = $doctrine->getRepository(Agent::class)->findOneBy(['codAgent' => $request->request->get('username')]);
if ($isExist != null) {
try {
$isExistUser = $doctrine->getRepository(User::class)->isExist($request->request->get('email'), $request->request->get('username'));
if (count($isExistUser) == 0) {
$password = $request->request->get('password');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('inseminateur_register');
}
$user = new User();
$user->setEmail($request->request->get('email'));
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('password')
)
);
$user->setRoles(["ROLE_AGENT"]);
$user->setUsername($request->request->get('username'));
$user->setEtat(1);
$user->setCreatedAt(new \DateTime());
//dump($agent,$user);die;
$em->persist($user);
$em->flush();
$isExist->setUser($user);
$isExist->setImada($agent->getImada());
$isExist->setDelegation($agent->getDelegation());
$isExist->setGouvernorat($agent->getGouvernorat());
$isExist->setNprAgentFr($agent->getNprAgentFr());
$isExist->setNprAgentAr($agent->getNprAgentAr());
$isExist->setAdresse($agent->getAdresse());
$isExist->setNumTel($agent->getNumTel());
$isExist->setNumFax($agent->getNumFax());
$isExist->getTypeAgent($agent->getTypeAgent());
$isExist->setIdentAgent($agent->getIdentAgent());
$isExist->setUpdatedAt(new \DateTime());
$em->persist($isExist);
$em->flush();
$this->addFlash('success', 'Votre inscription a été effectuée avec succès !');
return $this->redirectToRoute('login');
} else {
$this->addFlash(
'failure_register_user_exist',
'true'
);
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
return $this->render('profil/register_agent.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
}
} catch (Exception $e) {
return $this->redirectToRoute('agent_register');
}
} else {
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register_no_agent',
'true'
);
return $this->render('profil/register_agent.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
} //end exist
} //has captcha
} //captcha is null
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$this->addFlash(
'failure_register',
'true'
);
return $this->render('profil/register_agent.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('profil/register_agent.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'user' => $user,
'form' => $form->createView(),
]);
}
#[Route('/agent/rest_password', name: 'agent_reset_password', methods: ['GET', 'POST'])]
public function resetPasswordAgent(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, MailerInterface $mailer): Response
{
$em = $doctrine->getManager();
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$userExist = $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
if ($userExist !== null) {
try {
$password = (string)(random_int(100000, 999999));
$userExist->setPassword(
$passwordHasher->hashPassword(
$userExist,
$password
)
);
$em->persist($userExist);
$em->flush();
$to = $user->getEmail();
$sub = '[OEP] Votre mot de passe';
$msg = '<p>Bonjour ' . $user->getUsername() . ', <br/> Votre mot de passe est ' . $password . '.</p>';
$globals = new Globals();
$globals->sendEmail($mailer, $to, $sub, $msg);
return $this->redirectToRoute('login');
} catch (Exception $e) {
return $this->redirectToRoute('agent_reset_password');
}
} else {
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_register',
'true'
);
return $this->render('security/reset_password_agent.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end exist user
}
}
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_recaptcha',
'true'
);
return $this->render('security/reset_password_agent.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('security/reset_password_agent.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_AGENT")
*/
#[Route('/agent/profil', name: 'agent_profil', methods: ['GET', 'POST'])]
public function profilAgent(Request $request, ManagerRegistry $doctrine): Response
{
if ($this->isGranted('ROLE_AGENT') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
$agent = $doctrine->getRepository(Agent::class)->findOneBy(['user' => $user]);
$gouvernorats = $doctrine->getRepository(DicGouvernorat::class)->findAll();
$delegations = $doctrine->getRepository(DicDelegation::class)->findBy(['gouvernorat' => $agent->getGouvernorat()]);
$imadas = $doctrine->getRepository(DicImada::class)->findBy(['delegation' => $agent->getDelegation()]);
$form = $this->createForm(AgentType::class, $agent);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
$agent->setUpdatedAt(new \DateTime());
$em->persist($agent);
$em->flush();
return $this->redirectToRoute('agent_profil');
}
return $this->render('profil/profil_agent.html.twig', [
'controller_name' => 'ProfilController',
'gouvernorats' => $gouvernorats,
'delegations' => $delegations,
'imadas' => $imadas,
'agent' => $agent,
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_AGENT")
*/
#[Route('/agent/password/edit', name: 'agent_edit_password', methods: ['GET', 'POST'])]
public function passwordAgent(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_AGENT') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('agent_profil');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('agent_profil');
}
/*====================================== Admin ========================================*/
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/admin/password/edit', name: 'admin_edit_password', methods: ['GET', 'POST'])]
public function passwordAdmin(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_ADMIN') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('admin_user_admin_add');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('admin_dashboard');
}
/**************************consultation********************************** */
/**
* @IsGranted("ROLE_CONSULTATION")
*/
#[Route('/consultation/password/edit', name: 'consultation_edit_password', methods: ['GET', 'POST'])]
public function passwordConsultation(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_CONSULTATION') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('app_consultation');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('app_consultation');
}
/**************************partenaire********************************* */
/**
* @IsGranted("ROLE_PARTENAIRE")
*/
#[Route('/partenaire/password/edit', name: 'partenaire_edit_password', methods: ['GET', 'POST'])]
public function passwordPartenaire(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
if ($this->isGranted('ROLE_PARTENAIRE') == false) {
return $this->render('erreur/access_denied.html.twig', []);
}
$em = $doctrine->getManager();
$user = $this->getUser();
if ($passwordHasher->isPasswordValid($user, $request->request->get('password'))) {
if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
$password = $request->request->get('newPassword');
if (!$this->isPasswordStrong($password)) {
$this->addFlash('pass_error', 'true');
return $this->redirectToRoute('app_partenaire');
}
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$request->request->get('newPassword')
)
);
$user->setUpdatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash(
'success_password_edit',
'true'
);
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
} else {
$this->addFlash(
'success_password_edit',
'false'
);
}
return $this->redirectToRoute('app_partenaire');
}
/***********************User************************************/
#[Route('/rest_password', name: 'reset_password', methods: ['GET', 'POST'])]
public function resetPasswordUser(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, MailerInterface $mailer): Response
{
$em = $doctrine->getManager();
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
//&& $form->isValid()
if ($form->isSubmitted()) {
if ($request->request->has('g-recaptcha-response')) {
if ($request->request->get('g-recaptcha-response') != '') {
$userExist = $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
if ($userExist !== null) {
try {
$password = (string)(random_int(100000, 999999));
$userExist->setPassword(
$passwordHasher->hashPassword(
$userExist,
$password
)
);
$em->persist($userExist);
$em->flush();
$to = $user->getEmail();
//$to='ryahyaoui@progress.tn';
$sub = '[OEP] Votre mot de passe';
$msg = '<p>Bonjour ' . $user->getUsername() . ', <br/> Votre mot de passe est ' . $password . '.</p>';
$globals = new Globals();
$globals->sendEmail($mailer, $to, $sub, $msg);
return $this->redirectToRoute('login');
} catch (\Exception $e) {
return $this->redirectToRoute('reset_password');
}
} else {
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_register',
'true'
);
return $this->render('security/reset_password_user.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end user exist
}
}
$user->setEmail($user->getEmail());
$this->addFlash(
'failure_recaptcha',
'true'
);
return $this->render('security/reset_password_user.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
} //end submit
return $this->render('security/reset_password_user.html.twig', [
'controller_name' => 'ProfilController',
'user' => $user,
'form' => $form->createView(),
]);
}
}