bundles/KVBundle/Controller/KombiVerkehrController.php line 140

Open in your IDE?
  1. <?php
  2. namespace KVBundle\Controller;
  3. use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
  4. use KVBundle\Service\CatJsonWebservice;
  5. use Pimcore\Controller\FrontendController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use KVBundle\OAuth\NextTuesdayResourceOwner;
  8. use KVBundle\OAuth\KVUserService;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Pimcore\Log\ApplicationLogger;
  11. class KombiVerkehrController extends FrontendController
  12. {
  13.     protected $hwi;
  14.     protected $requestStack;
  15.     protected $userService;
  16.     protected $catJsonWebservice;
  17.     public ApplicationLogger $applicationLogger;
  18.     protected $helpdeskMode false;
  19.     protected $loginUserId null;
  20.     protected $geschaeftspartnerId null;
  21.     protected $helpdesk_loginUserId null;
  22.     protected $helpdesk_geschaeftspartnerId null;
  23.     public function __construct (NextTuesdayResourceOwner $resourceRequestStack $requestStackKVUserService $userServiceCatJsonWebservice $catJsonWebserviceApplicationLogger $applicationLogger)
  24.     {
  25.         $this->hwi $resource;
  26.         $this->requestStack $requestStack;
  27.         $this->userService $userService;
  28.         $this->catJsonWebservice $catJsonWebservice;
  29.         $this->applicationLogger $applicationLogger;
  30.         $this->setHelpdeskMode(false);
  31.         $userSessionData $this->getUserSessionData();
  32.         if (array_key_exists('data'$userSessionData) && array_key_exists('catData'$userSessionData)) {
  33.             $this->setLoginUserId($userSessionData['data']['ANSPRECHPARTNERID']);
  34.             $this->setGeschaeftspartnerId($userSessionData['catData']['geschaeftspartnerId']);
  35.         }
  36.         // check for Helpdesk Login
  37.         if (array_key_exists('key'$_REQUEST) && strlen(trim($_REQUEST['key'])) > 0) {
  38.             if ($userSessionData && array_key_exists('data'$userSessionData) && ($userSessionData['data']['PERMISSIONS']['krankikom-orders-view-customers-orders'] === true || $userSessionData['data']['LOGIN'] === 'service.kombiverkehr@krankikom.de')) {
  39.                 $this->setHelpdeskMode(true);
  40.                 $keyData explode('#'base64_decode(substr($_REQUEST['key'], 20)));
  41.                 $key_sessionId $keyData[0];
  42.                 $key_gID $keyData[1];
  43.                 $key_userId $keyData[2];
  44.                 if ($key_sessionId === sha1(session_id())) {
  45.                     $this->log('--HELPDESK LOGIN ENABLED-- g='.$this->getGeschaeftspartnerId().', u='.$this->getLoginUserId().' --> g='.$key_gID.', u='.$key_userId1);
  46.                     $this->setHelpdeskLoginUserId($userSessionData['data']['ANSPRECHPARTNERID']);
  47.                     $this->setHelpdeskGeschaeftspartnerId($userSessionData['catData']['geschaeftspartnerId']);
  48.                     $this->setLoginUserId($key_userId);
  49.                     $this->setGeschaeftspartnerId($key_gID);
  50.                 } else {
  51.                     http_response_code(401);
  52.                     die;
  53.                 }
  54.             } else {
  55.                 http_response_code(401);
  56.                 die;
  57.             }
  58.         }
  59.     }
  60.     /**
  61.      *
  62.      * logLevel 1 / 2 / 3
  63.      * 1 = only minimum log, 2 = log more... , 3 = log everything
  64.      *
  65.      * type "error" will always be logged, doesn't matter which logLevel
  66.      *
  67.      * @param  string $msg
  68.      * @param  int    $logLevel
  69.      * @param  string $type
  70.      * @return bool
  71.      */
  72.     public function log(string $msgint $logLevel 1string $type 'info'): bool
  73.     {
  74.         if ($logLevel $_ENV['MAX_LOG_LEVEL'] && $type != 'error') {
  75.             return false;
  76.         }
  77.         $msg $msg ' # 'session_id() . ' ## ' .gethostname();
  78.         switch($type) {
  79.             case 'error':
  80.                 $this->applicationLogger->error($msg);
  81.                 break;
  82.             default:
  83.                 $this->applicationLogger->info($msg);
  84.         }
  85.         return true;
  86.     }
  87.     /**
  88.      * check if the user has a valid Session.
  89.      * this is done by watching key oauth_user in the Session object
  90.      * @param Request $request
  91.      * @return bool
  92.      */
  93.     public function checkUserLoggedIn(Request $request) : bool
  94.     {
  95.         $session $this->requestStack->getSession();
  96.         if(!empty($session->get("oauth_user")))
  97.         {
  98.             return true;
  99.         }
  100.         return false;
  101.     }
  102.     public function setLoggedInSessionKey(UserResponseInterface $info)
  103.     {
  104.         $userArray $this->userService->loadUserByOAuthUserResponse($info);
  105.         $this->log('Fetching Ansprechpartner data from CAT - got ID: '.$userArray['data']['ANSPRECHPARTNERID'].' - FROM: ' $this->hwi->getOption('authorization_url'), 1);
  106.         // get Ansprechpartner Data from CAT
  107.         if (empty($userArray['data']['ANSPRECHPARTNERID'])) {
  108.             echo ('did not receive ANSPRECHPARTNERID from NT'); echo '<pre>'var_dump($userArray); die;
  109.         }
  110.         $userArray['catData'] = $this->catJsonWebservice->fetchAnsprechpartnerByIdFromCat($userArray['data']['ANSPRECHPARTNERID']);
  111.         $session $this->requestStack->getSession();
  112.         $session->set("oauth_user"$userArray);
  113.     }
  114.     /**
  115.      * returns the stored user Data
  116.      * @return string[]
  117.      */
  118.     protected function getUserSessionData() : array
  119.     {
  120.         if($this->requestStack->getSession()->get("oauth_user"))
  121.         {
  122.             return $this->requestStack->getSession()->get("oauth_user");
  123.         }
  124.         return array("info" => "no session data found");
  125.     }
  126.     /**
  127.      * removes session data
  128.      * @return string[]
  129.      */
  130.     protected function removeUserSessionData() : bool
  131.     {
  132.         $session $this->requestStack->getSession();
  133.         $session->remove("oauth_user");
  134.         $session->clear();
  135.         return true;
  136.     }
  137.     /**
  138.      * Get the value of loginUserId
  139.      */
  140.     public function getLoginUserId()
  141.     {
  142.         return $this->loginUserId;
  143.     }
  144.     /**
  145.      * Set the value of loginUserId
  146.      *
  147.      * @return  self
  148.      */
  149.     public function setLoginUserId($loginUserId)
  150.     {
  151.         $this->loginUserId $loginUserId;
  152.         return $this;
  153.     }
  154.     /**
  155.      * Get the value of geschaeftspartnerId
  156.      */
  157.     public function getGeschaeftspartnerId()
  158.     {
  159.         return $this->geschaeftspartnerId;
  160.     }
  161.     /**
  162.      * Set the value of geschaeftspartnerId
  163.      *
  164.      * @return  self
  165.      */
  166.     public function setGeschaeftspartnerId($geschaeftspartnerId)
  167.     {
  168.         $this->geschaeftspartnerId $geschaeftspartnerId;
  169.         return $this;
  170.     }
  171.     /**
  172.      * Get the value of helpdeskMode
  173.      */
  174.     public function getHelpdeskMode()
  175.     {
  176.         return $this->helpdeskMode;
  177.     }
  178.     /**
  179.      * Set the value of helpdeskMode
  180.      *
  181.      * @return  self
  182.      */
  183.     public function setHelpdeskMode($helpdeskMode)
  184.     {
  185.         $this->helpdeskMode $helpdeskMode;
  186.         return $this;
  187.     }
  188.     /**
  189.      * Get the value of helpdeskSuffix
  190.      */
  191.     public function getHelpdeskSuffix()
  192.     {
  193.         $suffix '';
  194.         if ($this->getHelpdeskMode()) {
  195.             $suffix '-helpdesk-'.$this->getHelpdeskGeschaeftspartnerId().'-'.$this->getHelpdeskLoginUserId();
  196.         }
  197.         return $suffix;
  198.     }
  199.     /**
  200.      * Get the value of helpdesk_loginUserId
  201.      */
  202.     public function getHelpdeskLoginUserId()
  203.     {
  204.         return $this->helpdesk_loginUserId;
  205.     }
  206.     /**
  207.      * Set the value of helpdesk_loginUserId
  208.      *
  209.      * @return  self
  210.      */
  211.     public function setHelpdeskLoginUserId($helpdesk_loginUserId)
  212.     {
  213.         $this->helpdesk_loginUserId $helpdesk_loginUserId;
  214.         return $this;
  215.     }
  216.     /**
  217.      * Get the value of helpdesk_geschaeftspartnerId
  218.      */
  219.     public function getHelpdeskGeschaeftspartnerId()
  220.     {
  221.         return $this->helpdesk_geschaeftspartnerId;
  222.     }
  223.     /**
  224.      * Set the value of helpdesk_geschaeftspartnerId
  225.      *
  226.      * @return  self
  227.      */
  228.     public function setHelpdeskGeschaeftspartnerId($helpdesk_geschaeftspartnerId)
  229.     {
  230.         $this->helpdesk_geschaeftspartnerId $helpdesk_geschaeftspartnerId;
  231.         return $this;
  232.     }
  233. }