<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
//use Symfony\Component\Security\Core\Security; ?????
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class ProfileController extends AbstractController
{
#[Route('/profile', name: 'app_profile')]
public function index(): Response
{
if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
return $this->render('profile/index.html.twig', [
'user' => $this->getUser(),
]);
}
/**
* @Route("/profile/edit-profile", name="app_edit_profile")
*/
public function editProfile(Request $request, EntityManagerInterface $entityManager)
{
if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
//?????if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
$user=$this->getUser();
$form=$this->createForm(RegistrationFormType::class,$user);
$form->remove('plainPassword');
//$form->remove('rolesList');
$form->remove('isActive');
// $form->remove('employee_bareme');
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success','Your profile has been modified with success');
return $this->redirectToRoute('app_profile');
}
return $this->render('profile/edit_profile.html.twig', [
'form' => $form->createView(),
'user' => $user,
]);
}
/**
* @Route("/profile/change-password", name="app_change_password")
*/
public function changePassword(Request $request, EntityManagerInterface $entityManager,
UserPasswordHasherInterface $userPasswordHasher)
{
//check connected
if (!$this->getUser()){
return $this->redirectToRoute('app_login');
}
$user=$this->getUser();
$form = $this->createFormBuilder()
->add('oldPassword', PasswordType::class,['label'=>'Old password'])
->add('newPassword', PasswordType::class,['label'=>'New password'])
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$oldPassword = $form["oldPassword"]->getData();
$newPassword = $form["newPassword"]->getData();
$newPasswordEncrypted=$userPasswordHasher->hashPassword($user,$newPassword);
if (!$userPasswordHasher->isPasswordValid($user,$oldPassword))
{
$this->addFlash('error','Wrong password');
return $this->redirectToRoute('app_login',[]);
} else {
$user->setPassword($newPasswordEncrypted);
$entityManager->flush();
$this->addFlash('success','Your password has been modified with success');
return $this->redirectToRoute('app_profile');
}
}
//dd($newPasswordEncrypted);
return $this->render('profile/change_password.html.twig', [
'form' => $form->createView()
]);
}
}