Initialer Laravel Commit für BetiX
This commit is contained in:
44
app/Console/Commands/ReencryptUserData.php
Normal file
44
app/Console/Commands/ReencryptUserData.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ReencryptUserData extends Command
|
||||
{
|
||||
protected $signature = 'data:reencrypt-users {--chunk=500}';
|
||||
protected $description = 'Re-read and re-save user fields to normalize encryption and blind indices';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$chunk = (int) $this->option('chunk');
|
||||
$updated = 0;
|
||||
|
||||
// We want small transactions per chunk to avoid long locks
|
||||
User::query()->orderBy('id')
|
||||
->chunkById($chunk, function ($users) use (&$updated) {
|
||||
DB::transaction(function () use ($users, &$updated) {
|
||||
foreach ($users as $u) {
|
||||
// Read via casts (plaintext) and assign back to trigger normalization
|
||||
$email = $u->email;
|
||||
$username = $u->username;
|
||||
|
||||
$dirty = false;
|
||||
if ($email !== null) { $u->email = $email; $dirty = true; }
|
||||
if ($username !== null) { $u->username = $username; $dirty = true; }
|
||||
|
||||
if ($dirty) {
|
||||
$u->saveQuietly();
|
||||
$updated++;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$this->info("Users normalized: {$updated}");
|
||||
$this->info('Tip: Remove APP_PREVIOUS_KEYS after normalization if keys are unified.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user