]> BookStack Code Mirror - bookstack/blob - database/migrations/2018_08_04_115700_create_bookshelves_table.php
Added shelve icon, improved migration, added role permission
[bookstack] / database / migrations / 2018_08_04_115700_create_bookshelves_table.php
1 <?php
2
3 use Illuminate\Support\Facades\Schema;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Database\Migrations\Migration;
6
7 class CreateBookshelvesTable extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
15     {
16         Schema::create('bookshelves', function (Blueprint $table) {
17             $table->increments('id');
18             $table->string('name', 200);
19             $table->string('slug', 200);
20             $table->text('description');
21             $table->integer('created_by')->nullable()->default(null);
22             $table->integer('updated_by')->nullable()->default(null);
23             $table->boolean('restricted')->default(false);
24             $table->integer('image_id')->nullable()->default(null);
25             $table->timestamps();
26
27             $table->index('slug');
28             $table->index('created_by');
29             $table->index('updated_by');
30             $table->index('restricted');
31         });
32
33         // Copy existing role permissions from Books
34         $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
35         foreach ($ops as $op) {
36             $dbOpName = strtolower(str_replace(' ', '-', $op));
37             $roleIdsWithBookPermission = DB::table('role_permissions')
38                 ->leftJoin('permission_role', 'role_permissions.id', '=', 'permission_role.permission_id')
39                 ->leftJoin('roles', 'roles.id', '=', 'permission_role.role_id')
40                 ->where('role_permissions.name', '=', 'book-' . $dbOpName)->get(['roles.id'])->pluck('id');
41
42             $permId = DB::table('role_permissions')->insertGetId([
43                 'name' => 'bookshelf-' . $dbOpName,
44                 'display_name' => $op . ' ' . 'BookShelves',
45                 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
46                 'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
47             ]);
48
49             $rowsToInsert = $roleIdsWithBookPermission->map(function($roleId) use ($permId) {
50                 return [
51                     'role_id' => $roleId,
52                     'permission_id' => $permId
53                 ];
54             })->toArray();
55
56             // Assign view permission to all current roles
57             DB::table('permission_role')->insert($rowsToInsert);
58         }
59     }
60
61     /**
62      * Reverse the migrations.
63      *
64      * @return void
65      */
66     public function down()
67     {
68         // Drop created permissions
69         $ops = ['bookshelf-create-all','bookshelf-create-own','bookshelf-delete-all','bookshelf-delete-own','bookshelf-update-all','bookshelf-update-own','bookshelf-view-all','bookshelf-view-own'];
70
71         $permissionIds = DB::table('role_permissions')->whereIn('name', $ops)
72             ->get(['id'])->pluck('id')->toArray();
73         DB::table('permission_role')->whereIn('permission_id', $permissionIds)->delete();
74         DB::table('role_permissions')->whereIn('id', $permissionIds)->delete();
75
76         // Drop shelves table
77         Schema::dropIfExists('bookshelves');
78     }
79 }