Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search audit entries #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
113 changes: 44 additions & 69 deletions src/Controller/ViewerController.php
Original file line number Diff line number Diff line change
@@ -1,106 +1,81 @@
<?php

declare(strict_types=1);

namespace DH\AuditorBundle\Controller;

use DH\Auditor\Exception\AccessDeniedException;
use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Security;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\DateRangeFilter;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\SimpleFilter;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Query;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
use DH\Auditor\Provider\Doctrine\Persistence\Schema\SchemaManager;
use DH\Auditor\Provider\Doctrine\Service\AuditingService;
use DH\AuditorBundle\Form\FilterForm;
use DH\AuditorBundle\Helper\UrlHelper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;

/**
* @see \DH\AuditorBundle\Tests\Controller\ViewerControllerTest
* Overrides DH\AuditorBundle\Controller\ViewerController
*/
class ViewerController extends AbstractController
{
private Environment $environment;

public function __construct(Environment $environment)
{
$this->environment = $environment;
}

#[Route(path: '/audit', name: 'dh_auditor_list_audits', methods: ['GET'])]
public function listAuditsAction(Reader $reader): Response
{
$schemaManager = new SchemaManager($reader->getProvider());

/** @var AuditingService[] $auditingServices */
$auditingServices = $reader->getProvider()->getAuditingServices();
$audited = [];
$scope = Security::VIEW_SCOPE;
foreach ($auditingServices as $auditingService) {
$audited = array_merge(
$audited,
array_filter(
$schemaManager->getAuditableTableNames($auditingService->getEntityManager()),
static function ($entity) use ($reader, $scope) {
$roleChecker = $reader->getProvider()->getAuditor()->getConfiguration()->getRoleChecker();

return null === $roleChecker ? true : $roleChecker($entity, $scope);
},
ARRAY_FILTER_USE_KEY
)
);
}

return $this->render('@DHAuditor/Audit/audits.html.twig', [
'audited' => $audited,
'reader' => $reader,
]);
}

#[Route(path: '/audit/transaction/{hash}', name: 'dh_auditor_show_transaction', methods: ['GET'])]
public function showTransactionAction(Reader $reader, string $hash): Response
{
$audits = $reader->getAuditsByTransactionHash($hash);

return $this->render('@DHAuditor/Audit/transaction.html.twig', [
'hash' => $hash,
'audits' => $audits,
]);
}

#[Route(path: '/audit/{entity}/{id}', name: 'dh_auditor_show_entity_history', methods: ['GET'])]
public function showEntityHistoryAction(Request $request, Reader $reader, string $entity, int|string $id = null): Response
/**
* @Route(path="/audit/{entity}/{id}", name="dh_auditor_show_entity_history_2", methods={"GET", "POST"})
*
* @param int|string $id
*/
public function showEntityHistoryAction(Request $request, Reader $reader, string $entity, $id = null): Response
{
\assert(\is_string($request->query->get('page', '1')) || \is_int($request->query->get('page', '1')));
$page = (int) $request->query->get('page', '1');
$page = $page < 1 ? 1 : $page;

$entity = UrlHelper::paramToNamespace($entity);

if (!$reader->getProvider()->isAuditable($entity)) {
throw $this->createNotFoundException();
}

$supportedFilters = Query::getSupportedFilters();
$form = $this->createForm(FilterForm::class);

$form->handleRequest($request);

try {
$pager = $reader->paginate($reader->createQuery($entity, [
$page = (int) $form->get('page')->getData();
$page = $page < 1 ? 1 : $page;
$query = $reader->createQuery($entity, [
'object_id' => $id,
'page' => $page,
'page_size' => Reader::PAGE_SIZE,
]), $page, Reader::PAGE_SIZE);
} catch (AccessDeniedException) {
]);

if ($form->isSubmitted() && $form->isValid()) {
$min = $form->get('created_at_start')->getData();
$max = $form->get('created_at_end')->getData();

if ($min || $max) {
$query->addFilter(new DateRangeFilter('created_at', $min, $max));
}
foreach ($form->all() as $field) {
$data = $field->getData();
if (!$data || \in_array($field->getName(), ['page', 'created_at_start', 'created_at_end'])) {
continue;
}
dump($field->getName());
dump($data);
$query->addFilter(new SimpleFilter($field->getName(), $data));
}
}

$pager = $reader->paginate($query, $page, Reader::PAGE_SIZE);
} catch (AccessDeniedException $e) {
throw $this->createAccessDeniedException();
}

return $this->render('@DHAuditor/Audit/entity_history.html.twig', [
'id' => $id,
'entity' => $entity,
'paginator' => $pager,
'supportedFilters' => $supportedFilters,
'form' => $form->createView(),
'page' => $form->get('page')->getData(),
]);
}

protected function renderView(string $view, array $parameters = []): string
{
return $this->environment->render($view, $parameters);
}
}
75 changes: 75 additions & 0 deletions src/Form/FilterForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace DH\AuditorBundle\Form;

use DH\Auditor\Provider\Doctrine\Model\Transaction;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Query;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class FilterForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$supportedFilters = Query::getSupportedFilters();
$classes = 'border rounded py-1 px-3 text-gray-700 mr-4 text-sm';

foreach ($supportedFilters as $supportedFilter) {
if ($supportedFilter === 'created_at') {
$builder->add('created_at_start', DateTimeType::class, [
'mapped' => false,
'block_prefix' => 'audit_filter',
'required' => false,
'label' => 'From',
'attr' => [
'class' => $classes,
],
]);
$builder->add('created_at_end', DateTimeType::class, [
'mapped' => false,
'block_prefix' => 'audit_filter',
'required' => false,
'label' => 'To',
'attr' => [
'class' => $classes,
],
]);
} elseif ($supportedFilter === 'type') {
$builder->add($supportedFilter, ChoiceType::class, [
'mapped' => false,
'required' => false,
'block_prefix' => 'audit_filter',
'choices' => [
Transaction::INSERT => Transaction::INSERT,
Transaction::UPDATE => Transaction::UPDATE,
Transaction::REMOVE => Transaction::REMOVE,
Transaction::ASSOCIATE => Transaction::ASSOCIATE,
Transaction::DISSOCIATE => Transaction::DISSOCIATE,
],
'attr' => [
'class' => $classes,
],
]);
} else {
$builder->add($supportedFilter, TextType::class, [
'mapped' => false,
'required' => false,
'block_prefix' => 'audit_filter',
'attr' => [
'class' => $classes,
],
]);
}
}
$builder->add('page', HiddenType::class, [
'mapped' => false,
'required' => false,
'label_render' => false,
'block_prefix' => 'audit_filter',
]);
}
}
Loading