]> BookStack Code Mirror - bookstack/blob - database/migrations/2016_09_29_101449_remove_hidden_roles.php
Implement functionality to export a book, along with its pages and chapters, as a...
[bookstack] / database / migrations / 2016_09_29_101449_remove_hidden_roles.php
1 <?php
2
3 use Carbon\Carbon;
4 use Illuminate\Database\Migrations\Migration;
5 use Illuminate\Database\Schema\Blueprint;
6 use Illuminate\Support\Facades\DB;
7 use Illuminate\Support\Facades\Schema;
8
9 return new class extends Migration
10 {
11     /**
12      * Run the migrations.
13      */
14     public function up(): void
15     {
16         // Remove the hidden property from roles
17         Schema::table('roles', function (Blueprint $table) {
18             $table->dropColumn('hidden');
19         });
20
21         // Add column to mark system users
22         Schema::table('users', function (Blueprint $table) {
23             $table->string('system_name')->nullable()->index();
24         });
25
26         // Insert our new public system user.
27         $publicUserId = DB::table('users')->insertGetId([
28             'email'           => '[email protected]',
29             'name'            => 'Guest',
30             'system_name'     => 'public',
31             'email_confirmed' => true,
32             'created_at'      => Carbon::now(),
33             'updated_at'      => Carbon::now(),
34         ]);
35
36         // Get the public role
37         $publicRole = DB::table('roles')->where('system_name', '=', 'public')->first();
38
39         // Connect the new public user to the public role
40         DB::table('role_user')->insert([
41             'user_id' => $publicUserId,
42             'role_id' => $publicRole->id,
43         ]);
44     }
45
46     /**
47      * Reverse the migrations.
48      */
49     public function down(): void
50     {
51         Schema::table('roles', function (Blueprint $table) {
52             $table->boolean('hidden')->default(false);
53             $table->index('hidden');
54         });
55
56         DB::table('users')->where('system_name', '=', 'public')->delete();
57
58         Schema::table('users', function (Blueprint $table) {
59             $table->dropColumn('system_name');
60         });
61
62         DB::table('roles')->where('system_name', '=', 'public')->update(['hidden' => true]);
63     }
64 };