Skip to content

Commit 0ab16d5

Browse files
#11 Restrict "showLogs" button when log_record exists. Restrict "downloadLogfile" button when log file exists
1 parent 1f80e86 commit 0ab16d5

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ v2.0
1616

1717
* [#1](https://github.com/cleverage/ui-process-bundle/issues/1) Add Makefile & .docker for local standalone usage
1818
* [#1](https://github.com/cleverage/ui-process-bundle/issues/1) Add rector, phpstan & php-cs-fixer configurations & apply it. Remove phpcs configuration.
19+
* [#11](https://github.com/cleverage/ui-process-bundle/issues/11) Restrict "Download log file" and "Show logs stored in database" buttons visibility
20+
1921

2022
v1.0.6
2123
------

config/services/controller.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ services:
1111
$uploadDirectory: '%upload_directory%'
1212
$context: '@EasyCorp\Bundle\EasyAdminBundle\Factory\AdminContextFactory'
1313
$logDirectory: '%kernel.logs_dir%'
14+
$processExecutionRepository: '@cleverage_ui_process.repository.process_execution'
1415
tags:
1516
- { name: 'controller.service_arguments' }

src/Controller/Admin/ProcessExecutionCrudController.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CleverAge\UiProcessBundle\Admin\Field\EnumField;
1717
use CleverAge\UiProcessBundle\Entity\ProcessExecution;
18+
use CleverAge\UiProcessBundle\Repository\ProcessExecutionRepository;
1819
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
1920
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
2021
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
@@ -33,6 +34,14 @@
3334
#[IsGranted('ROLE_USER')]
3435
class ProcessExecutionCrudController extends AbstractCrudController
3536
{
37+
38+
public function __construct(
39+
private readonly ProcessExecutionRepository $processExecutionRepository,
40+
private readonly string $logDirectory,
41+
) {
42+
43+
}
44+
3645
public static function getEntityFqcn(): string
3746
{
3847
return ProcessExecution::class;
@@ -65,6 +74,8 @@ public function configureCrud(Crud $crud): Crud
6574

6675
public function configureActions(Actions $actions): Actions
6776
{
77+
$processExecutionRepository = $this->processExecutionRepository;
78+
6879
return Actions::new()
6980
->add(
7081
Crud::PAGE_INDEX,
@@ -77,6 +88,9 @@ public function configureActions(Actions $actions): Actions
7788
]
7889
)
7990
->linkToCrudAction('showLogs')
91+
->displayIf(function (ProcessExecution $entity) {
92+
return $this->processExecutionRepository->hasLogs($entity);
93+
})
8094
)->add(
8195
Crud::PAGE_INDEX,
8296
Action::new('downloadLogfile', false, 'fas fa-download')
@@ -88,6 +102,9 @@ public function configureActions(Actions $actions): Actions
88102
]
89103
)
90104
->linkToCrudAction('downloadLogFile')
105+
->displayIf(function (ProcessExecution $entity) {
106+
return file_exists($this->getLogFilePath($entity));
107+
})
91108
);
92109
}
93110

@@ -115,12 +132,10 @@ public function showLogs(AdminContext $adminContext): RedirectResponse
115132

116133
public function downloadLogFile(
117134
AdminContext $context,
118-
string $logDirectory,
119135
): Response {
120136
/** @var ProcessExecution $processExecution */
121137
$processExecution = $context->getEntity()->getInstance();
122-
$filepath = $logDirectory.\DIRECTORY_SEPARATOR.$processExecution->code.\DIRECTORY_SEPARATOR
123-
.$processExecution->logFilename;
138+
$filepath = $this->getLogFilePath($processExecution);
124139
$basename = basename($filepath);
125140
$content = file_get_contents($filepath);
126141
if (false === $content) {
@@ -137,4 +152,12 @@ public function configureFilters(Filters $filters): Filters
137152
{
138153
return $filters->add('code')->add('startDate');
139154
}
155+
156+
private function getLogFilePath(ProcessExecution $processExecution): string
157+
{
158+
return $this->logDirectory.
159+
\DIRECTORY_SEPARATOR.$processExecution->code.
160+
\DIRECTORY_SEPARATOR.$processExecution->logFilename
161+
;
162+
}
140163
}

src/Repository/ProcessExecutionRepository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CleverAge\UiProcessBundle\Repository;
1515

16+
use CleverAge\UiProcessBundle\Entity\LogRecord;
1617
use CleverAge\UiProcessBundle\Entity\ProcessExecution;
1718
use Doctrine\ORM\EntityManagerInterface;
1819
use Doctrine\ORM\EntityRepository;
@@ -49,4 +50,16 @@ public function getLastProcessExecution(string $code): ?ProcessExecution
4950
->getQuery()
5051
->getOneOrNullResult();
5152
}
53+
54+
public function hasLogs(ProcessExecution $processExecution): bool
55+
{
56+
$qb = $this->createQueryBuilder('pe')
57+
->select('count(lr.id)')
58+
->join(LogRecord::class, 'lr', 'WITH', 'lr.processExecution = pe')
59+
->where('pe.id = :id')
60+
->setParameter('id', $processExecution->getId()
61+
);
62+
63+
return (int) $qb->getQuery()->getSingleScalarResult() > 0;
64+
}
5265
}

0 commit comments

Comments
 (0)