Laravel Auditing

repository·master·Indexed 25 days ago

https://github.com/owen-it/laravel-auditing

A Laravel package for tracking changes to Eloquent models using a trait to maintain a history of modifications for security and business auditing. It supports various audit event types (retrieved, created, updated, deleted, restored), queued auditing for performance, and customizable audit drivers and resolvers. The package includes an Auditor facade and an Audit model to manage and access audit records, including old and new values, associated users, and timestamps.

Tokens
1.6K
Snippets
0
Records
16
Agent score
85%

What's inside laravel-auditing

  1. Overview of Laravel Auditing

    master
    Laravel Auditing is a package designed to help you track and understand changes in your Eloquent models. It provides a history of model changes by using a trait, allowing you to identify discrepancies, anomalies, or suspect activities within your application data. Once auditing is enabled, retrieving the history of changes is straightforward for display or analysis.
  2. Publish the auditing configuration and migrations

    master

    To customize the package behavior or set up the database schema, you can publish the configuration file and the migration stub using the Laravel vendor:publish command.

    Available tags:

    • config: Publishes the config/audit.php file.
    • migrations: Publishes the audits.stub migration file to your database/migrations directory.
  3. Install Laravel Auditing via artisan command

    master

    Run the auditing:install command to automate the initial setup of the package. This command performs the following actions:

    1. Publishes the Auditing configuration file.
    2. Publishes the Auditing database migrations.
    3. Registers the OwenIt\Auditing\AuditingServiceProvider::class in your application's config/app.php file.
  4. Configure queued auditing via audit.queue.enable

    master

    The auditing process can be offloaded to a queue to improve performance. This is controlled by the audit.queue.enable configuration key.

    • If audit.queue.enable is false (default), audits are executed synchronously using Auditor::execute($model).
    • If audit.queue.enable is true, the observer will dispatch a DispatchAudit event, allowing the audit to be handled asynchronously.
  5. Configure empty value auditing behavior

    master

    The Auditor class respects two configuration keys to determine if audits should be recorded when no changes are detected:

    • audit.empty_values: A boolean determining if empty changes should be stored.
    • audit.allowed_empty_values: An array of event names (e.g., from getAuditEvent()) that are explicitly allowed to store empty values even if audit.empty_values is false.

    If both conditions are not met and both new_values and old_values are empty, the audit process will exit without creating a record.

  6. Check Laravel Auditing version compatibility

    master

    Before installing, ensure your environment meets the requirements for the version you intend to use. The current active support version is 14.x.

    VersionIlluminateStatusPHP Version
    14.x11.x.x - 13.x.xActive support>= 8.2
    13.x7.x.x - 11.x.xEnd of life>= 7.3 | 8.0
    12.x6.x.x - 9.x.xEnd of life>= 7.3 | 8.0
    11.x5.8.x - 8.x.xEnd of life>= 7.3
    10.x5.8.x - 7.x.xEnd of life>= 7.2.5
    9.x5.8.x - 6.x.xEnd of life>= 7.1.3
    8.x5.2.x - 5.7.xEnd of life>= 7.0.13
    7.x5.2.x - 5.6.xEnd of life>= 7.0.13
    6.x5.2.x - 5.6.xEnd of life>= 7.0.13
    5.x5.2.x - 5.5.xEnd of life>= 7.0.13
    4.x5.2.x - 5.5.xEnd of life>= 5.5.9
    3.x5.2.x - 5.4.xEnd of life>= 5.5.9
    2.x5.1.x - 5.3.xEnd of life>= 5.5.9
  7. Execute auditing for a model

    master

    The execute method triggers the auditing process for a given Auditable model. It performs the following steps:

    1. Checks if the model is readyForAuditing().
    2. Resolves the appropriate AuditDriver.
    3. Fires the Auditing event (which can be used to cancel the audit).
    4. Evaluates whether to skip auditing based on the audit.empty_values and audit.allowed_empty_values configuration settings if no changes are detected.
    5. Instructs the driver to audit($model).
    6. Calls $driver->prune($model) to clean up old audit data.
    7. Dispatches the Audited event upon successful completion.
  8. Retrieve the audit driver for a model

    master
    Use the auditDriver method to obtain the specific AuditDriver instance assigned to an Auditable model. This is useful when you need to interact directly with the driver responsible for storing or managing that model's audit logs. The driver is determined by the value returned from the model's getAuditDriver() method.
  9. Audit event types for Auditable models

    master

    The AuditableObserver automatically triggers audits based on Eloquent model lifecycle events. When an audit is dispatched, the model's audit event type is set to one of the following values:

    • retrieved
    • created
    • updated
    • deleted
    • restored

    Note: During a model restoration process, the observer tracks the restoring state to prevent duplicate or incorrect updated audits from being generated alongside the restored audit.

  10. Access audit record properties

    master

    The Audit model represents a single audit entry. When interacting with an instance of this model, you can access the following properties:

    • event: The type of event that triggered the audit (e.g., created, updated).
    • tags: A string containing tags associated with the audit.
    • new_values: An array of the values after the change.
    • old_values: An array of the values before the change.
    • auditable_type: The class name of the model being audited.
    • auditable_id: The primary key of the model being audited.
    • user: The user responsible for the change (implements Authenticatable).
    • auditable: The actual model instance that was audited (implements Auditable).
    • created_at / updated_at: Timestamps for the audit record.