3 use Illuminate\Database\Migrations\Migration;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Support\Str;
7 return new class extends Migration
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');
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');
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');
41 Schema::rename('permissions', 'role_permissions');
42 Schema::rename('restrictions', 'entity_permissions');
44 // Create the new public role
47 'display_name' => 'Public',
48 'description' => 'The role given to public visitors if allowed',
49 'system_name' => 'public',
51 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
52 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
56 while (DB::table('roles')->where('name', '=', $publicRoleData['display_name'])->count() > 0) {
57 $publicRoleData['display_name'] = $publicRoleData['display_name'] . Str::random(2);
59 $publicRoleId = DB::table('roles')->insertGetId($publicRoleData);
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,
76 // Update admin role with system name
77 DB::table('roles')->where('name', '=', 'admin')->update(['system_name' => 'admin']);
81 * Reverse the migrations.
85 public function down()
87 Schema::drop('joint_permissions');
89 Schema::rename('role_permissions', 'permissions');
90 Schema::rename('entity_permissions', 'restrictions');
92 // Delete the public role
93 DB::table('roles')->where('system_name', '=', 'public')->delete();
95 Schema::table('roles', function (Blueprint $table) {
96 $table->dropColumn('system_name');
97 $table->dropColumn('hidden');