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