Replace empty or too short api keys instead of resetting them

This commit is contained in:
Igor Scheller 2024-01-02 13:51:20 +01:00 committed by Michael Weimann
parent 3f03d6b1d8
commit dbbf30e233
1 changed files with 10 additions and 3 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Support\Str;
class CleanupShortApiKeys extends Migration
{
@ -14,8 +15,14 @@ class CleanupShortApiKeys extends Migration
public function up(): void
{
$db = $this->schema->getConnection();
foreach ($db->table('users')->get() as $user) {
if (Str::length($user->api_key) > 42) {
continue;
}
$db->table('users')
->where($db->raw('LENGTH(api_key)'), '<=', 42)
->update(['api_key' => '']);
->where('id', $user->id)
->update(['api_key' => bin2hex(random_bytes(32))]);
}
}
}