GDPR-compliant user data anonymization module for Yii2 applications.
- REST API endpoints for user data anonymization
- CLI commands for analysis and execution
- Configurable helper classes for custom anonymization logic
- JWT-based authentication for REST endpoints
- Dry-run mode for previewing changes
- Built-in helper for yii2-usuario tables
- PHP >= 8.1
- Yii2 >= 2.0.45
- bizley/jwt >= 4.0
composer require dmstr/yii2-anonymizer-moduleIf not using Packagist, add to your composer.json:
{
"autoload": {
"psr-4": {
"dmstr\\anonymizer\\": "path/to/yii2-anonymizer-module/src"
}
}
}Then run:
composer dump-autoloadAdd to project/config/main.php in the $common['modules'] section:
'anonymizer' => [
'class' => \dmstr\anonymizer\Module::class,
'helpers' => [
\dmstr\anonymizer\helpers\UsuarioAnonymizationHelper::class,
// Add application-specific helpers here
],
'userModelClass' => \project\modules\user\models\User::class,
'requiredRole' => 'Tech_User', // Optional: Role required for REST access
'anonymizationPrefix' => 'ANONYMIZED_USER_',
'anonymizationDomain' => 'waldportal.local',
],Add to project/config/main.php in the $console['controllerMap'] section:
$console = [
'controllerMap' => [
'anonymizer' => [
'class' => \dmstr\anonymizer\commands\AnonymizerController::class,
],
],
];Add to project/config/main.php in the urlManager.rules section:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['anonymizer/anonymize'],
'patterns' => [
'DELETE {uuid}' => 'remove',
'GET {uuid}' => 'analyze',
'OPTIONS {uuid}' => 'options',
],
],Your User model must implement a static findUserByUuid() method:
public static function findUserByUuid(string $uuid): ?self
{
$socialAccount = \Da\User\Model\SocialNetworkAccount::find()
->andWhere(['client_id' => $uuid])
->one();
if (!$socialAccount) {
return null;
}
return static::findOne($socialAccount->user_id);
}Anonymize user data:
curl -X DELETE \
"https://your-app.com/anonymizer/anonymize/550e8400-e29b-41d4-a716-446655440000" \
-H "Authorization: Bearer <jwt-token>"Analyze (dry-run):
curl -X GET \
"https://your-app.com/anonymizer/anonymize/550e8400-e29b-41d4-a716-446655440000" \
-H "Authorization: Bearer <jwt-token>"List configured helpers:
yii anonymizer/helpersAnalyze user data (dry-run):
yii anonymizer/analyze --uuid=550e8400-e29b-41d4-a716-446655440000Execute anonymization:
yii anonymizer/execute --uuid=550e8400-e29b-41d4-a716-446655440000 --forceJSON output format:
yii anonymizer/analyze --uuid=<uuid> --format=jsonImplement AnonymizationHelperInterface:
<?php
namespace app\helpers;
use dmstr\anonymizer\interfaces\AnonymizationHelperInterface;
use Yii;
class CustomDataHelper implements AnonymizationHelperInterface
{
public static function anonymize($user, array $options = []): array
{
// Perform anonymization
$count = Yii::$app->db->createCommand()
->update('custom_table', ['data' => null], ['user_id' => $user->id])
->execute();
return [
'success' => true,
'message' => 'Custom data anonymized',
'records_updated' => ['custom_table' => $count],
];
}
public static function analyze($user, array $options = []): array
{
// Count affected records (dry-run)
$count = (int) Yii::$app->db->createCommand(
'SELECT COUNT(*) FROM custom_table WHERE user_id = :id',
[':id' => $user->id]
)->queryScalar();
return [
'success' => true,
'message' => 'Analysis complete',
'records_updated' => ['custom_table' => $count],
];
}
public static function getDescription(): string
{
return 'Anonymizes custom application data';
}
}| Option | Type | Default | Description |
|---|---|---|---|
helpers |
array | [] |
List of helper class names |
userModelClass |
string | required | User model class with findUserByUuid() |
requiredRole |
string|null | null |
Role required for REST access |
anonymizationPrefix |
string | 'anon_' |
Prefix for anonymized values |
anonymizationDomain |
string | 'anonymized.local' |
Domain for anonymized emails |
BSD-3-Clause