jeffersongoncalves / laravel-ccpa
CCPA/CPRA toolkit core for Laravel: declare personal information on Eloquent models with its §1798.140(v) category, sensitive flag and whether it is sold or shared; build the inventory, export, deidentify, check k-anonymity.
Package info
github.com/jeffersongoncalves/laravel-ccpa
pkg:composer/jeffersongoncalves/laravel-ccpa
Requires
- php: ^8.2
- illuminate/console: ^12.0|^13.0
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.24
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Core of a Laravel toolkit for the California Consumer Privacy Act, as amended by the CPRA (Cal. Civ. Code §1798.100 et seq.). You declare which columns of your Eloquent models hold personal information. With those declarations the package can:
- build the personal information inventory: the categories you collect, which ones are sensitive, and which ones you sell or share. That is what your notice at collection and your privacy policy have to disclose (§§1798.100 and 1798.130(a)(5));
- export a consumer's personal information (right to know, §1798.110);
- deidentify a record, choosing a strategy per column (right to delete, §1798.105).
This package gives you tooling. It does not make you compliant on its own. You still need legal review for your categories, purposes and retention periods.
Installation
composer require jeffersongoncalves/laravel-ccpa
php artisan vendor:publish --tag="ccpa-config"
Declaring personal information
use JeffersonGoncalves\Ccpa\Attributes\PersonalData; use JeffersonGoncalves\Ccpa\Concerns\InteractsWithPersonalData; use JeffersonGoncalves\Ccpa\Contracts\HasPersonalData; use JeffersonGoncalves\Ccpa\Enums\{AnonymizeStrategy, DataCategory, PiCategory}; #[PersonalData('name', purpose: 'Account identification', piCategory: PiCategory::Identifiers, anonymize: AnonymizeStrategy::Mask)] #[PersonalData('email', piCategory: PiCategory::Identifiers, retention: '5 years', anonymize: AnonymizeStrategy::Hash, shared: true)] #[PersonalData('ssn', category: DataCategory::Sensitive, piCategory: PiCategory::Identifiers)] #[PersonalData('purchase_history', piCategory: PiCategory::Commercial, sold: true)] class User extends Authenticatable implements HasPersonalData { use InteractsWithPersonalData; }
piCategoryfollows the categories of §1798.140(v)(1):identifiers,customer_records,protected_classifications,commercial,biometric,internet_activity,geolocation,sensory,professional,educationandinferences.category: DataCategory::Sensitivemarks sensitive personal information (§1798.140(ae)): government identifiers, account log-in, precise geolocation, health, biometric data and so on. Consumers can limit its use (§1798.121).soldandsharedmark columns you sell, or share for cross-context behavioral advertising (§1798.140(ad) and (ah)). Consumers can opt out of both (§1798.120).
Register the model in config/ccpa.php:
'models' => [ App\Models\User::class, ],
Deidentify strategies
| Strategy | Result |
|---|---|
Nullify (default) |
null |
Hash |
HMAC-SHA256 keyed by CCPA_HASH_KEY (falls back to APP_KEY). It gives a stable pseudonym, so unique indexes keep working |
Mask |
Mary Smith → M********* |
Keep |
untouched, for data you must keep (a §1798.105(d) exception, such as a legal obligation) |
Encrypted casts ('encrypted') work as expected: the value is decrypted, then deidentified, then encrypted again.
Usage
use JeffersonGoncalves\Ccpa\Facades\Ccpa; Ccpa::inventory(); // one row per declared column, with pi_category, sold and shared $user->exportPersonalData(); // ['name' => ..., 'email' => ..., ...] $user->anonymizePersonalData(); // applies each strategy and saves
anonymizePersonalData() fires JeffersonGoncalves\Ccpa\Events\PersonalDataAnonymized with the model and the columns it changed. Columns marked Keep are not in that list.
php artisan ccpa:inventory # table php artisan ccpa:inventory --json # machine readable
k-anonymity
Deidentified information must not reasonably be linkable to a consumer (§1798.140(m)). Before you share or publish a dataset as deidentified, check that nobody can be singled out by combining quasi-identifiers such as ZIP code, birth date and gender. A dataset is k-anonymous when every combination of those values is shared by at least k rows. It is l-diverse when each of those groups also holds at least l distinct sensitive values.
use JeffersonGoncalves\Ccpa\Facades\Ccpa; use JeffersonGoncalves\Ccpa\Generalize; Ccpa::kAnonymity(Patient::class, [ 'zip' => Generalize::prefix(3), // 94110 → 941 'birth_date' => Generalize::date('Y'), // 1990-03-01 → 1990 'age' => Generalize::range(10), // 34 → 30-39 'gender', // as stored ], k: 5, sensitive: 'diagnosis'); // ['rows' => 1200, 'groups' => 87, 'k' => 3, 'rows_below_k' => 14, 'l' => 1]
php artisan ccpa:k-anonymity "App\Models\Patient" zip birth_date --k=5 --sensitive=diagnosis
The command fails (exit code 1) when the data is below the target k, so it can run in CI or before a scheduled export.
Testing
composer test
Changelog
See CHANGELOG.
License
The MIT License (MIT). See License File.
