Skip to content

Commit 13b9e9e

Browse files
authored
Merge pull request #12 from cleverage/11_restrict_download_show_logs
#11 Restrict "Download log file" and "Show logs stored in database" buttons visibility
2 parents 1f80e86 + 1918138 commit 13b9e9e

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-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: 18 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,12 @@
3334
#[IsGranted('ROLE_USER')]
3435
class ProcessExecutionCrudController extends AbstractCrudController
3536
{
37+
public function __construct(
38+
private readonly ProcessExecutionRepository $processExecutionRepository,
39+
private readonly string $logDirectory,
40+
) {
41+
}
42+
3643
public static function getEntityFqcn(): string
3744
{
3845
return ProcessExecution::class;
@@ -77,6 +84,7 @@ public function configureActions(Actions $actions): Actions
7784
]
7885
)
7986
->linkToCrudAction('showLogs')
87+
->displayIf(fn (ProcessExecution $entity) => $this->processExecutionRepository->hasLogs($entity))
8088
)->add(
8189
Crud::PAGE_INDEX,
8290
Action::new('downloadLogfile', false, 'fas fa-download')
@@ -88,6 +96,7 @@ public function configureActions(Actions $actions): Actions
8896
]
8997
)
9098
->linkToCrudAction('downloadLogFile')
99+
->displayIf(fn (ProcessExecution $entity) => file_exists($this->getLogFilePath($entity)))
91100
);
92101
}
93102

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

116125
public function downloadLogFile(
117126
AdminContext $context,
118-
string $logDirectory,
119127
): Response {
120128
/** @var ProcessExecution $processExecution */
121129
$processExecution = $context->getEntity()->getInstance();
122-
$filepath = $logDirectory.\DIRECTORY_SEPARATOR.$processExecution->code.\DIRECTORY_SEPARATOR
123-
.$processExecution->logFilename;
130+
$filepath = $this->getLogFilePath($processExecution);
124131
$basename = basename($filepath);
125132
$content = file_get_contents($filepath);
126133
if (false === $content) {
@@ -137,4 +144,12 @@ public function configureFilters(Filters $filters): Filters
137144
{
138145
return $filters->add('code')->add('startDate');
139146
}
147+
148+
private function getLogFilePath(ProcessExecution $processExecution): string
149+
{
150+
return $this->logDirectory.
151+
\DIRECTORY_SEPARATOR.$processExecution->code.
152+
\DIRECTORY_SEPARATOR.$processExecution->logFilename
153+
;
154+
}
140155
}

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)