3 use Illuminate\Database\Schema\Blueprint;
4 use Illuminate\Database\Migrations\Migration;
6 class CreateJointPermissionsTable extends Migration
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');
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');
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');
40 Schema::rename('permissions', 'role_permissions');
41 Schema::rename('restrictions', 'entity_permissions');
43 // Create the new public role
46 'display_name' => 'Public',
47 'description' => 'The role given to public visitors if allowed',
48 'system_name' => 'public',
50 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
51 'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
55 while (DB::table('roles')->where('name', '=', $publicRoleData['display_name'])->count() > 0) {
56 $publicRoleData['display_name'] = $publicRoleData['display_name'] . str_random(2);
58 $publicRoleId = DB::table('roles')->insertGetId($publicRoleData);
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
75 // Update admin role with system name
76 DB::table('roles')->where('name', '=', 'admin')->update(['system_name' => 'admin']);
78 // Generate the new entity jointPermissions
79 $restrictionService = app(\BookStack\Services\PermissionService::class);
80 $restrictionService->buildJointPermissions();
84 * Reverse the migrations.
88 public function down()
90 Schema::drop('joint_permissions');
92 Schema::rename('role_permissions', 'permissions');
93 Schema::rename('entity_permissions', 'restrictions');
95 // Delete the public role
96 DB::table('roles')->where('system_name', '=', 'public')->delete();
98 Schema::table('roles', function (Blueprint $table) {
99 $table->dropColumn('system_name');