2017-11-18 19:35:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
|
|
|
|
|
|
class Add2FaLastAuthorizationTimeColumn extends Migration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the migrations.
|
|
|
|
*/
|
2022-11-25 21:29:04 +01:00
|
|
|
public function up(): void
|
2017-11-18 19:35:33 +01:00
|
|
|
{
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
|
|
|
$table->text('totp_secret')->nullable()->change();
|
|
|
|
$table->timestampTz('totp_authenticated_at')->after('totp_secret')->nullable();
|
|
|
|
});
|
|
|
|
|
|
|
|
DB::transaction(function () {
|
|
|
|
DB::table('users')->get()->each(function ($user) {
|
|
|
|
if (is_null($user->totp_secret)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::table('users')->where('id', $user->id)->update([
|
|
|
|
'totp_secret' => Crypt::encrypt($user->totp_secret),
|
2022-10-14 18:59:20 +02:00
|
|
|
'updated_at' => Carbon::now()->toAtomString(),
|
2017-11-18 19:35:33 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse the migrations.
|
|
|
|
*/
|
2022-11-25 21:29:04 +01:00
|
|
|
public function down(): void
|
2017-11-18 19:35:33 +01:00
|
|
|
{
|
|
|
|
DB::transaction(function () {
|
|
|
|
DB::table('users')->get()->each(function ($user) {
|
|
|
|
if (is_null($user->totp_secret)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::table('users')->where('id', $user->id)->update([
|
|
|
|
'totp_secret' => Crypt::decrypt($user->totp_secret),
|
2022-10-14 18:59:20 +02:00
|
|
|
'updated_at' => Carbon::now()->toAtomString(),
|
2017-11-18 19:35:33 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
DB::statement('ALTER TABLE users MODIFY totp_secret CHAR(16) DEFAULT NULL');
|
|
|
|
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
|
|
|
$table->dropColumn('totp_authenticated_at');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|