Laravel Auditing
website·Indexed 33 days ago
https://laravel-auditing.com/A robust auditing package for Laravel that allows developers to maintain control over application data by tracking changes to Eloquent models. It features customizable audit drivers, attribute modifiers, audit resolvers, and support for custom audit models and events.
What's inside Laravel Auditing
- Laravel Auditing is a package designed to provide detailed auditing capabilities for Eloquent models. It allows developers to track changes made to records and their attributes, helping detect discrepancies, anomalies, or suspect activities within an application. The package is implemented by adding a trait to the models that need to be tracked, enabling the retrieval and display of audited data and metadata.
Overview of Laravel Auditing for Eloquent models
Laravel Auditing is a tool for Laravel developers to track every change occurring in Eloquent models, including the creation, modification, and deletion of records, as well as attribute changes. It provides a robust way to ensure data security and maintain a detailed audit trail of application data.Overview of Laravel Auditing Drivers
Audit drivers contain the logic for handling audits ofAuditablemodels. They are responsible for storing model attribute changes and handling the pruning of old audits when an audit threshold is configured. The package includes aDatabasedriver by default.Retrieve audit metadata
Audit metadata contains information about the audit event, such as the user, IP address, URL, and timestamps. You can retrieve this data as an array usinggetMetadata(). Passingtrueas the first argument returns a JSON string, and the second argument allows forjson_encode()bitmask options (e.g.,JSON_PRETTY_PRINT). Alternatively, metadata attributes can be accessed directly as properties on theAuditmodel.// Get as array var_dump($audit->getMetadata()); // Get as JSON echo $audit->getMetadata(true, JSON_PRETTY_PRINT); // Direct property access echo $audit->id; echo $audit->event; echo $audit->url; echo $audit->ip_address; echo $audit->user_agent; echo $audit->tags; echo $audit->created_at->toDateTimeString(); echo $audit->user_id;Setup the Audits database table
Publish the migration for theauditstable to thedatabase/directory and then run the migration command to create the table where audit records will be stored.php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations" php artisan migrateCreate and register a custom Audit Resolver
Audit resolvers are classes used to add metadata to specific columns in the audit table. To create a custom resolver, implement the
OwenIt\Auditing\Contracts\Resolverinterface and define a public staticresolve()method. You can generate a resolver using the CLI commandphp artisan auditing:audit-resolver.The
resolve()method receives the auditable Eloquent model as an argument, allowing you to extract data from the model itself or the environment. To register the resolver, add it to theresolversarray inconfig/audit.php, where the array key corresponds to the target column name in the database.// 1. Create the Resolver class namespace App use OwenIt Auditing Contracts Auditable; use OwenIt Auditing Contracts Resolver; class TenantIdResolver implements Resolver { public static function resolve(Auditable $auditable = null) { if(method_exists($auditable, 'tenant')) { return $auditable->tenant_id; } return null; } } // 2. Register in config/audit.php 'resolvers' => [ 'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class, 'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class, 'url' => OwenIt\Auditing\Resolvers\UrlResolver::class, 'tenant_id' => App\AuditResolvers\TenantIdResolver::class, ],Cancel an audit using the Auditing event
TheOwenIt\Auditing\Events\Auditingevent is fired while the auditing process is being handled. You can cancel the creation of an audit record by returningfalsefrom thehandle()method of the event listener.namespace App\Listeners; use OwenIt\Auditing\Events\Auditing; class AuditingListener { public function handle(Auditing $event) { // Return false to cancel the audit if ($someCondition) { return false; } } }Configure Audit migrations to use UUIDs instead of auto-incrementing IDs
If your application uses UUIDs, you must manually define theuserandauditablecolumns in the migration instead of using themorphs()helper to ensure the ID columns are typed as UUIDs. Ensure these types match the primary keys of the referenced tables.// For the User $table->string('user_type')->nullable(); $table->uuid('user_id')->nullable(); $table->index([ 'user_type', 'user_id', ]); // For the Auditable model $table->string('auditable_type'); $table->uuid('auditable_id'); $table->index([ 'auditable_type', 'auditable_id', ]);Manually trigger and prune audit records using the Auditor
The
Auditorclass is used to manually create and clearAuditrecords. While theAuditableObservertypically handles this automatically, you can manually invoke auditing using theexecute()method and remove old records using theprune()method.Warning: If you manually call the Auditor while the model's
$auditEventsproperty is still configured (not set to an empty array), you will create duplicateAuditrecords because the observer will also trigger.use OwenIt�Auditing�Facades�Auditor; // Manually execute an audit and then prune if ($audit = Auditor::execute($article)) { Auditor::prune($article); }Dispatch a custom audit event for an Auditable model
To log custom values that do not originate from standard Eloquent events ('created', 'updated', 'deleted', 'restored', 'retrieved'), you can manually dispatch the\OwenIt\Auditing\Events\AuditCustomevent. You must set theauditEvent,isCustomEvent,auditCustomOld, andauditCustomNewproperties on the Auditable model before dispatching the event.use OwenIt\Auditing\Events\AuditCustom; use Illuminate\Support\Facades\Event; $article = Article::find(1); $article->auditEvent = 'whateverYouWant'; $article->isCustomEvent = true; $article->auditCustomOld = [ 'customExample' => 'Anakin Skywalker' ]; $article->auditCustomNew = [ 'customExample' => 'Darth Vader' ]; Event::dispatch(new AuditCustom($article));Implement a custom Audit model
Starting from version 4.1.0, you can use a custom Audit model instead of the defaultIlluminate\Database\Eloquent\Model. Any custom Audit model must implement theOwenIt\Auditing\Contracts\Auditinterface. Most of the core auditing logic is provided by theOwenIt\Auditing\Audittrait, which should be used within the model.Upgrade Laravel Auditing from version 12.x to 13
Version 13 introduces breaking changes to the configuration file and resolvers.
- Config Key Change: The
resolverkey has been replaced byresolvers(plural). - User Resolver: The user resolver has moved from the top-level config to the
userconfig key. - Resolver Contract: Custom implementations of
UrlResolver,UserAgentResolver, orIpAddressResolvermust now implement theOwenIt\Auditing\Contracts\Resolvercontract.
// Custom Resolver Implementation class UrlResolver implements \OwenIt\Auditing\Contracts\Resolver {} // config/audit.php return [ 'resolvers' => [ 'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class, 'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class, 'url' => OwenIt\Auditing\Resolvers\UrlResolver::class, ], 'user' => [ 'morph_prefix' => 'user', 'guards' => ['web', 'api'], 'resolver' => OwenIt\Auditing\Resolvers\UserResolver::class ], ];- Config Key Change: The