承接 outsidaz/laravel-data-anonymization 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

outsidaz/laravel-data-anonymization

Composer 安装命令:

composer require outsidaz/laravel-data-anonymization

包简介

README 文档

README

This Laravel package facilitates data anonymization, which helps organizations protect privacy, comply with regulations, reduce the risk of data breaches, and enable safe data sharing:

  1. Protecting privacy: Data contains sensitive information about individuals, such as their name, address, email, phone number, and other personally identifiable information (PII). Data anonymization helps protect the privacy of individuals by removing or masking their PII from the dataset.

  2. Compliance with regulations: Many countries and industries have regulations that require organizations to protect the privacy of individuals by anonymizing their data. For example, the General Data Protection Regulation (GDPR) in the European Union requires organizations to protect the privacy of individuals by anonymizing their data.

  3. Reducing the risk of data breaches: Data breaches can have serious consequences for organizations and individuals, including financial loss, reputational damage, and identity theft. By anonymizing data, organizations can reduce the risk of data breaches and minimize the impact of any data breaches that do occur.

  4. Enabling data sharing: Anonymized data can be shared with other organizations or researchers without violating the privacy of individuals. This can help promote collaboration and innovation in fields such as healthcare, finance, and social sciences.

Installation

You can install the package via composer:

composer require outsidaz/laravel-data-anonymization

You can publish the config file with:

php artisan vendor:publish --tag="laravel-data-anonymization-config"

This is the contents of the published config file:

return [
    'locale' => 'en_US',
    'chunk_size' => 1000,
    'models_path' => app_path('Models'),
    'models_namespace' => '\\App\\Models',
]

Usage

Model based definitions

In any model that contains sensitive data use the Anonymizable trait and implement the anonymizableAttributes method:

<?php
class User extends Authenticatable
{
    use Anonymizable;
    
    <...>

    public function anonymizableAttributes(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
    public function anonymizableCondition(): Builder
    {
        return self::withTrashed()->where('something', '=>', '...');
    }
}

Factory based definitions

To reduce the amount of necessary code, the anonymizable attributes may also be defined on your factories. Do note that the model still needs to implement the Anonymizable to work with these definitions.

Attributes based on the factory's definition

It is possible to use the factory's definition to use as anonymizable values. Implement the anonymizableAttributes method on your factory and return an array with keys that corresponds to the definition of the factory:

<?php

class UserFactory extends Factory
{
    public function definition(): array
    {
        return [
            'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
            'password' => 'secret',
            'firstname' => $this->faker->firstName(),
            'surname' => $this->faker->lastName(),
            'phone' => $this->faker->e164PhoneNumber(),
            'position' => $this->faker->jobTitle(),
            'token' => null,
        ]
    }
    
    public function anonymizableAttributes(): array
    {
        return [
            'email',
            'password',
            'firstname',
            'surname',
            'phone',
            'position',
            'token',
        ]
    }
}

This will use reduce the amount of code you need to write if the data you want to anonymize is the same as your factory's definition.

Note that only the defined keys will be anonymized!

Attributes based on a custom definition

If you prefer to still use custom values, you can still define them on the factory. Implement the anonymizableDefinition method on your factory, and return a keyed array:

<?php

class UserFactory extends Factory
{
    public function definition(): array
    {
        return [
            'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
            'password' => 'secret',
            'firstname' => $faker->firstName(),
            'surname' => $faker->lastName(),
            'phone' => $faker->e164PhoneNumber(),
            'position' => $faker->jobTitle(),
            'token' => null,
        ]
    }
    
    public function anonymizableDefinition(): array
    {
        return [
            'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
            'password' => 'secret',
            'firstname' => $faker->firstName(),
            'surname' => $faker->lastName(),
            'phone' => $faker->e164PhoneNumber(),
            'position' => $faker->jobTitle(),
            'token' => $this->faker->md5(),
        ]
    }
}

Defining custom values is mostly useful when used in tandem with the anonymizableAttributes method. This allows certain attributes from the factory to be used, while still defining custom attributes when necessary:

<?php

class UserFactory extends Factory
{
    public function definition(): array
    {
        return [
            'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
            'password' => 'secret',
            'firstname' => $faker->firstName(),
            'surname' => $faker->lastName(),
            'phone' => $faker->e164PhoneNumber(),
            'position' => $faker->jobTitle(),
            'token' => null,
        ]
    }
    
    public function anonymizableAttributes(): array
    {
        return [
            'email',
            'password',
            'firstname',
            'surname',
            'phone',
            'position',
        ]
    }
    
    public function anonymizableDefinition(): array
    {
        return [
            'token' => $this->faker->md5(),
        ]
    }
}

Important note

The data from the anonymizable methods on the factory will overwrite the data defined in the anonymizableAttributes method on the model!

Running the anonymizer

Anonymization is performed using command:

php artisan db:anonymize

Or on specific models:

php artisan db:anonymize --model=\\App\User --model=\\App\\Profile

License

The MIT License (MIT). Please see License File for more information.

outsidaz/laravel-data-anonymization 适用场景与选型建议

outsidaz/laravel-data-anonymization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.47k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「anonymization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 outsidaz/laravel-data-anonymization 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 14.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-14