Skip to content

Commit 82a8cc3

Browse files
committed
Recycle Bin: Ensure permissions are updated for deleted items
Fixed lack of permission update scope in permission builder where trashed pages on chapter permission updates would not get permission updates. Also added permission rebuild on restore as an extra assurance. Added tests to cover. Thanks to SVO — svo.com.br for reporting.
1 parent e210cc3 commit 82a8cc3

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

app/Entities/Tools/TrashCan.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ protected function restoreEntity(Entity $entity): int
358358
$entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
359359
}
360360

361+
$entity->rebuildPermissions();
362+
361363
return $count;
362364
}
363365

app/Permissions/JointPermissionBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public function rebuildForEntity(Entity $entity): void
7070
}
7171

7272
if ($entity instanceof Chapter) {
73-
foreach ($entity->pages as $page) {
73+
$childPages = $entity->pages()
74+
->withTrashed()
75+
->select(['id', 'owned_by', 'book_id', 'chapter_id'])
76+
->get();
77+
foreach ($childPages as $page) {
7478
$entities[] = $page;
7579
}
7680
}

tests/Permissions/EntityPermissionsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,4 +711,31 @@ public function test_book_permissions_can_be_generated_without_error_if_child_ch
711711

712712
$this->assertNull($error);
713713
}
714+
715+
public function test_chapter_permissions_change_updates_permissions_for_pages_in_recycle_bin()
716+
{
717+
$chapter = $this->entities->chapterHasPages();
718+
/** @var Page $page */
719+
$page = $chapter->pages()->first();
720+
$editor = $this->users->editor();
721+
$editorRole = $editor->roles->first();
722+
723+
$this->asAdmin()->delete($page->getUrl());
724+
$this->assertTrue($page->refresh()->trashed());
725+
726+
$this->put($chapter->getUrl('/permissions'), [
727+
'permissions' => [
728+
'0' => [
729+
'active' => 'true',
730+
],
731+
],
732+
]);
733+
734+
$this->assertDatabaseHas('joint_permissions', [
735+
'entity_type' => 'page',
736+
'entity_id' => $page->id,
737+
'role_id' => $editorRole->id,
738+
'status' => 0,
739+
]);
740+
}
714741
}

tests/Settings/RecycleBinTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,24 @@ public function test_restore_page_shows_link_to_parent_restore_if_parent_also_de
296296
$pageRestoreView->assertSee('The parent of this item has also been deleted.');
297297
$this->withHtml($pageRestoreView)->assertElementContains('a[href$="/settings/recycle-bin/' . $bookDeletion->id . '/restore"]', 'Restore Parent');
298298
}
299+
300+
public function test_restore_action_rebuilds_permissions_for_entity()
301+
{
302+
$book = $this->entities->bookHasChaptersAndPages();
303+
/** @var Page $page */
304+
$page = $book->pages()->first();
305+
306+
$this->asEditor()->delete($book->getUrl());
307+
$bookDeletion = $book->deletions()->first();
308+
309+
DB::table('joint_permissions')->where('entity_id', '=', $book->id)
310+
->where('entity_type', '=', 'book')->delete();
311+
DB::table('joint_permissions')->where('entity_id', '=', $page->id)
312+
->where('entity_type', '=', 'page')->delete();
313+
314+
$this->asAdmin()->post("/settings/recycle-bin/{$bookDeletion->id}/restore");
315+
316+
$this->assertDatabaseHas('joint_permissions', ['entity_id' => $book->id, 'entity_type' => 'book']);
317+
$this->assertDatabaseHas('joint_permissions', ['entity_id' => $page->id, 'entity_type' => 'page']);
318+
}
299319
}

0 commit comments

Comments
 (0)