src/Controller/HomeController.php line 72

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