PHP code example of th3mouk / audit-trail-bundle

1. Go to this page and download the library: Download th3mouk/audit-trail-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

th3mouk / audit-trail-bundle example snippets


// config/bundles.php
return [
    // ...
    Th3Mouk\AuditTrail\AuditTrailBundle::class => ['all' => true],
];

use Th3Mouk\AuditTrail\Attribute\AuditLabel;
use Th3Mouk\AuditTrail\Attribute\AuditScope;
use Th3Mouk\AuditTrail\Attribute\Auditable;

#[ORM\Entity]
#[Auditable(type: 'membership')]
#[AuditScope(root: Organization::class, via: 'organization')]
class OrganizationMembership
{
    // Snapshotted onto every entry that names this row, so the trail stays readable
    // once the row is gone. A property is the safe form: a label *method* runs inside
    // the flush, where reading an unloaded association would cost a query.
    #[AuditLabel]
    private string $summary;

    // ...
}

use Th3Mouk\AuditTrail\Repository\AuditCursor;
use Th3Mouk\AuditTrail\Repository\AuditLogRepository;

$history = $auditLogs
    ->forRoot('organization', (string) $organization->getId(), $before)   // ?AuditCursor
    ->setMaxResults(50)
    ->getQuery()
    ->getResult();

$next = [] === $history ? null : AuditCursor::of(end($history));

use Th3Mouk\AuditTrail\AuditLoggerInterface;
use Th3Mouk\AuditTrail\Model\AuditScopeRef;

$this->audit->deleted(
    OrganizationMembership::class,
    4217,
    ['role' => 'Manager', 'user' => 'Jean Dupont'],
    'Manager — Jean Dupont',
    AuditScopeRef::of('organization', 31, 'Acme'),
    ['origin' => 'bulk-revoke'],
);
$this->entityManager->flush();

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Th3Mouk\AuditTrail\Actor\ActorResolverInterface;
use Th3Mouk\AuditTrail\Model\Actor;

#[AutoconfigureTag('audit_trail.actor_resolver', ['priority' => 100])]
final readonly class ApiKeyActorResolver implements ActorResolverInterface
{
    public function __construct(private RequestStack $requests)
    {
    }

    public function resolve(): ?Actor
    {
        $key = $this->requests->getCurrentRequest()?->headers->get('X-Api-Key');

        return null === $key ? null : Actor::of($key, 'api_key', 'Integration key');
    }
}

// config/packages/audit_trail.php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $container): void {
    $container->extension('audit_trail', [
        'bridges' => ['api_platform' => [
            'enabled' => true,
            'access' => [
                // Any one grant is enough. A mapping adds a subject, for permissions anchored on a
                // resource: ['attribute' => 'view', 'subject' => 'audit-logs']. Or use 'expression'
                // for a raw one, or 'public' => true to opt out loudly.
                'grants' => ['show audit'],
            ],
        ]],
    ]);
};