[TASK] EIDUtility: Improve function resolving and error translation handling

This commit is contained in:
Philipp Dieter 2022-11-09 19:18:34 +01:00
parent 252d398174
commit ec7bb84f5e
2 changed files with 151 additions and 11 deletions

View File

@ -19,6 +19,7 @@ use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer; use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
@ -88,6 +89,16 @@ class AbstractEIDController
*/ */
protected $importLogger = null; protected $importLogger = null;
/**
* uriMapping
*/
protected $uriMapping = null;
/**
* uriMappingValues
*/
protected $uriMappingValues = [];
/* /*
* returns the extensionkey set in the exended calss * returns the extensionkey set in the exended calss
@ -189,6 +200,8 @@ class AbstractEIDController
0, 0,
true true
); );
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
$GLOBALS['LANG']->init('default');
$GLOBALS['TSFE'] = $frontendController; $GLOBALS['TSFE'] = $frontendController;
$frontendController->connectToDB(); $frontendController->connectToDB();
$frontendController->fe_user = EidUtility::initFeUser(); $frontendController->fe_user = EidUtility::initFeUser();
@ -219,15 +232,44 @@ class AbstractEIDController
return $response->withStatus(404); return $response->withStatus(404);
} }
$httpMethod = strtolower($request->getMethod()); $httpMethod = strtolower($request->getMethod());
if ($apiObjectId) { if ($this->uriMapping) {
$requestMethod = $httpMethod $uriParts = explode('/', $request->getUri()->getPath());
. ucfirst($apiObject) $uriParts = array_slice($uriParts, 3);
. 'SingleRequest'; foreach ($this->uriMapping[$httpMethod] as $mapping => $function) {
$request->apiObjectId = $apiObjectId; $mappingParts = explode('/', $mapping);
$mappingParts = array_slice($mappingParts, 1);
$max = max(count($mappingParts), count($uriParts));
$mappingMatching = true;
for ($i = 0; $i < $max; $i++) {
if ($uriParts[$i] == $mappingParts[$i]) {
continue;
}
if (
$uriParts[$i]
&& substr($mappingParts[$i], 0, 1) == '{'
&& substr($mappingParts[$i], -1, 1) == '}'
) {
$mappingKey = substr($mappingParts[$i], 1, -1);
$this->uriMappingValues[$mappingKey] = $uriParts[$i];
continue;
}
$mappingMatching = false;
}
if ($mappingMatching == true) {
$requestMethod = $function . 'Request';
}
}
} else { } else {
$requestMethod = $httpMethod if ($apiObjectId) {
. ucfirst($apiObject) $requestMethod = $httpMethod
. 'Request'; . ucfirst($apiObject)
. 'SingleRequest';
$request->apiObjectId = $apiObjectId;
} else {
$requestMethod = $httpMethod
. ucfirst($apiObject)
. 'Request';
}
} }
if (method_exists($this, $requestMethod)) { if (method_exists($this, $requestMethod)) {
$responseData = $this->$requestMethod($request, $response); $responseData = $this->$requestMethod($request, $response);

View File

@ -119,8 +119,9 @@ trait ValidationTrait
* @param schema * @param schema
* @return void * @return void
*/ */
protected function validateAgainstSchema($input, $schema) protected function validateAgainstSchema(
{ $input, $schema, $translate = false
) {
$validator = new Validator(); $validator = new Validator();
$input = ArrayUtility::removeEmptyStrings($input); $input = ArrayUtility::removeEmptyStrings($input);
if (is_array($input) && array_key_exists('eID', $input)) { if (is_array($input) && array_key_exists('eID', $input)) {
@ -141,6 +142,7 @@ trait ValidationTrait
json_encode($schema), json_encode($schema),
-1 -1
); );
if (!$validationResult->isValid()) { if (!$validationResult->isValid()) {
$this->isValid = false; $this->isValid = false;
$this->responseStatus = [400 => 'validationError']; $this->responseStatus = [400 => 'validationError'];
@ -166,6 +168,9 @@ trait ValidationTrait
]; ];
} }
} }
if ($translate) {
$this->translateErrorMessages($validationResult);
}
} }
return $validationResult; return $validationResult;
} }
@ -213,7 +218,8 @@ trait ValidationTrait
$translation = LocalizationUtility::translate( $translation = LocalizationUtility::translate(
$key, $key,
$this->getExtensionKey(), $this->getExtensionKey(),
$arguments $arguments,
'de'
); );
if ($translation) { if ($translation) {
return $translation; return $translation;
@ -229,5 +235,97 @@ trait ValidationTrait
return null; return null;
} }
/**
* translate error messages to user readable strings
*/
protected function translateErrorMessages($validationResult)
{
foreach ($validationResult->getErrors() as $error){
$errorLabel = null;
$field = implode('.', $error->dataPointer());
if ($error->keyword() == 'required') {
$tmp = $error->dataPointer();
array_push($tmp, $error->keywordArgs()['missing']);
$field = implode('.', $tmp);
}
if ($error->keyword() == 'additionalProperties') {
continue;
}
switch ($error->keyword()) {
case 'required':
$errorLabel = $this->getTranslation(
'error.' . $field . '.required'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.required', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
case 'pattern':
$errorLabel = $this->getTranslation(
'error.' . $field . '.pattern'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.pattern', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
case 'format':
$errorLabel = $this->getTranslation(
'error.' . $field . '.format'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.format', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
default:
$errorLabel = $this->getTranslation(
'error.' . $field . '.' . $error->keyword()
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
}
}
}
} }