mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
add verification tool for admin
This commit is contained in:
parent
12d6829b98
commit
75ba56fa09
@ -28,10 +28,15 @@ use GuzzleHttp\Exception\TransferException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
// TODO à mettre dans services
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
@ -78,6 +83,27 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "{_locale}/admin/doc/gen/generate/test/from/{template}/for/{entityClassName}/{entityId}",
|
||||
* name="chill_docgenerator_test_generate_from_template"
|
||||
* )
|
||||
*/
|
||||
public function adminTestGenerateDocFromTemplateAction(
|
||||
DocGeneratorTemplate $template,
|
||||
string $entityClassName,
|
||||
int $entityId,
|
||||
Request $request
|
||||
): Response {
|
||||
return $this->generateDocFromTemplate(
|
||||
$template,
|
||||
$entityClassName,
|
||||
$entityId,
|
||||
$request,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}",
|
||||
@ -89,6 +115,81 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
string $entityClassName,
|
||||
int $entityId,
|
||||
Request $request
|
||||
): Response {
|
||||
return $this->generateDocFromTemplate(
|
||||
$template,
|
||||
$entityClassName,
|
||||
$entityId,
|
||||
$request,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "/api/1.0/docgen/templates/by-entity/{entityClassName}",
|
||||
* name="chill_docgenerator_templates_for_entity_api"
|
||||
* )
|
||||
*/
|
||||
public function listTemplateApiAction(string $entityClassName): Response
|
||||
{
|
||||
$nb = $this->docGeneratorTemplateRepository->countByEntity($entityClassName);
|
||||
$paginator = $this->paginatorFactory->create($nb);
|
||||
$entities = $this->docGeneratorTemplateRepository->findByEntity(
|
||||
$entityClassName,
|
||||
$paginator->getCurrentPageFirstItemNumber(),
|
||||
$paginator->getItemsPerPage()
|
||||
);
|
||||
|
||||
return $this->json(
|
||||
new Collection($entities, $paginator),
|
||||
Response::HTTP_OK,
|
||||
[],
|
||||
[AbstractNormalizer::GROUPS => ['read']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "{_locale}/admin/doc/gen/generate/test/redirect",
|
||||
* name="chill_docgenerator_test_generate_redirect"
|
||||
* )
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function redirectToTestGenerate(Request $request): RedirectResponse
|
||||
{
|
||||
$template = $request->query->getInt('template');
|
||||
|
||||
if (null === $template) {
|
||||
throw new BadRequestHttpException('template parameter is missing');
|
||||
}
|
||||
|
||||
$entityClassName = $request->query->get('entityClassName');
|
||||
|
||||
if (null === $entityClassName) {
|
||||
throw new BadRequestHttpException('entityClassName is missing');
|
||||
}
|
||||
|
||||
$entityId = $request->query->get('entityId');
|
||||
|
||||
if (null === $entityId) {
|
||||
throw new BadRequestHttpException('entityId is missing');
|
||||
}
|
||||
|
||||
return $this->redirectToRoute(
|
||||
'chill_docgenerator_test_generate_from_template',
|
||||
['template' => $template, 'entityClassName' => $entityClassName, 'entityId' => $entityId,
|
||||
'returnPath' => $request->query->get('returnPath', '/'), ]
|
||||
);
|
||||
}
|
||||
|
||||
private function generateDocFromTemplate(
|
||||
DocGeneratorTemplate $template,
|
||||
string $entityClassName,
|
||||
int $entityId,
|
||||
Request $request,
|
||||
bool $isTest
|
||||
): Response {
|
||||
try {
|
||||
$context = $this->contextManager->getContextByDocGeneratorTemplate($template);
|
||||
@ -105,9 +206,25 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
$contextGenerationData = [];
|
||||
|
||||
if ($context instanceof DocGeneratorContextWithPublicFormInterface
|
||||
&& $context->hasPublicForm($template, $entity)) {
|
||||
$builder = $this->createFormBuilder($context->getFormData($template, $entity));
|
||||
&& $context->hasPublicForm($template, $entity) || $isTest) {
|
||||
if ($context instanceof DocGeneratorContextWithPublicFormInterface) {
|
||||
$builder = $this->createFormBuilder(
|
||||
array_merge(
|
||||
$context->getFormData($template, $entity),
|
||||
$isTest ? ['test_file' => null] : []
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$context->buildPublicForm($builder, $template, $entity);
|
||||
|
||||
if ($isTest) {
|
||||
$builder->add('test_file', FileType::class, [
|
||||
'label' => 'Template file',
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$form = $builder->getForm()->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
@ -121,36 +238,41 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
$getUrlGen = $this->tempUrlGenerator->generate(
|
||||
'GET',
|
||||
$template->getFile()->getFilename()
|
||||
);
|
||||
if ($isTest && null !== $contextGenerationData['test_file']) {
|
||||
/** @var File $file */
|
||||
$file = $contextGenerationData['test_file'];
|
||||
$templateResource = fopen($file->getPathname(), 'rb');
|
||||
} else {
|
||||
$getUrlGen = $this->tempUrlGenerator->generate(
|
||||
'GET',
|
||||
$template->getFile()->getFilename()
|
||||
);
|
||||
|
||||
$data = $this->client->request('GET', $getUrlGen->url);
|
||||
$data = $this->client->request('GET', $getUrlGen->url);
|
||||
|
||||
$iv = $template->getFile()->getIv(); // iv as an Array
|
||||
$ivGoodFormat = pack('C*', ...$iv); // iv as a String (ok for openssl_decrypt)
|
||||
$iv = $template->getFile()->getIv(); // iv as an Array
|
||||
$ivGoodFormat = pack('C*', ...$iv); // iv as a String (ok for openssl_decrypt)
|
||||
|
||||
$method = 'AES-256-CBC';
|
||||
$method = 'AES-256-CBC';
|
||||
|
||||
$key = $template->getFile()->getKeyInfos()['k'];
|
||||
$keyGoodFormat = Base64Url::decode($key);
|
||||
$key = $template->getFile()->getKeyInfos()['k'];
|
||||
$keyGoodFormat = Base64Url::decode($key);
|
||||
|
||||
$dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat);
|
||||
$dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat);
|
||||
|
||||
if (false === $dataDecrypted) {
|
||||
throw new Exception('Error during Decrypt ', 1);
|
||||
if (false === $dataDecrypted) {
|
||||
throw new Exception('Error during Decrypt ', 1);
|
||||
}
|
||||
|
||||
if (false === $templateResource = fopen('php://memory', 'r+b')) {
|
||||
$this->logger->error('Could not write data to memory');
|
||||
|
||||
throw new HttpException(500);
|
||||
}
|
||||
fwrite($templateResource, $dataDecrypted);
|
||||
rewind($templateResource);
|
||||
}
|
||||
|
||||
if (false === $templateResource = fopen('php://memory', 'r+b')) {
|
||||
$this->logger->error('Could not write data to memory');
|
||||
|
||||
throw new HttpException(500);
|
||||
}
|
||||
|
||||
fwrite($templateResource, $dataDecrypted);
|
||||
rewind($templateResource);
|
||||
|
||||
$datas = $context->getData($template, $entity, $contextGenerationData);
|
||||
dump('datas compiled', $datas);
|
||||
|
||||
@ -158,6 +280,24 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
|
||||
fclose($templateResource);
|
||||
|
||||
if ($isTest) {
|
||||
return new StreamedResponse(
|
||||
static function () use ($generatedResource) {
|
||||
fpassthru($generatedResource);
|
||||
fclose($generatedResource);
|
||||
|
||||
exit();
|
||||
},
|
||||
Response::HTTP_OK,
|
||||
[
|
||||
'Content-Transfer-Encoding', 'binary',
|
||||
'Content-Type' => 'application/vnd.oasis.opendocument.text',
|
||||
'Content-Disposition' => sprintf('attachment; filename="%s.odt"', 'generated'),
|
||||
'Content-Length' => fstat($generatedResource)['size'],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
$genDocName = 'doc_' . sprintf('%010d', mt_rand()) . 'odt';
|
||||
|
||||
$getUrlGen = $this->tempUrlGenerator->generate(
|
||||
@ -206,28 +346,4 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
|
||||
throw new Exception('Unable to generate document.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "/api/1.0/docgen/templates/by-entity/{entityClassName}",
|
||||
* name="chill_docgenerator_templates_for_entity_api"
|
||||
* )
|
||||
*/
|
||||
public function listTemplateApiAction(string $entityClassName): Response
|
||||
{
|
||||
$nb = $this->docGeneratorTemplateRepository->countByEntity($entityClassName);
|
||||
$paginator = $this->paginatorFactory->create($nb);
|
||||
$entities = $this->docGeneratorTemplateRepository->findByEntity(
|
||||
$entityClassName,
|
||||
$paginator->getCurrentPageFirstItemNumber(),
|
||||
$paginator->getItemsPerPage()
|
||||
);
|
||||
|
||||
return $this->json(
|
||||
new Collection($entities, $paginator),
|
||||
Response::HTTP_OK,
|
||||
[],
|
||||
[AbstractNormalizer::GROUPS => ['read']]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
<th></th>
|
||||
<th>{{ 'Title'|trans }}</th>
|
||||
<th>{{ 'docgen.Context'|trans }}</th>
|
||||
<th>{{ 'docgen.test generate'|trans }}</th>
|
||||
<th>{{ 'Edit'|trans }}</th>
|
||||
{% endblock %}
|
||||
|
||||
@ -15,6 +16,20 @@
|
||||
<td>{{ entity.id }}</td>
|
||||
<td>{{ entity.name|localize_translatable_string}}</td>
|
||||
<td>{{ contextManager.getContextByKey(entity.context).name|trans }}</td>
|
||||
<td>
|
||||
<form method="get" action="{{ path('chill_docgenerator_test_generate_redirect') }}">
|
||||
<input type="hidden" name="returnPath" value="{{ app.request.query.get('returnPath', '/')|e('html_attr') }}" />
|
||||
<input type="hidden" name="template" value="{{ entity.id|e('html_attr') }}" />
|
||||
<input type="hidden" name="entityClassName" value="{{ contextManager.getContextByKey(entity.context).entityClass|e('html_attr') }}" />
|
||||
<input type="text" name="entityId" />
|
||||
|
||||
<ul class="record_actions">
|
||||
<li>
|
||||
<button type="submit" class="btn btn-mini btn-neutral">{{ 'docgen.test generate'|trans }}</button>
|
||||
</li>
|
||||
</ul>
|
||||
</form>
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ chill_path_add_return_path('chill_crud_docgen_template_edit', { 'id': entity.id }) }}" class="btn btn-edit">
|
||||
{{ 'Edit'|trans }}
|
||||
|
Loading…
x
Reference in New Issue
Block a user