<?php
namespace KVBundle\Controller;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use KVBundle\Service\CatJsonWebservice;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use KVBundle\OAuth\NextTuesdayResourceOwner;
use KVBundle\OAuth\KVUserService;
use Symfony\Component\HttpFoundation\RequestStack;
use Pimcore\Log\ApplicationLogger;
class KombiVerkehrController extends FrontendController
{
protected $hwi;
protected $requestStack;
protected $userService;
protected $catJsonWebservice;
public ApplicationLogger $applicationLogger;
protected $helpdeskMode = false;
protected $loginUserId = null;
protected $geschaeftspartnerId = null;
protected $helpdesk_loginUserId = null;
protected $helpdesk_geschaeftspartnerId = null;
public function __construct (NextTuesdayResourceOwner $resource, RequestStack $requestStack, KVUserService $userService, CatJsonWebservice $catJsonWebservice, ApplicationLogger $applicationLogger)
{
$this->hwi = $resource;
$this->requestStack = $requestStack;
$this->userService = $userService;
$this->catJsonWebservice = $catJsonWebservice;
$this->applicationLogger = $applicationLogger;
$this->setHelpdeskMode(false);
$userSessionData = $this->getUserSessionData();
if (array_key_exists('data', $userSessionData) && array_key_exists('catData', $userSessionData)) {
$this->setLoginUserId($userSessionData['data']['ANSPRECHPARTNERID']);
$this->setGeschaeftspartnerId($userSessionData['catData']['geschaeftspartnerId']);
}
// check for Helpdesk Login
if (array_key_exists('key', $_REQUEST) && strlen(trim($_REQUEST['key'])) > 0) {
if ($userSessionData && array_key_exists('data', $userSessionData) && ($userSessionData['data']['PERMISSIONS']['krankikom-orders-view-customers-orders'] === true || $userSessionData['data']['LOGIN'] === 'service.kombiverkehr@krankikom.de')) {
$this->setHelpdeskMode(true);
$keyData = explode('#', base64_decode(substr($_REQUEST['key'], 20)));
$key_sessionId = $keyData[0];
$key_gID = $keyData[1];
$key_userId = $keyData[2];
if ($key_sessionId === sha1(session_id())) {
$this->log('--HELPDESK LOGIN ENABLED-- g='.$this->getGeschaeftspartnerId().', u='.$this->getLoginUserId().' --> g='.$key_gID.', u='.$key_userId, 1);
$this->setHelpdeskLoginUserId($userSessionData['data']['ANSPRECHPARTNERID']);
$this->setHelpdeskGeschaeftspartnerId($userSessionData['catData']['geschaeftspartnerId']);
$this->setLoginUserId($key_userId);
$this->setGeschaeftspartnerId($key_gID);
} else {
http_response_code(401);
die;
}
} else {
http_response_code(401);
die;
}
}
}
/**
*
* logLevel 1 / 2 / 3
* 1 = only minimum log, 2 = log more... , 3 = log everything
*
* type "error" will always be logged, doesn't matter which logLevel
*
* @param string $msg
* @param int $logLevel
* @param string $type
* @return bool
*/
public function log(string $msg, int $logLevel = 1, string $type = 'info'): bool
{
if ($logLevel > $_ENV['MAX_LOG_LEVEL'] && $type != 'error') {
return false;
}
$msg = $msg . ' # '. session_id() . ' ## ' .gethostname();
switch($type) {
case 'error':
$this->applicationLogger->error($msg);
break;
default:
$this->applicationLogger->info($msg);
}
return true;
}
/**
* check if the user has a valid Session.
* this is done by watching key oauth_user in the Session object
* @param Request $request
* @return bool
*/
public function checkUserLoggedIn(Request $request) : bool
{
$session = $this->requestStack->getSession();
if(!empty($session->get("oauth_user")))
{
return true;
}
return false;
}
public function setLoggedInSessionKey(UserResponseInterface $info)
{
$userArray = $this->userService->loadUserByOAuthUserResponse($info);
$this->log('Fetching Ansprechpartner data from CAT - got ID: '.$userArray['data']['ANSPRECHPARTNERID'].' - FROM: ' . $this->hwi->getOption('authorization_url'), 1);
// get Ansprechpartner Data from CAT
if (empty($userArray['data']['ANSPRECHPARTNERID'])) {
echo ('did not receive ANSPRECHPARTNERID from NT'); echo '<pre>'; var_dump($userArray); die;
}
$userArray['catData'] = $this->catJsonWebservice->fetchAnsprechpartnerByIdFromCat($userArray['data']['ANSPRECHPARTNERID']);
$session = $this->requestStack->getSession();
$session->set("oauth_user", $userArray);
}
/**
* returns the stored user Data
* @return string[]
*/
protected function getUserSessionData() : array
{
if($this->requestStack->getSession()->get("oauth_user"))
{
return $this->requestStack->getSession()->get("oauth_user");
}
return array("info" => "no session data found");
}
/**
* removes session data
* @return string[]
*/
protected function removeUserSessionData() : bool
{
$session = $this->requestStack->getSession();
$session->remove("oauth_user");
$session->clear();
return true;
}
/**
* Get the value of loginUserId
*/
public function getLoginUserId()
{
return $this->loginUserId;
}
/**
* Set the value of loginUserId
*
* @return self
*/
public function setLoginUserId($loginUserId)
{
$this->loginUserId = $loginUserId;
return $this;
}
/**
* Get the value of geschaeftspartnerId
*/
public function getGeschaeftspartnerId()
{
return $this->geschaeftspartnerId;
}
/**
* Set the value of geschaeftspartnerId
*
* @return self
*/
public function setGeschaeftspartnerId($geschaeftspartnerId)
{
$this->geschaeftspartnerId = $geschaeftspartnerId;
return $this;
}
/**
* Get the value of helpdeskMode
*/
public function getHelpdeskMode()
{
return $this->helpdeskMode;
}
/**
* Set the value of helpdeskMode
*
* @return self
*/
public function setHelpdeskMode($helpdeskMode)
{
$this->helpdeskMode = $helpdeskMode;
return $this;
}
/**
* Get the value of helpdeskSuffix
*/
public function getHelpdeskSuffix()
{
$suffix = '';
if ($this->getHelpdeskMode()) {
$suffix = '-helpdesk-'.$this->getHelpdeskGeschaeftspartnerId().'-'.$this->getHelpdeskLoginUserId();
}
return $suffix;
}
/**
* Get the value of helpdesk_loginUserId
*/
public function getHelpdeskLoginUserId()
{
return $this->helpdesk_loginUserId;
}
/**
* Set the value of helpdesk_loginUserId
*
* @return self
*/
public function setHelpdeskLoginUserId($helpdesk_loginUserId)
{
$this->helpdesk_loginUserId = $helpdesk_loginUserId;
return $this;
}
/**
* Get the value of helpdesk_geschaeftspartnerId
*/
public function getHelpdeskGeschaeftspartnerId()
{
return $this->helpdesk_geschaeftspartnerId;
}
/**
* Set the value of helpdesk_geschaeftspartnerId
*
* @return self
*/
public function setHelpdeskGeschaeftspartnerId($helpdesk_geschaeftspartnerId)
{
$this->helpdesk_geschaeftspartnerId = $helpdesk_geschaeftspartnerId;
return $this;
}
}