utopia-php / audit
一个用于管理应用程序用户日志的简单审计库
0.45.0
2024-09-18 04:40 UTC
Requires
- php: >=8.0
- utopia-php/database: 0.55.*
Requires (Dev)
- laravel/pint: 1.5.*
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.3
- dev-main
- 0.45.0
- 0.44.0
- 0.44.0-RC1
- 0.43.0
- 0.42.0
- 0.41.0
- 0.40.0
- 0.39.1
- 0.39.0
- 0.38.0
- 0.37.0
- 0.36.x-dev
- 0.36.1
- 0.36.0
- 0.35.0
- 0.34.0
- 0.33.1
- 0.33.0
- 0.32.0
- 0.31.0
- 0.30.0
- 0.29.0
- 0.28.0
- 0.27.0
- 0.26.0
- 0.25.0
- 0.24.1
- 0.24.0
- 0.23.0
- 0.22.0
- 0.21.0
- 0.20.0
- 0.19.0
- 0.18.0
- 0.17.0
- 0.16.0
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.0
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.0
- dev-chore-findones-empties
- dev-chore-update-database
- dev-add-attribute-key
- dev-feat-upgrade-db
This package is auto-updated.
Last update: 2024-09-18 05:44:07 UTC
README
Utopia框架审计库是一个简单轻量级的库,用于管理应用程序用户日志。这个库的目标是尽可能简单易学易用。该库由Appwrite团队维护。
虽然这个库是Utopia框架项目的一部分,但它不依赖任何外部库,可以独立于任何其他PHP项目或框架使用。
入门指南
使用composer安装
composer require utopia-php/audit
初始化审计对象
<?php require_once __DIR__ . '/../../vendor/autoload.php'; use PDO; use PDO; use Utopia\Audit\Audit; use Utopia\Cache\Cache; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Database\Adapter\MySQL; use Utopia\Database\Database; $dbHost = '127.0.0.1'; $dbUser = 'travis'; $dbPass = ''; $dbPort = '3306'; $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, [ PDO::ATTR_TIMEOUT => 3, // Seconds PDO::ATTR_PERSISTENT => true, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_STRINGIFY_FETCHES => true, ]); $cache = new Cache(new NoCache()); $database = new Database(new MySQL($pdo),$cache); $database->setNamespace('namespace'); $audit = new Audit($database); $audit->setup();
创建日志
在审计数据库中记录用户操作的简单示例。
$userId = 'user-unique-id'; $event = 'deleted'; // Log specific action name $resource = 'database/document-1'; // Resource unique ID (great for filtering specific logs) $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'; // Set user-agent $ip = '127.0.0.1'; // User IP $location = 'US'; // Country name or code $data = ['key1' => 'value1','key2' => 'value2']; // Any key-value pair you need to log $audit->log($userId, $event, $resource, $userAgent, $ip, $location, $data);
按用户获取日志
通过给定的用户ID获取所有日志
$logs = $audit->getLogsByUser( 'userId' // User unique ID ); // Returns an array of all logs for specific user
按用户和操作获取日志
通过给定的用户ID和特定的事件名称获取所有日志
$logs = $audit->getLogsByUserAndEvents( 'userId', // User unique ID ['update', 'delete'] // List of selected event to fetch ); // Returns an array of all logs for specific user filtered by given actions
按资源获取日志
通过给定的资源名称获取所有日志
$logs = $audit->getLogsByResource( 'resource-name', // Resource Name ); // Returns an array of all logs for the specific resource
系统要求
Utopia框架需要PHP 8.0或更高版本。我们建议尽可能使用最新的PHP版本。
版权和许可
MIT许可证(MIT) http://www.opensource.org/licenses/mit-license.php