]> BookStack Code Mirror - bookstack/commitdiff
Added proper entity permission removal on role deletion
authorDan Brown <redacted>
Fri, 7 Oct 2022 12:12:33 +0000 (13:12 +0100)
committerDan Brown <redacted>
Fri, 7 Oct 2022 12:12:33 +0000 (13:12 +0100)
Added test to cover.

app/Auth/Permissions/PermissionsRepo.php
app/Auth/Role.php
tests/Permissions/RolesTest.php

index 2c2bedb725d0beea98fb03380fbc269af1123a62..6dcef72568343d550c169a4ad7124a035c0223ed 100644 (file)
@@ -139,6 +139,7 @@ class PermissionsRepo
             }
         }
 
+        $role->entityPermissions()->delete();
         $role->jointPermissions()->delete();
         Activity::add(ActivityType::ROLE_DELETE, $role);
         $role->delete();
index 3ae469b59c10fb106d4f7364567101ce8e698cde..d5ce5cab70522c6e602d2d55857bb9448d5f1c61 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace BookStack\Auth;
 
+use BookStack\Auth\Permissions\EntityPermission;
 use BookStack\Auth\Permissions\JointPermission;
 use BookStack\Auth\Permissions\RolePermission;
 use BookStack\Interfaces\Loggable;
@@ -54,6 +55,14 @@ class Role extends Model implements Loggable
         return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
     }
 
+    /**
+     * Get the entity permissions assigned to this role.
+     */
+    public function entityPermissions(): HasMany
+    {
+        return $this->hasMany(EntityPermission::class);
+    }
+
     /**
      * Check if this role has a permission.
      */
index 7512c6d2fb0b220c0d4cb9c2f3c3fe43832f0a2b..6c2f4c0df204697edfca5e82b216711ec2530efe 100644 (file)
@@ -163,6 +163,29 @@ class RolesTest extends TestCase
         $this->assertEquals($this->user->id, $roleA->users()->first()->id);
     }
 
+    public function test_entity_permissions_are_removed_on_delete()
+    {
+        /** @var Role $roleA */
+        $roleA = Role::query()->create(['display_name' => 'Entity Permissions Delete Test']);
+        $page = $this->entities->page();
+
+        $this->entities->setPermissions($page, ['view'], [$roleA]);
+
+        $this->assertDatabaseHas('entity_permissions', [
+            'role_id' => $roleA->id,
+            'restrictable_id' => $page->id,
+            'restrictable_type' => $page->getMorphClass(),
+        ]);
+
+        $this->asAdmin()->delete("/settings/roles/delete/$roleA->id");
+
+        $this->assertDatabaseMissing('entity_permissions', [
+            'role_id' => $roleA->id,
+            'restrictable_id' => $page->id,
+            'restrictable_type' => $page->getMorphClass(),
+        ]);
+    }
+
     public function test_image_view_notice_shown_on_role_form()
     {
         /** @var Role $role */