bobkosse/laravel-data-security 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

bobkosse/laravel-data-security

最新稳定版本:0.2.0

Composer 安装命令:

composer require bobkosse/laravel-data-security

包简介

Easy to use data security solution for Laravel

README 文档

README

Workflow Badge Release Badge Stars Badge Open Issues Badge

This package provides a lightweight solution for handling sensitive data within Laravel Eloquent models. It automatically encrypts specific fields, decrypts them only when explicitly requested, and includes an audit tool to inspect privacy coverage across your application.

Features

  • Automatic field encryption: Encrypts sensitive model attributes before they are stored in the database.
  • Automatic field decryption: Decrypts privacy fields only when privacy is explicitly revealed.
  • Safe default masking: Privacy fields return [ENCRYPTED] by default to prevent accidental data exposure.
  • Bulk write support: Works with model-based bulk operations such as insert(), insertOrIgnore(), upsert(), and where()->update().
  • Duplicate encryption protection: Prevents already encrypted values from being encrypted again.
  • Clear decryption errors: Throws a PrivacyDecryptionException when encrypted values cannot be decrypted.
  • Privacy audit command: Scans your models and reports which ones use the privacy trait and which privacy fields they define.
  • Interactive encryption command: Encrypt existing data for newly added privacy fields.
  • Attribute-based configuration: Use PHP 8 attributes for cleaner model definitions.

Installation

  1. Add the package to your project.
  2. Register the PrivacyAuditCommand in your console kernel if it is not auto-discovered.

Usage

1. Prepare your model

Add the HasPrivacy trait to any Eloquent model containing sensitive data. Define the fields that should be protected using the #[Protect] attribute.

use BobKosse\DataSecurity\Attributes\Protect;
use BobKosse\DataSecurity\Traits\HasPrivacy;
use Illuminate\Database\Eloquent\Model;

#[Protect(fields: ['phone_number', 'address', 'social_security_number'])]
class PatientProfile extends Model
{
  use HasPrivacy;
}

2. How it works

Saving data

When a value is assigned to a field listed in the #[Protect] attribute, the trait automatically encrypts it before it reaches the database.

This works with:

  • fill()
  • create()
  • save()
  • update()
  • forceFill()

It also supports bulk model operations:

  • insert()
  • insertOrIgnore()
  • upsert()
  • where()->update()

Revealing data

To access the decrypted value, explicitly reveal privacy first.

Reading data

By default, privacy fields return [ENCRYPTED] when accessed.

use BobKosse\DataSecurity\Exceptions\PrivacyDecryptionException;

$profile = PatientProfile::find(1);

// Returns "[ENCRYPTED]"
echo $profile->phone_number;

// Returns the decrypted value (e.g., "+31 6 12345678")
$profile->revealPrivacy(true);
echo $profile->phone_number;

This can also be used in combination with authorization policies to ensure sensitive data is only accessible by authorized users.

3. `HasPrivacy` works on all Laravel Eloquent models

The trait is designed for Eloquent models and does not affect raw database queries.

Supported Eloquent-based writes include:

  • fill()
  • create()
  • save()
  • update()
  • forceFill()
  • insert()
  • insertOrIgnore()
  • upsert()
  • where()->update()

Commands

Privacy Audit Command

The package includes a console command that scans a directory for Eloquent models and reports which ones use the privacy trait.

php artisan privacy:audit app/Models

The command shows:

  • Model: the full class name of the model
  • Has Privacy Trait: whether the trait is implemented
  • Privacy Fields: the fields currently configured for encryption

Privacy Encrypt Field Command

When you add privacy protection to an existing field that already contains plain-text data, you can use this command to encrypt it.

php artisan privacy:encrypt-field

This interactive command will:

  1. Ask you to select a model.
  2. Ask you to select a field from that model.
  3. Automatically update the model file to add the #[Protect] attribute and HasPrivacy trait if they are missing.
  4. Encrypt all existing plain-text values for that field in the database.

Important notes

  • User model safety: The trait intentionally avoids running on the default User model to prevent accidental locking out of authentication data.
  • Database column size: Ensure privacy columns can store encrypted strings, typically using TEXT or BLOB.
  • Raw SQL is out of scope: Direct DB::table() or raw SQL statements bypass the trait.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: HTML

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固