Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand Down
1 change: 1 addition & 0 deletions config/services/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
21 changes: 18 additions & 3 deletions src/Controller/Admin/ProcessExecutionCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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')
Expand All @@ -88,6 +96,7 @@ public function configureActions(Actions $actions): Actions
]
)
->linkToCrudAction('downloadLogFile')
->displayIf(fn (ProcessExecution $entity) => file_exists($this->getLogFilePath($entity)))
);
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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
;
}
}
13 changes: 13 additions & 0 deletions src/Repository/ProcessExecutionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}