]> BookStack Code Mirror - bookstack/blob - database/migrations/2016_04_20_192649_create_joint_permissions_table.php
745e156bc20334f392a5b5c726ea6029cdb54056
[bookstack] / database / migrations / 2016_04_20_192649_create_joint_permissions_table.php
1 <?php
2
3 use Illuminate\Database\Migrations\Migration;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Support\Str;
6
7 return new class extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
15     {
16         Schema::create('joint_permissions', function (Blueprint $table) {
17             $table->increments('id');
18             $table->integer('role_id');
19             $table->string('entity_type');
20             $table->integer('entity_id');
21             $table->string('action');
22             $table->boolean('has_permission')->default(false);
23             $table->boolean('has_permission_own')->default(false);
24             $table->integer('created_by');
25             // Create indexes
26             $table->index(['entity_id', 'entity_type']);
27             $table->index('has_permission');
28             $table->index('has_permission_own');
29             $table->index('role_id');
30             $table->index('action');
31             $table->index('created_by');
32         });
33
34         Schema::table('roles', function (Blueprint $table) {
35             $table->string('system_name');
36             $table->boolean('hidden')->default(false);
37             $table->index('hidden');
38             $table->index('system_name');
39         });
40
41         Schema::rename('permissions', 'role_permissions');
42         Schema::rename('restrictions', 'entity_permissions');
43
44         // Create the new public role
45         $publicRoleData = [
46             'name'         => 'public',
47             'display_name' => 'Public',
48             'description'  => 'The role given to public visitors if allowed',
49             'system_name'  => 'public',
50             'hidden'       => true,
51             'created_at'   => \Carbon\Carbon::now()->toDateTimeString(),
52             'updated_at'   => \Carbon\Carbon::now()->toDateTimeString(),
53         ];
54
55         // Ensure unique name
56         while (DB::table('roles')->where('name', '=', $publicRoleData['display_name'])->count() > 0) {
57             $publicRoleData['display_name'] = $publicRoleData['display_name'] . Str::random(2);
58         }
59         $publicRoleId = DB::table('roles')->insertGetId($publicRoleData);
60
61         // Add new view permissions to public role
62         $entities = ['Book', 'Page', 'Chapter'];
63         $ops = ['View All', 'View Own'];
64         foreach ($entities as $entity) {
65             foreach ($ops as $op) {
66                 $name = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
67                 $permission = DB::table('role_permissions')->where('name', '=', $name)->first();
68                 // Assign view permission to public
69                 DB::table('permission_role')->insert([
70                     'permission_id' => $permission->id,
71                     'role_id'       => $publicRoleId,
72                 ]);
73             }
74         }
75
76         // Update admin role with system name
77         DB::table('roles')->where('name', '=', 'admin')->update(['system_name' => 'admin']);
78     }
79
80     /**
81      * Reverse the migrations.
82      *
83      * @return void
84      */
85     public function down()
86     {
87         Schema::drop('joint_permissions');
88
89         Schema::rename('role_permissions', 'permissions');
90         Schema::rename('entity_permissions', 'restrictions');
91
92         // Delete the public role
93         DB::table('roles')->where('system_name', '=', 'public')->delete();
94
95         Schema::table('roles', function (Blueprint $table) {
96             $table->dropColumn('system_name');
97             $table->dropColumn('hidden');
98         });
99     }
100 };