diff --git a/CHANGELOG.md b/CHANGELOG.md index 52cbc66..bb04c2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ v2.0 * [#1](https://github.com/cleverage/ui-process-bundle/issues/1) Add Makefile & .docker for local standalone usage * [#1](https://github.com/cleverage/ui-process-bundle/issues/1) Add rector, phpstan & php-cs-fixer configurations & apply it. Remove phpcs configuration. +* [#11](https://github.com/cleverage/ui-process-bundle/issues/11) Restrict "Download log file" and "Show logs stored in database" buttons visibility + v1.0.6 ------ diff --git a/config/services/controller.yaml b/config/services/controller.yaml index 4d7f18f..4cec79e 100644 --- a/config/services/controller.yaml +++ b/config/services/controller.yaml @@ -11,5 +11,6 @@ services: $uploadDirectory: '%upload_directory%' $context: '@EasyCorp\Bundle\EasyAdminBundle\Factory\AdminContextFactory' $logDirectory: '%kernel.logs_dir%' + $processExecutionRepository: '@cleverage_ui_process.repository.process_execution' tags: - { name: 'controller.service_arguments' } diff --git a/src/Controller/Admin/ProcessExecutionCrudController.php b/src/Controller/Admin/ProcessExecutionCrudController.php index 7033d2b..f0bd2ca 100644 --- a/src/Controller/Admin/ProcessExecutionCrudController.php +++ b/src/Controller/Admin/ProcessExecutionCrudController.php @@ -15,6 +15,7 @@ use CleverAge\UiProcessBundle\Admin\Field\EnumField; use CleverAge\UiProcessBundle\Entity\ProcessExecution; +use CleverAge\UiProcessBundle\Repository\ProcessExecutionRepository; use EasyCorp\Bundle\EasyAdminBundle\Config\Action; use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; @@ -33,6 +34,12 @@ #[IsGranted('ROLE_USER')] class ProcessExecutionCrudController extends AbstractCrudController { + public function __construct( + private readonly ProcessExecutionRepository $processExecutionRepository, + private readonly string $logDirectory, + ) { + } + public static function getEntityFqcn(): string { return ProcessExecution::class; @@ -77,6 +84,7 @@ public function configureActions(Actions $actions): Actions ] ) ->linkToCrudAction('showLogs') + ->displayIf(fn (ProcessExecution $entity) => $this->processExecutionRepository->hasLogs($entity)) )->add( Crud::PAGE_INDEX, Action::new('downloadLogfile', false, 'fas fa-download') @@ -88,6 +96,7 @@ public function configureActions(Actions $actions): Actions ] ) ->linkToCrudAction('downloadLogFile') + ->displayIf(fn (ProcessExecution $entity) => file_exists($this->getLogFilePath($entity))) ); } @@ -115,12 +124,10 @@ public function showLogs(AdminContext $adminContext): RedirectResponse public function downloadLogFile( AdminContext $context, - string $logDirectory, ): Response { /** @var ProcessExecution $processExecution */ $processExecution = $context->getEntity()->getInstance(); - $filepath = $logDirectory.\DIRECTORY_SEPARATOR.$processExecution->code.\DIRECTORY_SEPARATOR - .$processExecution->logFilename; + $filepath = $this->getLogFilePath($processExecution); $basename = basename($filepath); $content = file_get_contents($filepath); if (false === $content) { @@ -137,4 +144,12 @@ public function configureFilters(Filters $filters): Filters { return $filters->add('code')->add('startDate'); } + + private function getLogFilePath(ProcessExecution $processExecution): string + { + return $this->logDirectory. + \DIRECTORY_SEPARATOR.$processExecution->code. + \DIRECTORY_SEPARATOR.$processExecution->logFilename + ; + } } diff --git a/src/Repository/ProcessExecutionRepository.php b/src/Repository/ProcessExecutionRepository.php index 6034717..db0087d 100644 --- a/src/Repository/ProcessExecutionRepository.php +++ b/src/Repository/ProcessExecutionRepository.php @@ -13,6 +13,7 @@ namespace CleverAge\UiProcessBundle\Repository; +use CleverAge\UiProcessBundle\Entity\LogRecord; use CleverAge\UiProcessBundle\Entity\ProcessExecution; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; @@ -49,4 +50,16 @@ public function getLastProcessExecution(string $code): ?ProcessExecution ->getQuery() ->getOneOrNullResult(); } + + public function hasLogs(ProcessExecution $processExecution): bool + { + $qb = $this->createQueryBuilder('pe') + ->select('count(lr.id)') + ->join(LogRecord::class, 'lr', 'WITH', 'lr.processExecution = pe') + ->where('pe.id = :id') + ->setParameter('id', $processExecution->getId() + ); + + return (int) $qb->getQuery()->getSingleScalarResult() > 0; + } }