<?php
namespace App\Controller\Auth;
use App\Entity\Generic\User;
use App\Repository\Generic\UserRepository;
use App\Service\Util\NotificationService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use LogicException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/redirect', name: 'app_auth_handler')]
#[IsGranted('ROLE_USER')]
public function app_auth_handler(ManagerRegistry $managerRegistry, NotificationService $notificationService,EntityManagerInterface $entityManager, UserRepository $userRepository): Response
{
/**
* @var User $user
*/
$user = $this->getUser();
if ($user){
$user->setLastLogin(new \DateTime());
$this->getDoctrine()->getManager()->flush();
}
// if (!$user->isVerified()){
// return $this->redirectToRoute('app_auth_verify_account');
// }
try {
foreach ($userRepository->findAll() as $user) {
$array = [];
$targetUser = $user;
start:
if ($targetUser->getPresenter()) {
$array[] = $targetUser->getEmail();
$targetUser = $targetUser->getPresenter();
goto start;
} else {
$array[] = $targetUser->getEmail();
}
$user->setUserTrace(array_reverse($array));
}
$this->getDoctrine()->getManager()->flush();
} catch (Exception $exception) {
$notificationService->exception($exception);
$haveError = true;
}
if (!$this->getUser()) {
return $this->redirectToRoute('app_login');
}
/**
* @var User $user
*/
$user = $this->getUser();
// if (!$user->isActive()){
// $this->addFlash('error' , 'حساب شما مسدود شده است');
// return $this->redirectToRoute('app_logout');
// }
$user->setLastLogin(new DateTime());
$managerRegistry->getManager()->flush();
$port = $user->getUniquePresenterCode();
while ($port == null) {
try {
$tempPort = random_int(10000, 65000);
} catch (\Exception $exception) {
$notificationService->exception($exception);
}
if ($managerRegistry->getRepository(User::class)->findOneBy(['uniquePresenterCode' => $tempPort]) === null) {
$port = $tempPort;
}
}
$user->setUniquePresenterCode($port);
$managerRegistry->getManager()->flush();
if ($this->getUser()->hasRole('ROLE_ADMIN')) {
return $this->redirectToRoute('app_admin_dashboard');
}
// die('سیستم در حال بروزرسانی می باشد');
return $this->redirectToRoute('app_user_dashboard');
}
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils , Request $request): Response
{
if (str_contains($request->getHost(),'tehran')) {
$isStoreSite = true;
}else{
$isStoreSite = false;
}
if ($this->getUser()) {
return $this->redirectToRoute('app_auth_handler');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('auth/security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'isStoreSite' => $isStoreSite
]);
}
#[Route(path: '/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.');
}
}