schneespur/app/Services/DriverAnonymizationService.php
Michael ee3dbba6cc Initial release v1.0.0
Schneespur — Open-source winter service documentation software (PWA + Admin).
GPS tracking via OwnTracks, weather data, photo evidence, and legally
compliant service records for winter maintenance operators.

License: AGPL-3.0-or-later
2026-05-17 13:33:51 +00:00

38 lines
1.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\User;
use DomainException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class DriverAnonymizationService
{
public function anonymize(User $driver, string $reason): void
{
if ($driver->isAnonymized()) {
throw new DomainException('Driver is already anonymized.');
}
DB::transaction(function () use ($driver, $reason) {
$driver->name = '';
$driver->email = "anonymized-{$driver->id}@localhost";
$driver->phone = null;
$driver->owntracks_username = null;
$driver->owntracks_password_hash = null;
$driver->remember_token = null;
$driver->anonymized_at = now();
$driver->anonymization_reason = $reason;
DB::table('users')->where('id', $driver->id)->update([
'password' => Hash::make(Str::random(64)),
]);
$driver->saveQuietly();
DB::table('sessions')->where('user_id', $driver->id)->delete();
});
}
}