src/Services/Main/SecurityService.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Services\Main;
  3. use App\Object\Admin\User;
  4. use App\Object\Api\CallResponse;
  5. use App\Object\Api\Content;
  6. use App\Services\Util\JsonService;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class SecurityService
  11. {
  12.     /**
  13.      * @var ApiRequestService
  14.      */
  15.     private ApiRequestService $apiRequestService;
  16.     /**
  17.      * @var ContainerInterface
  18.      */
  19.     private ContainerInterface $container;
  20.     /**
  21.      * @var JsonService
  22.      */
  23.     private JsonService $jsonService;
  24.     /**
  25.      * @var Request
  26.      */
  27.     private $request//do not type hint
  28.     /**
  29.      * SecurityService constructor.
  30.      *
  31.      * @param ApiRequestService $apiRequestService
  32.      * @param ContainerInterface $container
  33.      * @param JsonService $jsonService
  34.      * @param RequestStack $request
  35.      */
  36.     public function __construct(
  37.         ApiRequestService $apiRequestService,
  38.         ContainerInterface $container,
  39.         JsonService $jsonService,
  40.         RequestStack $request
  41.     ) {
  42.         $this->apiRequestService $apiRequestService;
  43.         $this->container $container;
  44.         $this->jsonService $jsonService;
  45.         $this->request $request->getCurrentRequest();
  46.     }
  47.     /**
  48.      * @return mixed
  49.      */
  50.     public function getToken()
  51.     {
  52.         return $this->request->getSession()->get('_token');
  53.     }
  54.     /**
  55.      * @param $token
  56.      */
  57.     public function setToken($token): void
  58.     {
  59.         $this->request->getSession()->set('_token'$token);
  60.     }
  61.     /**
  62.      * @param $refreshToken
  63.      */
  64.     public function setRefreshToken($refreshToken): void
  65.     {
  66.         $this->request->getSession()->set('_refreshToken'$refreshToken);
  67.     }
  68.     /**
  69.      * @return mixed
  70.      */
  71.     public function getRefreshToken()
  72.     {
  73.         return $this->request->getSession()->get('_refreshToken');
  74.     }
  75.     /**
  76.      * @return bool
  77.      */
  78.     public function refreshToken(): bool
  79.     {
  80.         $response $this->apiRequestService->request('GET''token/refresh', [
  81.             'auth_bearer' => $this->getToken(),
  82.             'json' => [
  83.                 'refresh_token' => $this->getRefreshToken(),
  84.             ],
  85.         ]);
  86.         //TODO: ITDB-502
  87.         //Carefull $apiservice->get() call refreshToken()
  88.         /*$apiService = $this->container->get(ApiService::class);
  89.         $response = $apiService->get('token/refresh', [
  90.             'auth_bearer' => $this->getToken(),
  91.             'json' => [
  92.                 'refresh_token' => $this->getRefreshToken()
  93.             ],
  94.             'defaultResult' => []
  95.         ]);*/
  96.         if (!$response->getSuccess()) {
  97.             return false;
  98.         }
  99.         /*if ($response->getArrayResponse()['error']) {
  100.             return false;
  101.         }*/
  102.         $tokenResult $response->getContent()->getContent();
  103.         $this->setToken($tokenResult['token']);
  104.         $this->setRefreshToken($tokenResult['refresh_token']);
  105.         return true;
  106.     }
  107.     /**
  108.      * @param array $settings
  109.      * @param string $customerId
  110.      *
  111.      * @return array|bool
  112.      */
  113.     public function updateSSOParameters(array $settingsstring $customerId)
  114.     {
  115.         $customerSettings $this->getSSOParameters($customerId);
  116.         if (!$customerSettings) {
  117.             return false;
  118.         }
  119.         $settings['idp']['entityId'] = $customerSettings['entityId'];
  120.         $settings['idp']['x509cert'] = $customerSettings['x509cert'];
  121.         $settings['idp']['singleSignOnService']['url'] = $customerSettings['singleSignOnServiceUrl'];
  122.         $settings['idp']['singleLogoutService']['url'] = $customerSettings['singleLogoutServiceUrl'];
  123.         return $settings;
  124.     }
  125.     /**
  126.      * @param $user
  127.      * @param mixed $customerId
  128.      *
  129.      * @return array|bool
  130.      */
  131.     public function getSSOParameters($customerId)
  132.     {
  133.         $apiService $this->container->get('App\Services\Main\ApiService');
  134.         $response $apiService->get("login/sso-options/{$customerId}", ['defaultResult' => []]);
  135.         if ($response->getArrayResponse()['error']) {
  136.             return false;
  137.         }
  138.         return $response->getContent();
  139.     }
  140.     /**
  141.      * @param $user
  142.      *
  143.      * @return bool|User
  144.      */
  145.     public function refreshUser($user)
  146.     {
  147.         $apiService $this->container->get('App\Services\Main\ApiService');
  148.         if (!$user instanceof User) {
  149.             return false;
  150.         }
  151.         $response $apiService->get('user/me', [
  152.             'auth_bearer' => $this->getToken(),
  153.             'defaultResult' => [],
  154.         ]);
  155.         $responseArray $response->getArrayResponse();
  156.         if ($responseArray['error']) {
  157.             $resultRefreshToken $this->refreshToken();
  158.             if (!$resultRefreshToken) {
  159.                 return false;
  160.             }
  161.             return $user;
  162.         }
  163.         $userArray $responseArray['content'];
  164.         $user->setId($userArray['id']);
  165.         $user->setUsername($userArray['login']);
  166.         $user->setLogin($userArray['login']);
  167.         $user->setCustomer($this->jsonService->denormalize(
  168.             $userArray['customer'],
  169.             'App\Object\Admin\Customer'
  170.         ));
  171.         $user->setCustomerType($userArray['customer']['customer_type']);
  172.         $user->setEmail($userArray['email']);
  173.         $user->setFirstName($userArray['first_name']);
  174.         $user->setLastName($userArray['last_name']);
  175.         $user->setCustomizations($userArray['customizations']);
  176.         $user->setRole($this->jsonService->denormalize(
  177.             $userArray['role'],
  178.             'App\Object\Admin\Role'
  179.         ));
  180.         $role $userArray['role'];
  181.         $listPrivileges array_column($role['privileges'], 'name');
  182.         $user->setPrivileges($listPrivileges);
  183.         $listModules array_column($userArray['customer']['modules'], 'code');
  184.         $user->setModules($listModules);
  185.         $user->setUnreadNotificationsCount($userArray['unread_notifications_count']);
  186.         return $user;
  187.     }
  188.     /**
  189.      * @param $login
  190.      *
  191.      * @return array
  192.      */
  193.     public function requestPasswordRecovery($login): array
  194.     {
  195.         $apiService $this->container->get('App\Services\Main\ApiService');
  196.         return $apiService->post(
  197.             'login/password/recover',
  198.             [
  199.                 'json' => [
  200.                     'login' => $login,
  201.                 ],
  202.             ]
  203.         )->getArrayResponse();
  204.     }
  205.     /**
  206.      * @param $privateToken
  207.      *
  208.      * @return array
  209.      */
  210.     public function getPasswordRecoveryByKey($privateToken): array
  211.     {
  212.         $apiService $this->container->get('App\Services\Main\ApiService');
  213.         return $apiService->get(
  214.             'login/password/recover',
  215.             [
  216.                 'query' => [
  217.                     'privateToken' => $privateToken,
  218.                 ],
  219.             ]
  220.         )->getArrayResponse();
  221.     }
  222.     /**
  223.      * @param $email
  224.      * @param $login
  225.      *
  226.      * @return Content
  227.      */
  228.     public function handleFirstLogin($email$login): Content
  229.     {
  230.         $apiService $this->container->get('App\Services\Main\ApiService');
  231.         return $apiService->post(
  232.             'login/first-login',
  233.             [
  234.                 'json' => [
  235.                     'email' => $email,
  236.                     'login' => $login,
  237.                 ],
  238.             ]
  239.         );
  240.     }
  241.     /**
  242.      * @param $login
  243.      * @param $password
  244.      *
  245.      * @throws DecodingExceptionInterface
  246.      *
  247.      * @return Content
  248.      */
  249.     public function handleDefinePassword($login$password): Content
  250.     {
  251.         $apiService $this->container->get('App\Services\Main\ApiService');
  252.         return $apiService->post(
  253.             'login/define-password',
  254.             [
  255.                 'json' => [
  256.                     'login' => $login,
  257.                     'password' => $password,
  258.                 ],
  259.             ]
  260.         );
  261.     }
  262.     /**
  263.      * @param string $tokenSSO
  264.      *
  265.      * @return CallResponse
  266.      */
  267.     public function logUserSSO(string $tokenSSO): CallResponse
  268.     {
  269.         $apiService $this->container->get('App\Services\Main\ApiService');
  270.         return $apiService->post(
  271.             'login/sso',
  272.             [
  273.                 'json' => [
  274.                     'tokenSSO' => $tokenSSO,
  275.                 ],
  276.             ]
  277.         );
  278.     }
  279.     /**
  280.      * @param string $type
  281.      * @param array $parameters
  282.      * 
  283.      * @return array
  284.      */
  285.     public function authWithOauth(string $type, array $parameters): array
  286.     {
  287.         $apiService $this->container->get('App\Services\Main\ApiService');
  288.         $response $apiService->post(
  289.             'login/oauth',
  290.             [
  291.                 'json' => [
  292.                     'authType' => $type,
  293.                     'parameters' => $parameters,
  294.                 ],
  295.                 'defaultResult' => [],
  296.             ]
  297.         );
  298.         return $response->getContent();
  299.     }
  300. }