src/Controller/HomeController.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Model\User\Entity\Profile\SignatureMode;
  5. use App\Services\WithoutEds\SimpleSignatureVerifier;
  6. use App\Helpers\Timezone\MskDiff;
  7. use App\Model\Domain\Easuz\Notice\UseCase\CreateDraft\Message;
  8. use App\Model\User\Entity\User\UserRepository;
  9. use App\ReadModel\Admin\Settings\SettingsFetcher;
  10. use App\ReadModel\Admin\Settings\TimeZone\TimeZoneFetcher;
  11. use App\ReadModel\Certificate\CertificateFetcher;
  12. use App\ReadModel\Profile\ProfileFetcher;
  13. use App\ReadModel\User\UserJoin\UserJoinFetcher;
  14. use App\Security\UserIdentity;
  15. use App\Services\HandBook\ApiClient;
  16. use App\Services\Notice\NoticeSenderService;
  17. use DateTimeZone;
  18. use Doctrine\DBAL\Exception;
  19. use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  25. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  26. use Symfony\Component\Mailer\MailerInterface;
  27. use Symfony\Component\Mime\Address;
  28. use Symfony\Component\Mime\Email;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. use Twig\Environment;
  31. use Twig\Error\LoaderError;
  32. use Twig\Error\RuntimeError;
  33. use Twig\Error\SyntaxError;
  34. /**
  35.  * @method null|UserIdentity getUser()
  36.  */
  37. class HomeController extends AbstractController
  38. {
  39.     /**
  40.      * @var CertificateFetcher
  41.      */
  42.     private $certificateFetcher;
  43.     /**
  44.      * @var SettingsFetcher
  45.      */
  46.     private SettingsFetcher $settingsFetcher;
  47.     // Редирект в "Мои сертификаты" для этих ролей
  48.     private $roles = [
  49.         'ROLE_USER',
  50.         'ROLE_ORGANIZER',
  51.         'ROLE_PARTICIPANT'
  52.     ];
  53.     public function __construct(
  54.         CertificateFetcher $certificateFetcher,
  55.         SettingsFetcher $settingsFetcher
  56.     ) {
  57.         $this->certificateFetcher = $certificateFetcher;
  58.         $this->settingsFetcher = $settingsFetcher;
  59.     }
  60.     /**
  61.      * @param ProfileFetcher $profileFetcher
  62.      * @return Response
  63.      * @throws Exception
  64.      * @Route("/", name="home")
  65.      */
  66.     public function home(ProfileFetcher $profileFetcher): Response
  67.     {
  68.         $user = $this->getUser();
  69.         if ($user === null) {
  70.             return $this->redirect('login');
  71.         } else {
  72.             if ($user->getRole() === 'ROLE_AUDITOR') {
  73.                 return $this->redirectToRoute('auditor_organizations_procedures', ['guid' => $user->getId()]);
  74.             }
  75.             if ($pId = $user->getProfileId()) {
  76.                 $profile = $profileFetcher->find($pId);
  77.                 if (!$profile->getStatus()->isActive()) {
  78.                     return $this->redirectToRoute('profile', ['profile_id' => $pId]);
  79.                 }
  80.                 return $this->redirectToRoute('procedures');
  81.             }
  82.         }
  83.         if (in_array($user->getRole(), $this->roles)) {
  84.             return $this->redirectToRoute('certificates', ['user_id' => $user->getId()]);
  85.         }
  86.         return $this->render('app/home.html.twig');
  87.     }
  88.     /**
  89.      * @return Response
  90.      * @Route("/health", name="health_check")
  91.      */
  92.     public function health(): Response
  93.     {
  94.         return new Response('OK', 200);
  95.     }
  96.     /**
  97.      * @Route("/api/auth/session", name="api.auth.session", methods={"GET"})
  98.      */
  99.     public function authSession(
  100.         Request $request,
  101.         ProfileFetcher $profileFetcher,
  102.         TimeZoneFetcher $timeZoneFetcher,
  103.         MskDiff $mskDiffHelper,
  104.         ApiClient $apiClient,
  105.         JWTEncoderInterface $jwtEncoder,
  106.         UserJoinFetcher $userJoinFetcher,
  107.         SimpleSignatureVerifier $simpleSignatureVerifier
  108.     ) {
  109.         $timeZonesDict = [
  110.             'Europe/London',
  111.             'Europe/Berlin',
  112.             'Europe/Kaliningrad',
  113.             'Europe/Moscow',
  114.             'Europe/Samara',
  115.             'Asia/Yekaterinburg',
  116.             'Asia/Omsk',
  117.             'Asia/Krasnoyarsk', 'Asia/Irkutsk', 'Asia/Yakutsk', 'Asia/Vladivostok', 'Asia/Sakhalin', 'Asia/Anadyr'];
  118.         $clientTimeZoneValue = $request->query->get('timezone_value', null);
  119.         if ($clientTimeZoneValue !== null) {
  120.             $clientTimeZoneValue        = (int)$clientTimeZoneValue;
  121.         }
  122.         /** @var UserIdentity $session */
  123.         $session = $this->getUser();
  124.         $profile = null;
  125.         if (!$session or !$session->getProfileId()) {
  126.             throw new UnauthorizedHttpException("Unathorized");
  127.         }
  128.         $profileId = $session->getProfileId();
  129.         $profile = $profileFetcher->find($profileId);
  130.         $profileTimeZoneValue = $profile->getTimeZoneValue();
  131.         if ($profileTimeZoneValue !== null) {
  132.             $profileTimeZoneValue = array_flip($timeZonesDict)[$profileTimeZoneValue];
  133.         }
  134.         $clientTimeZone = $timeZoneFetcher->findByValue($clientTimeZoneValue);
  135.         $clientTimeZoneValueFormatted = '';
  136.         if ($clientTimeZoneValue >= 0) {
  137.             $clientTimeZoneValueFormatted = "+$clientTimeZoneValue";
  138.         } elseif ($clientTimeZoneValue < 0) {
  139.             $clientTimeZoneValueFormatted = "-$clientTimeZoneValue";
  140.         }
  141.         $clientTimeZoneTitle = $clientTimeZone ? $clientTimeZone['title'] : null;
  142.         $clientTimeZoneDate = new \DateTimeImmutable();
  143.         $clientTimeZoneDate = $clientTimeZoneDate->setTimezone(new DateTimeZone($clientTimeZoneValueFormatted));
  144.         $clientTimeZoneMskDiff = $mskDiffHelper->getDiffOffset($clientTimeZoneDate);
  145.         $clientTimeZoneMskDiff = $mskDiffHelper->mskFormatter($clientTimeZoneMskDiff);
  146.         $clientTimeZoneText = $clientTimeZoneValue;
  147.         if ($clientTimeZoneValue <= 10 || $clientTimeZoneValue >= -10) {
  148.             $clientTimeZoneText = '0' . $clientTimeZoneValue;
  149.         }
  150.         if ($clientTimeZoneValue >= 0) {
  151.             $clientTimeZoneText = '+' . $clientTimeZoneText;
  152.         } elseif ($clientTimeZoneValue < 0) {
  153.             $clientTimeZoneText = '-' . $clientTimeZoneText;
  154.         }
  155.         $clientTimeZoneText = $clientTimeZoneText . ':00';
  156.         $profileTimeZone = $timeZoneFetcher->findByValue($profileTimeZoneValue);
  157.         if ($profileTimeZoneValue >= 0) {
  158.             $profileTimeZoneValueFormatted = "+$profileTimeZoneValue";
  159.         } elseif ($profileTimeZone < 0) {
  160.             $profileTimeZoneValueFormatted = "-$profileTimeZoneValue";
  161.         }
  162.         $profileTimeZoneTitle = $profileTimeZone ? $profileTimeZone['title'] : null;
  163.         $profileTimeZoneDate = new \DateTimeImmutable();
  164.         $profileTimeZoneDate = $profileTimeZoneDate->setTimezone(new DateTimeZone($profileTimeZoneValueFormatted));
  165.         $profileTimeZoneMskDiff = $mskDiffHelper->getDiffOffset($profileTimeZoneDate);
  166.         $profileTimeZoneMskDiff = $mskDiffHelper->mskFormatter($profileTimeZoneMskDiff);
  167.         $profileTimeZoneText = $profileTimeZoneValue;
  168.         if (abs($profileTimeZoneValue) < 10) {
  169.             $profileTimeZoneText = '0' . abs($profileTimeZoneValue);
  170.         } else {
  171.             $profileTimeZoneText = abs($profileTimeZoneValue);
  172.         }
  173.         if ($profileTimeZoneValue >= 0) {
  174.             $profileTimeZoneText = '+' . $profileTimeZoneText;
  175.         } else {
  176.             $profileTimeZoneText = '-' . $profileTimeZoneText;
  177.         }
  178.         $profileTimeZoneText = $profileTimeZoneText . ':00';
  179.         if (!$session) {
  180.             return new JsonResponse(["session" => null]);
  181.         }
  182.         $userId = $session->getId();
  183.         $cert = $this->certificateFetcher->findDetailByUserId($userId);
  184.         $certificateThumbprint = $cert === null ? null : $cert->thumbprint ?? null;
  185.         $permissions =  $session->getPermissions();
  186.         if ($profileId !== null) {
  187.             $findJoinUser = $userJoinFetcher->findByUserIdAndProfileId($userId, $profileId);
  188.             if ($findJoinUser !== null) {
  189.                 $permissions = $findJoinUser->permissions;
  190.             }
  191.         }
  192.         return new JsonResponse([
  193.             "session" => [
  194.                 "user_id" => $session->getId(),
  195.                 "profile_id" => $profileId,
  196.                 "cert_thumbprint" => $certificateThumbprint,
  197.                 // EDS | SIMPLE — работа без ЭЦП (docs/work-without-eds-module.md, §6.4)
  198.                 "signature_mode" => $simpleSignatureVerifier->isProfileWithoutEds($profileId) ? SignatureMode::SIMPLE : SignatureMode::EDS,
  199.                 "email" => $session->getEmail(),
  200.                 "role" => $session->getRole(),
  201.                 "permissions" => $permissions,
  202.                 "role_profile_value" => $profile === null ? null : $profile->role_constant,
  203.                 "role_profile_name" => $profile === null ? null : $profile->role_name,
  204.                 "profile_type" => $profile === null ? null : $profile->type_profile,
  205.                 "client_time_zone_value" => $clientTimeZoneValue,
  206.                 "client_time_zone_text" => $clientTimeZoneText,
  207.                 "client_time_zone_title" => $clientTimeZoneTitle,
  208.                 'client_time_zone_msk_diff' => $clientTimeZoneMskDiff,
  209.                 "profile_time_zone_value" => $profileTimeZoneValue,
  210.                 "profile_time_zone_text" => $profileTimeZoneText,
  211.                 "profile_time_zone_title" => $profileTimeZoneTitle,
  212.                 'profile_time_zone_msk_diff' => $profileTimeZoneMskDiff,
  213.                 'handbook_url' => $apiClient->getEndpointWithoutApi(),
  214.                 'profileTimeZone' => $mskDiffHelper->getDiffOffset($profileTimeZoneDate),
  215.                 'organizationInn' => $profile->getInn(),
  216.             ]
  217.         ]);
  218.     }
  219.     /**
  220.      * @Route("/api/sentry", name="api.sentry.get", methods={"GET"})
  221.      */
  222.     public function apiSentry(Request $request): JsonResponse
  223.     {
  224.         $dsn = $_ENV['SENTRY_DSN'] ?? null;
  225.         return new JsonResponse(['SENTRY_DSN' => $dsn]);
  226.     }
  227.     /**
  228.      * @param Request $request
  229.      * @return Response
  230.      * @Route("/api/settings/frontend-info", name="api.frontend.info", methods="GET")
  231.      */
  232.     public function apiSettingsFrontendInfo(Request $request)
  233.     {
  234.         $data = $this->settingsFetcher->allArray();
  235.         return new JsonResponse([
  236.             "ORGANIZATION_FULL_NAME" => $data['KEY_FULL_NAME_ORGANIZATION'],
  237.             // ["ORGANIZATION_SHORT_NAME" => $data['KEY_SHORT_NAME_ORGANIZATION']],
  238.             "ORGANIZATION_INN" => $data['KEY_INN_ORGANIZATION'],
  239.             "ORGANIZATION_KPP" => $data['KEY_KPP_ORGANIZATION'],
  240.             "ORGANIZATION_OGRN" => $data['KEY_OGRN_ORGANIZATION'],
  241.             "ORGANIZATION_PAYMENT_ACCOUNT" => $data['KEY_BANK_CHECKING_ACCOUNT_ORGANIZATION'],
  242.             "ORGANIZATION_CORRESPONDENT_ACCOUNT" => $data['KEY_CORRESPONDENT_ACCOUNT_ORGANIZATION'],
  243.             "ORGANIZATION_BANK_NAME" => $data['KEY_BANK_NAME_ORGANIZATION'],
  244.             "ORGANIZATION_BANK_BIC" => $data['KEY_BANK_BIK_ORGANIZATION'],
  245.             "ORGANIZATION_EMAIL" => $data['KEY_EMAIL_SERVICE'],
  246.             "ORGANIZATION_PHONE" => $data['KEY_PHONE_SERVICE'],
  247.             "ORGANIZATION_FACT_ADDRESS=" => $data['KEY_FACT_ADDRESS_ORGANIZATION'],
  248.             "ORGANIZATION_LEGAL_ADDRESS=" => $data['KEY_LEGAL_ADDRESS_ORGANIZATION'],
  249.             "PLATFORM_EMAIL_INFO" => $data['KEY_EMAIL_SERVICE'],
  250.             "KEY_NAME_SERVICE" => $data['KEY_NAME_SERVICE'],
  251.             // "PLATFORM_EMAIL_SUPPORT" => $data['KEY_EMAIL_SERVICE'],
  252.             // "LK_DOMAIN" => $data[""],
  253.             "PLATFORM_DOMAIN" => "",
  254.         ]);
  255.         // $data = $this->settingsFetcher->
  256.     }
  257.     /**
  258.      * @param string $email
  259.      * @return JsonResponse
  260.      * @Route("/test-mail/{email}", name="test-mail", methods="GET")
  261.      */
  262.     public function testMail(Environment $twig, string $email, MailerInterface $mailer)
  263.     {
  264.         try {
  265.             $emailMessage = (new Email())
  266.                 ->from(new Address("no-reply@rftorgi.ru", "ЭТП РфТорги"))
  267.                 ->to($email)
  268.                 ->subject("Сообщения")
  269.                 ->text("Тестовое сообщение");
  270.             // ->html($content);
  271.             // $headers = new Headers();
  272.             $headers = $emailMessage->getHeaders();
  273.             $headers->addTextHeader('List-Unsubscribe', '<https://lk.rftorgi.ru/unsubscribe>, <mailto:' . $email . '>');
  274.             $emailMessage->setHeaders($headers);
  275.             $mailer->send($emailMessage);
  276.             return new JsonResponse(["status" => "ok"]);
  277.         } catch (LoaderError $e) {
  278.             echo $e->getMessage();
  279.         } catch (RuntimeError $e) {
  280.             echo $e->getMessage();
  281.         } catch (SyntaxError $e) {
  282.             echo $e->getMessage();
  283.         } catch (TransportExceptionInterface $e) {
  284.             echo $e->getMessage();
  285.         }
  286.     }
  287. }