mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-14 09:57:43 +00:00
Add audit system core with event handling and subject conversion
- Implemented `AuditEvent` class to represent audit events. - Added `AuditEvent2Trail` for converting events to audit trails. - Introduced `Subject` and `SubjectConverterManager` for subject conversion. - Created contracts like `SubjectConverterInterface` and `AuditEvent2TrailInterface`. - Developed `AuditEventSubscriber` to persist audit events using `AuditTrailPersister`. - Included test classes for core audit services and components.
This commit is contained in:
51
src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php
Normal file
51
src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Audit;
|
||||
|
||||
use Chill\MainBundle\Entity\AuditTrail;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\Translation\TranslatableInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final readonly class AuditEvent2Trail implements AuditEvent2TrailInterface
|
||||
{
|
||||
public function __construct(
|
||||
private TranslatorInterface $translator,
|
||||
private SubjectConverterManagerInterface $subjectConverterManager,
|
||||
private ClockInterface $clock,
|
||||
private Security $security,
|
||||
) {}
|
||||
|
||||
public function convertToTrail(AuditEvent $event): AuditTrail
|
||||
{
|
||||
$description = $event->description instanceof TranslatableInterface ?
|
||||
$event->description->trans($this->translator)
|
||||
: $event->description;
|
||||
|
||||
$targets = array_map(fn (mixed $subject) => $this->subjectConverterManager->convertEntityToSubjects($subject), $event->subjects);
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
return new AuditTrail(
|
||||
Uuid::uuid7(),
|
||||
$event->action,
|
||||
$this->clock->now(),
|
||||
$user instanceof User ? $user : null,
|
||||
$description,
|
||||
$targets,
|
||||
$event->metadata,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user