<?php
namespace App\Controller\Backoffice;
use App\Entity\User;
use App\Entity\Admin;
use App\Entity\SetPassword;
use App\Form\SetPasswordType;
use App\Entity\PasswordUpdate;
use App\Form\AdminProfileType;
use App\Form\PasswordUpdateType;
use App\Repository\UserRepository;
use Symfony\Component\Form\FormError;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class AdminAccountController extends AbstractController
{
private $translator;
private $request;
public function __construct(TranslatorInterface $translator, RequestStack $requestStack)
{
$this->translator = $translator;
$this->request = $requestStack->getCurrentRequest();
}
private function generateUniqueFileName()
{
return md5(uniqid());
}
#[Route(path: '/connexion', name: 'admin_login')]
public function login(AuthenticationUtils $utils)
{
$error = $utils->getLastAuthenticationError();
$username = $utils->getLastUsername();
return $this->render('backoffice/account/login.html.twig', [
'hasError' => $error,
'username' => $username
]);
}
#[Route(path: '/verification-compte', name: 'account_check')]
public function accountCheck(AuthenticationUtils $utils)
{
$admin = $this->getUser();
if ($admin->getHasAlreadyLoggedIn()) {
if (in_array("ROLE_EDITOR",$admin->getRoles()))
{
return $this->redirectToRoute('admin_blog_published_articles');
}
return $this->redirectToRoute('admin_dashboard');
}
else {
return $this->redirectToRoute('set_password');
}
}
#[Route(path: '/securite/configurer-mot-de-passe', name: 'set_password')]
public function setPassword(Request $request, UserPasswordHasherInterface $passwordHasher)
{
$admin = $this->getUser();
$password = new SetPassword();
$form = $this->createForm(SetPasswordType::class, $password);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newPass = $password->getNewPassword();
$hash = $passwordHasher->hashPassword($admin, $newPass);
$admin->setPassword($hash)
->setHasAlreadyLoggedIn(true)
->setActivated(true);
$manager = $this->getDoctrine()->getManager();
$manager->persist($admin);
$manager->flush();
$this->addFlash(
'success',
$this->translator->trans('backoffice.new_password_save')
);
if (in_array("ROLE_EDITOR",$admin->getRoles()))
{
return $this->redirectToRoute('admin_blog_published_articles');
}
return $this->redirectToRoute('admin_dashboard');
}
return $this->render('backoffice/user/set-password.html.twig', [
'form' => $form->createView()
]);
}
#[Route(path: '/deconnexion', name: 'admin_logout')]
public function logout(){}
#[Route(path: '/modifier-mon-profil', name: 'admin_edit_profile')]
public function adminProfile(Request $request, ObjectManager $manager, UserPasswordHasherInterface $passwordHasher)
{
$admin = $this->getUser();
$oldPhoto = $admin->getPhoto();
$passUpdate = new PasswordUpdate();
$formPassword = $this->createForm(PasswordUpdateType::class, $passUpdate);
$formProfile = $this->createForm(AdminProfileType::class, $admin);
$formPassword->handleRequest($request);
$formProfile->handleRequest($request);
if ($formPassword->isSubmitted() && $formPassword->isValid()) {
if (!password_verify($passUpdate->getOldPassword(), $admin->getPassword())) {
$formPassword->get('oldPassword')->addError(new FormError("Ce n'est pas votre de passe actuel !"));
}
else {
$newPass = $passUpdate->getNewPassword();
$hash = $passwordHasher->hashPassword($admin, $newPass);
$admin->setPassword($hash);
$manager->persist($admin);
$manager->flush();
$this->addFlash(
'success',
$this->translator->trans('backoffice.password_edit')
);
return $this->redirectToRoute('admin_profile');
}
}
if ($formProfile->isSubmitted() && $formProfile->isValid()) {
if ($admin->getPhoto() != null) {
$avatar = $admin->getPhoto();
$fileName = $this->generateUniqueFileName() . '.' . $avatar->guessExtension();
$avatar->move(
$this->getParameter('avatar_directory'),
$fileName
);
$admin->setPhoto($fileName);
if(file_exists($this->getParameter('avatar_directory') . '/' . $oldPhoto))
{
unlink($this->getParameter('avatar_directory') . '/' . $oldPhoto);
}
}
else {
$admin->setPhoto($oldPhoto);
}
$manager->persist($admin);
$manager->flush();
$this->addFlash(
'success',
$this->translator->trans('backoffice.information_modified')
);
return $this->redirectToRoute('admin_profile');
}
return $this->render('backoffice/account/edit_profile.html.twig', [
'formProfile' => $formProfile->createView(),
'formPassword' => $formPassword->createView(),
'oldPhoto' => $oldPhoto
]);
}
#[Route(path: '/mon-profil', name: 'admin_profile')]
public function seeProfile()
{
$admin = $this->getUser();
return $this->render('/backoffice/account/see_profile.html.twig', [
'admin' => $admin
]);
}
#[Route(path: '/change-locale', name: 'change_locale')]
public function changeLocale(Request $request)
{
$locale = $request->getLocale();
if ($locale == "fr") {
$this->get('session')->set('_locale', 'en');
} else {
$this->get('session')->set('_locale', 'fr');
}
return $this->redirectToRoute('admin_dashboard');
}
}