Преглед изворни кода

Add migration to bind settings to users and to set identify admin

Bubka пре 2 година
родитељ
комит
d0401ced5d

+ 74 - 0
database/migrations/2023_02_10_145413_add_is_admin_and_preferences_to_users_table.php

@@ -0,0 +1,74 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Arr;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->boolean('is_admin')
+                ->after('password')
+                ->default(0);
+            $table->json('preferences')->nullable();
+        });
+
+        DB::table('users')->update(['is_admin' => 1]);
+
+        // User options are converted as user preferences
+        $options     = DB::table('options')->get();
+        $preferences = config('2fauth.preferences');
+
+        foreach ($options as $option) {
+            if (Arr::has($preferences, $option->key)) {
+                DB::table('users')->update(['preferences->'.$option->key => static::restoreType($option->value)]);
+                DB::table('options')->where('id', $option->id)->delete();
+            }
+        }
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropColumn('is_admin');
+        });
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropColumn('preferences');
+        });
+    }
+
+    /**
+     * Replaces patterned string that represent booleans with real booleans
+     *
+     * @param  mixed  $value
+     * @return mixed
+     */
+    protected static function restoreType(mixed $value)
+    {
+        if (is_numeric($value)) {
+            $value = is_float($value + 0) ? (float) $value : (int) $value;
+        }
+
+        if ($value === '{{}}') {
+            return false;
+        } elseif ($value === '{{1}}') {
+            return true;
+        }
+            
+        return $value;
+    }
+};