]> BookStack Code Mirror - bookstack/blob - database/migrations/2022_10_08_104202_drop_entity_restricted_field.php
Migrations: Updated with type hints instead of php doc
[bookstack] / database / migrations / 2022_10_08_104202_drop_entity_restricted_field.php
1 <?php
2
3 use Illuminate\Database\Migrations\Migration;
4 use Illuminate\Database\Query\Builder;
5 use Illuminate\Database\Query\JoinClause;
6 use Illuminate\Database\Schema\Blueprint;
7 use Illuminate\Support\Facades\DB;
8 use Illuminate\Support\Facades\Schema;
9
10 return new class extends Migration
11 {
12     /**
13      * Run the migrations.
14      */
15     public function up(): void
16     {
17         // Remove entity-permissions on non-restricted entities
18         $deleteInactiveEntityPermissions = function (string $table, string $morphClass) {
19             $permissionIds = DB::table('entity_permissions')->select('entity_permissions.id as id')
20                 ->join($table, function (JoinClause $join) use ($table, $morphClass) {
21                     return $join->where($table . '.restricted', '=', 0)
22                         ->on($table . '.id', '=', 'entity_permissions.entity_id');
23                 })->where('entity_type', '=', $morphClass)
24                 ->pluck('id');
25             DB::table('entity_permissions')->whereIn('id', $permissionIds)->delete();
26         };
27         $deleteInactiveEntityPermissions('pages', 'page');
28         $deleteInactiveEntityPermissions('chapters', 'chapter');
29         $deleteInactiveEntityPermissions('books', 'book');
30         $deleteInactiveEntityPermissions('bookshelves', 'bookshelf');
31
32         // Migrate restricted=1 entries to new entity_permissions (role_id=0) entries
33         $defaultEntityPermissionGenQuery = function (Builder $query, string $table, string $morphClass) {
34             return $query->select(['id as entity_id'])
35                 ->selectRaw('? as entity_type', [$morphClass])
36                 ->selectRaw('? as `role_id`', [0])
37                 ->selectRaw('? as `view`', [0])
38                 ->selectRaw('? as `create`', [0])
39                 ->selectRaw('? as `update`', [0])
40                 ->selectRaw('? as `delete`', [0])
41                 ->from($table)
42                 ->where('restricted', '=', 1);
43         };
44
45         $query = $defaultEntityPermissionGenQuery(DB::query(), 'pages', 'page')
46             ->union(fn(Builder $query) => $defaultEntityPermissionGenQuery($query, 'books', 'book'))
47             ->union(fn(Builder $query) => $defaultEntityPermissionGenQuery($query, 'chapters', 'chapter'))
48             ->union(fn(Builder $query) => $defaultEntityPermissionGenQuery($query, 'bookshelves', 'bookshelf'));
49
50         DB::table('entity_permissions')->insertUsing(['entity_id', 'entity_type', 'role_id', 'view', 'create', 'update', 'delete'], $query);
51
52         // Drop restricted columns
53         $dropRestrictedColumn = fn(Blueprint $table) => $table->dropColumn('restricted');
54         Schema::table('pages', $dropRestrictedColumn);
55         Schema::table('chapters', $dropRestrictedColumn);
56         Schema::table('books', $dropRestrictedColumn);
57         Schema::table('bookshelves', $dropRestrictedColumn);
58     }
59
60     /**
61      * Reverse the migrations.
62      */
63     public function down(): void
64     {
65         // Create restricted columns
66         $createRestrictedColumn = fn(Blueprint $table) => $table->boolean('restricted')->index()->default(0);
67         Schema::table('pages', $createRestrictedColumn);
68         Schema::table('chapters', $createRestrictedColumn);
69         Schema::table('books', $createRestrictedColumn);
70         Schema::table('bookshelves', $createRestrictedColumn);
71
72         // Set restrictions for entities that have a default entity permission assigned
73         // Note: Possible loss of data where default entity permissions have been configured
74         $restrictEntities = function (string $table, string $morphClass) {
75             $toRestrictIds = DB::table('entity_permissions')
76                 ->where('role_id', '=', 0)
77                 ->where('entity_type', '=', $morphClass)
78                 ->pluck('entity_id');
79             DB::table($table)->whereIn('id', $toRestrictIds)->update(['restricted' => true]);
80         };
81         $restrictEntities('pages', 'page');
82         $restrictEntities('chapters', 'chapter');
83         $restrictEntities('books', 'book');
84         $restrictEntities('bookshelves', 'bookshelf');
85
86         // Delete default entity permissions
87         DB::table('entity_permissions')->where('role_id', '=', 0)->delete();
88     }
89 };