3 use Illuminate\Database\Schema\Blueprint;
4 use Illuminate\Database\Migrations\Migration;
6 class CreateEntityPermissionsTable extends Migration
15 Schema::create('entity_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 // Create the new public role
41 $publicRole = new \BookStack\Role();
42 $publicRole->name = 'public';
43 $publicRole->display_name = 'Public';
44 $publicRole->description = 'The role given to public visitors if allowed';
45 $publicRole->system_name = 'public';
46 $publicRole->hidden = true;
48 while (\BookStack\Role::getRole($publicRole->name) !== null) {
49 $publicRole->name = $publicRole->name . str_random(2);
53 // Add new view permissions to public role
54 $entities = ['Book', 'Page', 'Chapter'];
55 $ops = ['View All', 'View Own'];
56 foreach ($entities as $entity) {
57 foreach ($ops as $op) {
58 $name = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
59 $permission = \BookStack\Permission::getByName($name);
60 // Assign view permissions to public
61 $publicRole->attachPermission($permission);
65 // Update admin role with system name
66 $admin = \BookStack\Role::getRole('admin');
67 $admin->system_name = 'admin';
70 // Generate the new entity permissions
71 $restrictionService = app(\BookStack\Services\RestrictionService::class);
72 $restrictionService->buildEntityPermissions();
76 * Reverse the migrations.
80 public function down()
82 Schema::drop('entity_permissions');
84 // Delete the public role
85 $public = \BookStack\Role::getSystemRole('public');
88 Schema::table('roles', function (Blueprint $table) {
89 $table->dropColumn('system_name');