aliirfaan/laravel-simple-access-log

此包创建一个数据库表以保存访问日志,并暴露事件以记录用户访问。

4.0.1 2023-01-18 08:02 UTC

This package is auto-updated.

Last update: 2024-09-18 11:38:13 UTC


README

许多系统需要记录用户访问以供审计。此包创建一个包含合理字段的数据库表以记录访问。

特性

  • 访问日志表结构
  • 常见访问操作的事件,如登录成功、登录失败、登出
  • 可配置连接,如果使用不同的数据库记录日志
  • 使用自定义模型

表字段

迁移模式说明可用字段。

Schema::connection(config('simple-access-log.access_log_db_connection'))->create('lsac_access_logs', function (Blueprint $table) {
    $table->id();
    $table->dateTime('ac_date_time_local', $precision = 0)->index('ac_date_time_local_index')->comment('Timestamp in local timezone.');
    $table->dateTime('ac_date_time_utc', $precision = 0)->nullable()->index('ac_date_time_utc_index');
    $table->string('al_actor_id')->nullable()->index('al_actor_id_index')->comment('User id in application. Can be null in cases where an action is performed programmatically.');
    $table->string('ac_actor_type', 255)->nullable()->index('ac_actor_type_index')->comment('Actor type in application. Useful if you are logging multiple types of users. Example: admin, user, guest');
    $table->string('al_actor_global_uid')->nullable()->index('al_actor_global_uid_index')->comment('User id if using a single sign on facility.');
    $table->string('ac_actor_username', 255)->nullable()->index('ac_actor_username_index')->comment('Username in application.');
    $table->string('ac_actor_group', 255)->nullable()->index('ac_actor_group_index')->comment('User role/group in application.');
    $table->string('ac_device_id', 255)->nullable()->index('ac_device_id_index')->comment('Device identifier.');
    $table->string('ac_event_name', 255)->index('ac_event_name_index')->comment('Common name for the event that can be used to filter down to similar events. Example: user.login.success, user.login.failure, user.logout');
    $table->ipAddress('ac_ip_addr')->nullable()->index('ac_ip_addr_index');
    $table->string('ac_server', 255)->nullable()->index('ac_server_index')->comment('Server ids or names, server location. Example: uat, production, testing, 192.168.2.10');
    $table->string('ac_version', 255)->nullable()->index('ac_version_index')->comment('Version of the code/release that is sending the events.');
    $table->timestamps();
});

事件

您可以分发这些事件以记录日志。如果您想进行额外处理,也可以监听这些事件。

  • 登录成功
  • 登录失败
  • 已登出

要求

安装

您可以使用Composer在现有的Laravel项目中安装此包

 $ composer require aliirfaan/laravel-simple-access-log

通过编辑 config/app.php 文件并添加到提供者数组中注册ServiceProvider

  aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider::class,

注意:对于Laravel <5.1版本,使用以下方法

 'aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider',

通过以下方式发布文件

 $ php artisan vendor:publish --provider="aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider"

或者仅使用 php artisan vendor:publish 并从输出列表中选择 aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider

应用迁移

 $ php artisan migrate

配置

此包在您的应用程序的 config 文件夹中发布一个名为 simple-access-log.php 的文件,其中包含此包的设置。大多数变量绑定到环境变量,但您可以直接编辑此文件或将其配置键添加到 .env 文件中。

access_log_db_connection | String
要使用的数据库连接。默认为环境变量 'DB_CONNECTION'。

'access_log_db_connection' => env('ACCESS_LOG_DB_CONNECTION', env('DB_CONNECTION'))

access_log_model | String 您想要使用的模型。模型必须实现 aliirfaan\LaravelSimpleAccessLog\Contracts\SimpleAccessLog

'access_log_model' => aliirfaan\LaravelSimpleAccessLog\Models\SimpleAccessLog::class,

用法

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use aliirfaan\LaravelSimpleAccessLog\Events\LoginSucceeded; // event you want to dispatch

class TestController extends Controller
{
    public function test(Request $request)
    {
        try {

            // log access after operation
            $eventData = [
                'ac_date_time_local' => date('Y-m-d H:i:s'),
                'ac_date_time_utc' => date('Y-m-d H:i:s'),
                'ac_actor_id' => 5,
                'ac_actor_type' => 'Model/Customer',
                'ac_actor_global_uid'=> 5,
                'ac_actor_username' => 'actor username',
                'ac_actor_group' => 'actor group',
                'ac_device_id' => 5,
                'ac_event_name' => 'user.login.success',
                'ac_server' => 'uat',
                'ac_version'=> 'version',
                'ac_ip_addr'=> $request->ip()
            ];

            // dispatch event
            LoginSucceeded::dispatch($eventData);

        } catch (\Exception $e) {
            report($e);
        }
    }
}

自定义模型

如果您对我们的访问日志有其他要求,您可以使用迁移添加列并使用自定义模型来使用您的新列。

<?php

namespace App\Models\AccessLog;

use Illuminate\Database\Eloquent\Model;
use aliirfaan\LaravelSimpleAccessLog\Contracts\SimpleAccessLog as SimpleAccessLogContract;
use aliirfaan\LaravelSimpleAccessLog\Models\SimpleAccessLog;

// custom class that extends base model and implements contract
class AccessLog extends SimpleAccessLog implements SimpleAccessLogContract
{
    public function __construct(array $attributes = [])
    {
        // add your additional columns to the fillable property
        $this->mergeFillable(['al_custom_field_1']);
        
        parent::__construct($attributes);
    }
}

在配置文件中指定自定义模型

'access_log_model' => App\Models\AccessLog\AccessLog::class,

许可证

MIT许可证 (MIT)

版权所有 (c) 2020

以下对本软件及其相关文档文件(“软件”)的副本的任何人,免费授予以下权利:在不受任何限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向提供软件的人员提供此类行为,但必须遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、适用于特定目的和无侵犯性的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是在合同行为、侵权行为或其他行为中,是否由软件或软件的使用或其他方式引起。