src/Controller/SecurityController.php line 24

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Office;
  4. use App\Entity\Users;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\PasswordHasher\Hasher\MessageDigestPasswordHasher;
  12. use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Psr\Log\LoggerInterface;
  17. class SecurityController extends AbstractController
  18. {
  19. private string $environment;
  20. private string $sessionId = '0';
  21. public function __construct(private ManagerRegistry $doctrine, private LoggerInterface $logger) {}
  22. #[\Symfony\Component\Routing\Attribute\Route('/login', name: 'login')]
  23. public function login(KernelInterface $kernel, Request $request, AuthenticationUtils $authenticationUtils, PasswordHasherFactoryInterface $hasherFactory): Response
  24. {
  25. $this->environment = $kernel->getEnvironment();
  26. $doctrine = $this->doctrine;
  27. $logger = $this->logger;
  28. $session = $request->getSession();
  29. $this->sessionId = $session->getId();
  30. $this->logOnDev('loginAction(' . ($request->isMethod('GET') ? 'GET' : ($request->isMethod('POST') ? 'POST' : 'OTHER')) .')');
  31. $user = new Users();
  32. // $passwordHasher = $hasherFactory->getPasswordHasher($user);
  33. // $logger->info('PasswordHasher='.get_class($passwordHasher).', '.$passwordHasher->encodeHashAsBase64);
  34. // $encoder = new MessageDigestPasswordHasher('sha1', false, 1);
  35. $plainPassword = 'xieyF@jzCH@9';
  36. // $encoded = $encoder->hash($plainPassword, '956540bd03d6da0546943322f348e405');
  37. // $user->setPassword($encoded);
  38. // $logger->info('loginAction: encoded: '.$encoded);
  39. // get the login error if there is one
  40. $error = $authenticationUtils->getLastAuthenticationError();
  41. // last username entered by the user
  42. $lastUsername = $authenticationUtils->getLastUsername();
  43. $uri = $request->server->get('HTTP_HOST');
  44. $officeArray = $doctrine->getRepository(Office::class)->findByofficedomain($uri);
  45. $office = array_pop($officeArray);
  46. $officeLogo = 'london-banner.jpg';
  47. if ($office !== null)
  48. $officeLogo = $office->getSitelogo();
  49. //return new Response(var_dump($office));
  50. //die;
  51. return $this->render(
  52. 'security/login.html.twig',
  53. [
  54. 'controller_name' => 'SecurityController',
  55. // last username entered by the user
  56. 'last_username' => $lastUsername,
  57. 'error' => $error,
  58. 'uri' => $uri,
  59. 'office' => $office,
  60. 'officeSiteLogo'=> $officeLogo,
  61. ]
  62. );
  63. }
  64. #[\Symfony\Component\Routing\Attribute\Route('/logout', name: 'logout')]
  65. public function logout(Request $request): Response
  66. {
  67. $this->sessionId = $session->getId();
  68. $this->logOnDev($sessionId.': Logout');
  69. $session->remove('user_authenticated');
  70. }
  71. private function logOnDev($msg) {
  72. if ($this->environment === 'dev') {
  73. $this->logger->info('SecurityController:'.$this->sessionId.': '.$msg);
  74. }
  75. }
  76. }