定制 ramiroestrella/laravel-database-anonymize 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ramiroestrella/laravel-database-anonymize

Composer 安装命令:

composer require ramiroestrella/laravel-database-anonymize

包简介

Laravel Database Anonymize is a package designed to streamline data anonymization, enabling organizations to safeguard privacy, comply with regulations, reduce the risk of data breaches, and share data securely.

README 文档

README

Laravel Database Anonymize is a package designed to streamline data anonymization, enabling organizations to safeguard privacy, comply with regulations, reduce the risk of data breaches, and share data securely.

Key Benefits:

  1. Privacy Protection
    Anonymization removes or masks sensitive personally identifiable information (PII) such as names, addresses, emails, and phone numbers, preserving individuals' privacy.

  2. Regulatory Compliance
    Meet legal and industry standards like the EU's General Data Protection Regulation (GDPR) by anonymizing sensitive data to ensure compliance.

  3. Risk Mitigation
    Minimize the impact of data breaches by reducing the exposure of sensitive information, thereby safeguarding against financial losses, reputational damage, and identity theft.

  4. Secure Data Sharing
    Share anonymized datasets with researchers or other organizations without compromising privacy, fostering collaboration and innovation in fields like healthcare, finance, and social sciences.

Installation

Install the package via Composer:

composer require ramiroestrella/laravel-database-anonymize

Publish the configuration file with:

php artisan vendor:publish --provider="RamiroEstrella\LaravelDatabaseAnonymize\Providers\DatabaseAnonymizeServiceProvider"

The published configuration file (config/laravel-database-anonymize.php) includes:

return [
    'locale' => 'en_US',
    'chunk_size' => 1000,

    /*
    |--------------------------------------------------------------------------
    | Restricted Environments
    |--------------------------------------------------------------------------
    | Define environments (e.g., production, staging) that require confirmation 
    | before running anonymization commands for added security.
    */
    'restricted_env' => ['production', 'staging'],

    /*
    |--------------------------------------------------------------------------
    | Model Ordering
    |--------------------------------------------------------------------------
    | Optionally define the order of model anonymization. Priority models are
    | processed first to ensure dependencies are handled correctly.
    */
    'priority_models' => [],
];

Usage

Step 1: Implement the Anonymizable Trait

Add the Anonymizable trait to models containing sensitive data and define the anonymizableAttributes method.

Example (App\Models\User):

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use RamiroEstrella\LaravelDatabaseAnonymize\Traits\Anonymizable;
use Faker\Generator;

class User extends Authenticatable
{
    use Anonymizable;

    public function toAnonymize(Generator $faker): array
    {
        return [
            'email' => $this->id . '@custom.dev',
            'password' => 'secret',
            'firstname' => $faker->firstName,
            'surname' => $faker->lastName,
            'phone' => $faker->e164PhoneNumber,
            'position' => $faker->jobTitle,
            'token' => null,
        ];
    }

    // Optional: Specify conditions for anonymization
    public function anonymizeCondition(): Builder
    {
        return self::withTrashed()->where('something', '>=', '...');
    }
}

Step 2: Anonymize Related Models

If your model has relationships, you can specify the data to anonymize within related models using the relations property.

Example (App\Models\Employee):

<?php

namespace App\Models;

use RamiroEstrella\LaravelDatabaseAnonymize\Traits\Anonymizable;
use Faker\Generator;

class Employee extends Authenticatable
{
    use Anonymizable;

    public function anonymizableAttributes(Generator $faker): array
    {
        $firstName = $faker->firstName();
        $lastName = $faker->lastName();

        return [
            'name' => $firstName . ' ' . $lastName,
            'relations' => [
                'person' => [
                    'first_name' => $firstName,
                    'family_name' => $lastName,
                ],
            ],
        ];
    }
}

In this example:

  • The name attribute of the Employee model is anonymized.
  • The related person model has its first_name and family_name attributes anonymized as well.

Step 3: Run the Anonymization Command

Anonymize all models:

php artisan db:anonymize

Anonymize specific models:

php artisan db:anonymize --model=App\\\\Models\\\\User --model=App\\\\Models\\\\Employee

Exclude a specific model:

php artisan db:anonymize --exclude=App\\\\Models\\\\Employee

Configuration Options

  • Locale: Defines the locale used by Faker (default: en_US).
  • Chunk Size: Processes data in chunks for memory efficiency (default: 1000).
  • Restricted Environments: Prevents accidental anonymization in critical environments like production or staging.
  • Priority Models: Ensures dependent models are anonymized in the correct order.

License

This package is open-sourced software licensed under the MIT License.

ramiroestrella/laravel-database-anonymize 适用场景与选型建议

ramiroestrella/laravel-database-anonymize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.78k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ramiroestrella/laravel-database-anonymize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ramiroestrella/laravel-database-anonymize 我们能提供哪些服务?
定制开发 / 二次开发

基于 ramiroestrella/laravel-database-anonymize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-10