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 ($user->getRole() === 'ROLE_AUDITOR') {
  71.                 return $this->redirectToRoute('auditor_organizations_procedures', ['guid' => $user->getId()]);
  72.             }
  73.             if ($pId $user->getProfileId()) {
  74.                 $profile $profileFetcher->find($pId);
  75.                 if (!$profile->getStatus()->isActive()) {
  76.                     return $this->redirectToRoute('profile', ['profile_id' => $pId]);
  77.                 }
  78.                 return $this->redirectToRoute('procedures');
  79.             }
  80.         }
  81.         if (in_array($user->getRole(), $this->roles)) {
  82.             return $this->redirectToRoute('certificates', ['user_id' => $user->getId()]);
  83.         }
  84.         return $this->render('app/home.html.twig');
  85.     }
  86.     /**
  87.      * @return Response
  88.      * @Route("/health", name="health_check")
  89.      */
  90.     public function health(): Response
  91.     {
  92.         return new Response('OK'200);
  93.     }
  94.     /**
  95.      * @Route("/api/auth/session", name="api.auth.session", methods={"GET"})
  96.      */
  97.     public function authSession(
  98.         Request $request,
  99.         ProfileFetcher $profileFetcher,
  100.         TimeZoneFetcher $timeZoneFetcher,
  101.         MskDiff $mskDiffHelper,
  102.         ApiClient $apiClient,
  103.         JWTEncoderInterface $jwtEncoder,
  104.         UserJoinFetcher $userJoinFetcher
  105.     ) {
  106.         $timeZonesDict = [
  107.             'Europe/London',
  108.             'Europe/Berlin',
  109.             'Europe/Kaliningrad',
  110.             'Europe/Moscow',
  111.             'Europe/Samara',
  112.             'Asia/Yekaterinburg',
  113.             'Asia/Omsk',
  114.             'Asia/Krasnoyarsk''Asia/Irkutsk''Asia/Yakutsk''Asia/Vladivostok''Asia/Sakhalin''Asia/Anadyr'];
  115.         $clientTimeZoneValue $request->query->get('timezone_value'null);
  116.         if ($clientTimeZoneValue !== null) {
  117.             $clientTimeZoneValue        = (int)$clientTimeZoneValue;
  118.         }
  119.         /** @var UserIdentity $session */
  120.         $session $this->getUser();
  121.         $profile null;
  122.         if (!$session or !$session->getProfileId()) {
  123.             throw new UnauthorizedHttpException("Unathorized");
  124.         }
  125.         $profileId $session->getProfileId();
  126.         $profile $profileFetcher->find($profileId);
  127.         $profileTimeZoneValue $profile->getTimeZoneValue();
  128.         if ($profileTimeZoneValue !== null) {
  129.             $profileTimeZoneValue array_flip($timeZonesDict)[$profileTimeZoneValue];
  130.         }
  131.         $clientTimeZone $timeZoneFetcher->findByValue($clientTimeZoneValue);
  132.         $clientTimeZoneValueFormatted '';
  133.         if ($clientTimeZoneValue >= 0) {
  134.             $clientTimeZoneValueFormatted "+$clientTimeZoneValue";
  135.         } elseif ($clientTimeZoneValue 0) {
  136.             $clientTimeZoneValueFormatted "-$clientTimeZoneValue";
  137.         }
  138.         $clientTimeZoneTitle $clientTimeZone $clientTimeZone['title'] : null;
  139.         $clientTimeZoneDate = new \DateTimeImmutable();
  140.         $clientTimeZoneDate $clientTimeZoneDate->setTimezone(new DateTimeZone($clientTimeZoneValueFormatted));
  141.         $clientTimeZoneMskDiff $mskDiffHelper->getDiffOffset($clientTimeZoneDate);
  142.         $clientTimeZoneMskDiff $mskDiffHelper->mskFormatter($clientTimeZoneMskDiff);
  143.         $clientTimeZoneText $clientTimeZoneValue;
  144.         if ($clientTimeZoneValue <= 10 || $clientTimeZoneValue >= -10) {
  145.             $clientTimeZoneText '0' $clientTimeZoneValue;
  146.         }
  147.         if ($clientTimeZoneValue >= 0) {
  148.             $clientTimeZoneText '+' $clientTimeZoneText;
  149.         } elseif ($clientTimeZoneValue 0) {
  150.             $clientTimeZoneText '-' $clientTimeZoneText;
  151.         }
  152.         $clientTimeZoneText $clientTimeZoneText ':00';
  153.         $profileTimeZone $timeZoneFetcher->findByValue($profileTimeZoneValue);
  154.         if ($profileTimeZoneValue >= 0) {
  155.             $profileTimeZoneValueFormatted "+$profileTimeZoneValue";
  156.         } elseif ($profileTimeZone 0) {
  157.             $profileTimeZoneValueFormatted "-$profileTimeZoneValue";
  158.         }
  159.         $profileTimeZoneTitle $profileTimeZone $profileTimeZone['title'] : null;
  160.         $profileTimeZoneDate = new \DateTimeImmutable();
  161.         $profileTimeZoneDate $profileTimeZoneDate->setTimezone(new DateTimeZone($profileTimeZoneValueFormatted));
  162.         $profileTimeZoneMskDiff $mskDiffHelper->getDiffOffset($profileTimeZoneDate);
  163.         $profileTimeZoneMskDiff $mskDiffHelper->mskFormatter($profileTimeZoneMskDiff);
  164.         $profileTimeZoneText $profileTimeZoneValue;
  165.         if (abs($profileTimeZoneValue) < 10) {
  166.             $profileTimeZoneText '0' abs($profileTimeZoneValue);
  167.         } else {
  168.             $profileTimeZoneText abs($profileTimeZoneValue);
  169.         }
  170.         if ($profileTimeZoneValue >= 0) {
  171.             $profileTimeZoneText '+' $profileTimeZoneText;
  172.         } else {
  173.             $profileTimeZoneText '-' $profileTimeZoneText;
  174.         }
  175.         $profileTimeZoneText $profileTimeZoneText ':00';
  176.         if (!$session) {
  177.             return new JsonResponse(["session" => null]);
  178.         }
  179.         $userId $session->getId();
  180.         $cert $this->certificateFetcher->findDetailByUserId($userId);
  181.         $certificateThumbprint $cert === null null $cert->thumbprint ?? null;
  182.         $permissions =  $session->getPermissions();
  183.         if ($profileId !== null) {
  184.             $findJoinUser $userJoinFetcher->findByUserIdAndProfileId($userId$profileId);
  185.             if ($findJoinUser !== null) {
  186.                 $permissions $findJoinUser->permissions;
  187.             }
  188.         }
  189.         return new JsonResponse([
  190.             "session" => [
  191.                 "user_id" => $session->getId(),
  192.                 "profile_id" => $profileId,
  193.                 "cert_thumbprint" => $certificateThumbprint,
  194.                 "email" => $session->getEmail(),
  195.                 "role" => $session->getRole(),
  196.                 "permissions" => $permissions,
  197.                 "role_profile_value" => $profile === null null $profile->role_constant,
  198.                 "role_profile_name" => $profile === null null $profile->role_name,
  199.                 "profile_type" => $profile === null null $profile->type_profile,
  200.                 "client_time_zone_value" => $clientTimeZoneValue,
  201.                 "client_time_zone_text" => $clientTimeZoneText,
  202.                 "client_time_zone_title" => $clientTimeZoneTitle,
  203.                 'client_time_zone_msk_diff' => $clientTimeZoneMskDiff,
  204.                 "profile_time_zone_value" => $profileTimeZoneValue,
  205.                 "profile_time_zone_text" => $profileTimeZoneText,
  206.                 "profile_time_zone_title" => $profileTimeZoneTitle,
  207.                 'profile_time_zone_msk_diff' => $profileTimeZoneMskDiff,
  208.                 'handbook_url' => $apiClient->getEndpointWithoutApi(),
  209.                 'profileTimeZone' => $mskDiffHelper->getDiffOffset($profileTimeZoneDate),
  210.                 'organizationInn' => $profile->getInn(),
  211.             ]
  212.         ]);
  213.     }
  214.     /**
  215.      * @Route("/api/sentry", name="api.sentry.get", methods={"GET"})
  216.      */
  217.     public function apiSentry(Request $request): JsonResponse
  218.     {
  219.         $dsn $_ENV['SENTRY_DSN'] ?? null;
  220.         return new JsonResponse(['SENTRY_DSN' => $dsn]);
  221.     }
  222.     /**
  223.      * @param Request $request
  224.      * @return Response
  225.      * @Route("/api/settings/frontend-info", name="api.frontend.info", methods="GET")
  226.      */
  227.     public function apiSettingsFrontendInfo(Request $request)
  228.     {
  229.         $data $this->settingsFetcher->allArray();
  230.         return new JsonResponse([
  231.             "ORGANIZATION_FULL_NAME" => $data['KEY_FULL_NAME_ORGANIZATION'],
  232.             // ["ORGANIZATION_SHORT_NAME" => $data['KEY_SHORT_NAME_ORGANIZATION']],
  233.             "ORGANIZATION_INN" => $data['KEY_INN_ORGANIZATION'],
  234.             "ORGANIZATION_KPP" => $data['KEY_KPP_ORGANIZATION'],
  235.             "ORGANIZATION_OGRN" => $data['KEY_OGRN_ORGANIZATION'],
  236.             "ORGANIZATION_PAYMENT_ACCOUNT" => $data['KEY_BANK_CHECKING_ACCOUNT_ORGANIZATION'],
  237.             "ORGANIZATION_CORRESPONDENT_ACCOUNT" => $data['KEY_CORRESPONDENT_ACCOUNT_ORGANIZATION'],
  238.             "ORGANIZATION_BANK_NAME" => $data['KEY_BANK_NAME_ORGANIZATION'],
  239.             "ORGANIZATION_BANK_BIC" => $data['KEY_BANK_BIK_ORGANIZATION'],
  240.             "ORGANIZATION_EMAIL" => $data['KEY_EMAIL_SERVICE'],
  241.             "ORGANIZATION_PHONE" => $data['KEY_PHONE_SERVICE'],
  242.             "ORGANIZATION_FACT_ADDRESS=" => $data['KEY_FACT_ADDRESS_ORGANIZATION'],
  243.             "ORGANIZATION_LEGAL_ADDRESS=" => $data['KEY_LEGAL_ADDRESS_ORGANIZATION'],
  244.             "PLATFORM_EMAIL_INFO" => $data['KEY_EMAIL_SERVICE'],
  245.             "KEY_NAME_SERVICE" => $data['KEY_NAME_SERVICE'],
  246.             // "PLATFORM_EMAIL_SUPPORT" => $data['KEY_EMAIL_SERVICE'],
  247.             // "LK_DOMAIN" => $data[""],
  248.             "PLATFORM_DOMAIN" => "",
  249.         ]);
  250.         // $data = $this->settingsFetcher->
  251.     }
  252.     /**
  253.      * @param string $email
  254.      * @return JsonResponse
  255.      * @Route("/test-mail/{email}", name="test-mail", methods="GET")
  256.      */
  257.     public function testMail(Environment $twigstring $emailMailerInterface $mailer)
  258.     {
  259.         try {
  260.             $emailMessage = (new Email())
  261.                 ->from(new Address("no-reply@rftorgi.ru""ЭТП РфТорги"))
  262.                 ->to($email)
  263.                 ->subject("Сообщения")
  264.                 ->text("Тестовое сообщение");
  265.             // ->html($content);
  266.             // $headers = new Headers();
  267.             $headers $emailMessage->getHeaders();
  268.             $headers->addTextHeader('List-Unsubscribe''<https://lk.rftorgi.ru/unsubscribe>, <mailto:' $email '>');
  269.             $emailMessage->setHeaders($headers);
  270.             $mailer->send($emailMessage);
  271.             return new JsonResponse(["status" => "ok"]);
  272.         } catch (LoaderError $e) {
  273.             echo $e->getMessage();
  274.         } catch (RuntimeError $e) {
  275.             echo $e->getMessage();
  276.         } catch (SyntaxError $e) {
  277.             echo $e->getMessage();
  278.         } catch (TransportExceptionInterface $e) {
  279.             echo $e->getMessage();
  280.         }
  281.     }
  282. }