benmacha/audit-bundle
Symfony bundle to audit Doctrine entity changes with rollback support. Includes a web UI and REST API to browse audit logs, flexible configuration, security integration, and optional async processing. Supports PHP 7.4–8.4 and Symfony 5.4–7.x.
Installation:
composer require benmacha/audit-bundle
Symfony Flex auto-configures the bundle.
Enable Auditing:
Add #[Auditable] to your entity:
use BenMacha\AuditBundle\Attribute\Auditable;
#[ORM\Entity]
#[Auditable] // Tracks all operations by default
class User { ... }
First Use Case:
Access the web UI at /admin/audit (default route) or use the API at /api/audit/logs.
#[Auditable] on entities to enable automatic tracking.#[Auditable(
operations: ['create', 'update'], // Exclude 'delete'
ignoredFields: ['password', 'apiToken']
)]
class Product { ... }
#[IgnoreAudit] or #[AuditSensitive] for granular control.class User {
#[IgnoreAudit] private string $password;
#[AuditSensitive(mask: true)] private string $ssn;
}
#[AuditContext] or #[AuditMetadata].#[AuditContext(reason: 'Admin update', category: 'user_management')]
class User { ... }
use BenMacha\AuditBundle\Event\AuditEvents;
class AuditSubscriber {
public static function getSubscribedEvents(): array {
return [AuditEvents::POST_AUDIT => 'onPostAudit'];
}
public function onPostAudit(AuditEvent $event) {
$this->notifyAdmin($event->getAuditLog());
}
}
# Fetch logs for a specific entity
curl -X GET "/api/audit/logs/entity/App\Entity\User/1"
public function revertOrder(Order $order, int $auditLogId) {
$this->auditService->rollbackEntity($auditLogId);
$this->notifyCustomer($order);
}
Performance Overhead:
async_processing: true in audit.yaml for large-scale apps.batch_size to avoid locking issues.Sensitive Data Exposure:
#[AuditSensitive] for PII (e.g., passwords, SSNs).mask: true.Event Ordering:
PRE_AUDIT events can modify logs before saving, but avoid infinite loops.Database Bloat:
retention_days in config to auto-purge old logs.php bin/console audit:cleanup periodically.Security Misconfigurations:
manage_role and rollback_role are restricted to admins.AUDIT_ENABLED=false in production .env if auditing is unnecessary.AUDIT_LOG_LEVEL=debug in .env for verbose logs.AuditEvent objects in subscribers:
public function onPostAudit(AuditEvent $event) {
\dump($event->getAuditLog()->toArray());
}
/api/audit/rollback/{id}.Custom Storage:
Override the default Doctrine storage by implementing AuditStorageInterface.
UI Customization:
Extend Twig templates in templates/bundles/AuditBundle/.
Rollback Validation:
Subclass AuditRollbackService to add pre-rollback checks.
Async Processing:
Configure messenger transport in audit.yaml for background jobs.
auto_discover: true in audit.yaml.ignoredFields.route_prefix if /admin/audit clashes with other routes.audit:
entities:
App\Entity\LogEntry:
enabled: false
indexed: true in #[AuditMetadata] for frequently searched fields.php bin/console audit:rollback --dry-run
How can I help you explore Laravel packages today?