]> BookStack Code Mirror - bookstack/blob - database/migrations/2019_12_29_120917_add_api_auth.php
Followed Laravel 9 update steps and file changes
[bookstack] / database / migrations / 2019_12_29_120917_add_api_auth.php
1 <?php
2
3 use Illuminate\Database\Migrations\Migration;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Support\Carbon;
6 use Illuminate\Support\Facades\Schema;
7
8 return new class extends Migration
9 {
10     /**
11      * Run the migrations.
12      *
13      * @return void
14      */
15     public function up()
16     {
17
18         // Add API tokens table
19         Schema::create('api_tokens', function (Blueprint $table) {
20             $table->increments('id');
21             $table->string('name');
22             $table->string('token_id')->unique();
23             $table->string('secret');
24             $table->integer('user_id')->unsigned()->index();
25             $table->date('expires_at')->index();
26             $table->nullableTimestamps();
27         });
28
29         // Add access-api permission
30         $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id;
31         $permissionId = DB::table('role_permissions')->insertGetId([
32             'name'         => 'access-api',
33             'display_name' => 'Access system API',
34             'created_at'   => Carbon::now()->toDateTimeString(),
35             'updated_at'   => Carbon::now()->toDateTimeString(),
36         ]);
37         DB::table('permission_role')->insert([
38             'role_id'       => $adminRoleId,
39             'permission_id' => $permissionId,
40         ]);
41     }
42
43     /**
44      * Reverse the migrations.
45      *
46      * @return void
47      */
48     public function down()
49     {
50         // Remove API tokens table
51         Schema::dropIfExists('api_tokens');
52
53         // Remove access-api permission
54         $apiAccessPermission = DB::table('role_permissions')
55             ->where('name', '=', 'access-api')->first();
56
57         DB::table('permission_role')->where('permission_id', '=', $apiAccessPermission->id)->delete();
58         DB::table('role_permissions')->where('name', '=', 'access-api')->delete();
59     }
60 };