src/Controller/Security/SecurityController.php line 96

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Controller\API\ApiTrait;
  4. use App\Form\CrmUser\LoginType;
  5. use App\Repository\ServiceFormRepository;
  6. use App\Repository\ServiceSectionGroupRepository;
  7. use App\Service\BarrierCache;
  8. use Drenso\OidcBundle\OidcClientInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. class SecurityController extends AbstractController
  19. {
  20.     use ApiTrait;
  21.     public function __construct(
  22.         private readonly ParameterBagInterface $parameterBag,
  23.     )
  24.     {
  25.     }
  26.     #[Route('/login_oidc'name'login_oidc')]
  27.     public function loginOIDC(OidcClientInterface $oidcClient): RedirectResponse
  28.     {
  29.         // Redirect to authorization @ OIDC provider
  30.         $redirect $oidcClient->generateAuthorizationRedirect(
  31.             null,
  32.             ['openid''account''email''profile''phone''address'],
  33.             false,
  34.             [
  35.                 "callback_uri" => 'http://localhost/infonot-sso-callback'
  36.             ],
  37.         );
  38.         return $redirect;
  39.     }
  40.     #[Route('/login_oidc_exist'name'login_oidc_exist')]
  41.     public function loginOIDCfromExistProfile(
  42.         BarrierCache        $barrierCache,
  43.         OidcClientInterface $oidcClient,
  44.         UserInterface       $user): RedirectResponse
  45.     {
  46.         // Redirect to authorization @ OIDC provider
  47.         $redirect $oidcClient->generateAuthorizationRedirect(
  48.             null,
  49.             ['openid''account''email''profile''phone''address'],
  50.             false,
  51.             [
  52.                 "callback_uri" => 'http://localhost/infonot-sso-callback'
  53.             ],
  54.         );
  55.         $query parse_url($redirect->getTargetUrl(), PHP_URL_QUERY);
  56.         $params = [];
  57.         parse_str($query$params);
  58.         if (($params['state'] ?? false) && $user && $user->getEmail()) {
  59.             $barrierCache->setValue('oidc_' $params['state'], $user->getId());
  60.         }
  61.         return $redirect;
  62.     }
  63.     /**
  64.      * @Route("/login_check", name="login_check")
  65.      * @Security("is_granted('ROLE_NOTARY') or is_granted('ROLE_REFOVOD')")
  66.      */
  67.     public function loginCheckAction(): Response
  68.     {
  69.         return new Response();
  70.     }
  71.     /**
  72.      * @Route("/partner_login_check", name="partner_login_check")
  73.      * @Security("is_granted('ROLE_NOTARY') or is_granted('ROLE_REFOVOD')")
  74.      */
  75.     public function partnerLoginCheckAction(): Response
  76.     {
  77.         return new Response();
  78.     }
  79.     /**
  80.      * @Route("/login", name="app_login")
  81.      */
  82.     public function login(
  83.         AuthenticationUtils $authenticationUtils,
  84.         ServiceFormRepository $serviceFormRepository,
  85.         ServiceSectionGroupRepository $serviceSectionGroupRepository,
  86.         Request $request
  87.     ): Response
  88.     {
  89.         if ($this->isGranted('ROLE_NOTARY')) {
  90.             return $this->redirectToRoute('area_notary_index');
  91.         }
  92.         if ($request->getHost() === $this->getParameter('public_appraiser_domain')) {
  93.             return $this->forward(AppraiserController::class . '::homepage', [
  94.                 'authenticationUtils' => $authenticationUtils,
  95.                 'serviceFormRepository' => $serviceFormRepository,
  96.                 'serviceSectionGroupRepository' => $serviceSectionGroupRepository,
  97.             ]);
  98.         }
  99.         list($error$lastUsername$groupsWithSections$paperCopy) =
  100.             $this->getLoginPageData($authenticationUtils$serviceSectionGroupRepository$serviceFormRepository);
  101.         // Костыль, но для срочности. Определим правельный paperCopy, из релевантного groupsWithSections
  102.         // TODO переработать
  103.         /** @see 271 */
  104.         foreach($groupsWithSections[0]['sections'] as $section) {
  105.             foreach($section['serviceForms'] as $form) {
  106.                 if($form['icon'] !== 'paper-copy') {
  107.                     continue;
  108.                 }
  109.                 $paperCopy $form;
  110.             }
  111.         }
  112.         return $this->render('publicPages/notaryLandingPage.html.twig',
  113.             [
  114.                 'controller_name' => 'Сервис электронной оценки',
  115.                 'is_login'        => true,
  116.                 'last_username'   => $lastUsername,
  117.                 'error'           => $error,
  118.                 'group'           => $groupsWithSections[0],
  119.                 'paperCopy'       => $paperCopy,
  120.             ]);
  121.     }
  122.     /**
  123.      * @Route("/remote/login", name="app_login_ajax", methods={"GET", "POST"})
  124.      */
  125.     public function loginAjax(AuthenticationUtils $authenticationUtils): Response
  126.     {
  127.         // Если пользователь уже авторизован (успешный логин)
  128.         if ($this->isGranted('ROLE_USER')) {
  129.             $redirectUrl $this->generateUrl('area_client_index');
  130.             if ($this->isGranted('ROLE_NOTARY')) {
  131.                 $redirectUrl $this->generateUrl('area_notary_index');
  132.             }
  133.             if ($this->isGranted('ROLE_PARTNER')) {
  134.                 $redirectUrl $this->generateUrl('area_partner_index');
  135.             }
  136.             if ($this->isGranted('ROLE_WORKER')) {
  137.                 $redirectUrl $this->generateUrl('area_notary_index');
  138.             }
  139.             if ($this->isGranted('ROLE_INFO_NOT')) {
  140.                 $redirectUrl $this->generateUrl('area_infonot_balance');
  141.             }
  142.             if ($this->isGranted('ROLE_REFOVOD')) {
  143.                 $redirectUrl $this->generateUrl('area_refovod_balance');
  144.             }
  145.             return $this->response([
  146.                 'success' => true,
  147.                 'redirect' => $redirectUrl
  148.             ]);
  149.         }
  150.         $error $authenticationUtils->getLastAuthenticationError();
  151.         if ($error) {
  152.             return $this->response(['error' => $error->getMessage()], 400);
  153.         }
  154.         // Если нет ошибки и пользователь не аутентифицирован - значит форма еще не отправлена
  155.         return $this->response(['error' => 'Неверный email или пароль'], 400);
  156.     }
  157.     /**
  158.      * @Route("/r-p/{code}", name="app_main_referal")
  159.      */
  160.     public function mainForReferal(
  161.         string $code,
  162.         AuthenticationUtils $authenticationUtils,
  163.         ServiceFormRepository $serviceFormRepository,
  164.         ServiceSectionGroupRepository $serviceSectionGroupRepository,
  165.     ): Response
  166.     {
  167.         if ($this->isGranted('ROLE_NOTARY')) {
  168.             return $this->redirectToRoute('area_notary_index');
  169.         }
  170.         list($error$lastUsername$groupsWithSections$paperCopy) =
  171.             $this->getLoginPageData($authenticationUtils$serviceSectionGroupRepository$serviceFormRepository);
  172.         // Костыль, но для срочности. Определим правельный paperCopy, из релевантного groupsWithSections
  173.         // TODO переработать
  174.         /** @see 271 */
  175.         foreach($groupsWithSections[0]['sections'] as $section) {
  176.             foreach($section['serviceForms'] as $form) {
  177.                 if($form['icon'] !== 'paper-copy') {
  178.                     continue;
  179.                 }
  180.                 $paperCopy $form;
  181.             }
  182.         }
  183.         return $this->render('publicPages/notaryLandingPage.html.twig',
  184.             [
  185.                 'controller_name' => 'Сервис электронной оценки',
  186.                 'is_login'        => true,
  187.                 'last_username'   => $lastUsername,
  188.                 'error'           => $error,
  189.                 'group'           => $groupsWithSections[0],
  190.                 'paperCopy'       => $paperCopy,
  191.                 'registerCode'    => $code,
  192.             ]);
  193.     }
  194.     /**
  195.      * @Route("/logout", name="app_logout")
  196.      */
  197.     public function logout()
  198.     {
  199.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  200.     }
  201.     protected function getLoginPageData(
  202.         AuthenticationUtils $authenticationUtils,
  203.         ServiceSectionGroupRepository $serviceSectionGroupRepository,
  204.         ServiceFormRepository $serviceFormRepository,
  205.     ): array
  206.     {
  207.         $error $authenticationUtils->getLastAuthenticationError();
  208.         $lastUsername $authenticationUtils->getLastUsername();
  209.         $groupsWithSections $serviceSectionGroupRepository->getList();
  210.         $services = [];
  211.         foreach ($serviceFormRepository->findAll() ?? [] as $service) {
  212.             $services[$service->getId()] = $service;
  213.         }
  214.         $paperCopy 0;
  215.         foreach ($groupsWithSections as $key => &$group) {
  216.             foreach ($group['sections'] as $sectionKey => $section) {
  217.                 foreach ($section['serviceForms'] ?? [] as $serviceForm) {
  218.                     if ($services[$serviceForm['id']]->getIcon() === 'paper-copy') {
  219.                         //Отдельно отчет
  220.                         $paperCopy $services[$serviceForm['id']];
  221.                         continue;
  222.                     }
  223.                     $groupsWithSections[$key]['sections'][$sectionKey]['forms'][] = $services[$serviceForm['id']];
  224.                 }
  225.             }
  226.             $group['sections'] = array_reverse($group['sections']);
  227.         }
  228.         return [$error$lastUsername$groupsWithSections$paperCopy];
  229.     }
  230. }