Skip to content

Commit

Permalink
Add h5p_author_delete permission (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Sep 21, 2023
1 parent 23e6a30 commit 357a1df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Enums/H5PPermissionsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class H5PPermissionsEnum extends BasicEnum

const H5P_AUTHOR_LIST = 'h5p_author_list';
const H5P_AUTHOR_UPDATE = 'h5p_author_update';
const H5P_AUTHOR_DELETE = 'h5p_author_delete';

const H5P_LIBRARY_LIST = 'h5p_library_list';
const H5P_LIBRARY_READ = 'h5p_library_read';
Expand Down
8 changes: 5 additions & 3 deletions src/Policies/H5PContentPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public function create(?User $user): bool
return $user && $user->can(H5PPermissionsEnum::H5P_CREATE);
}

public function delete(?User $user): bool
public function delete(?User $user, H5PContent $h5PContent): bool
{
return $user && $user->can(H5PPermissionsEnum::H5P_DELETE);
return $user &&
($user->can(H5PPermissionsEnum::H5P_DELETE) ||
($user->can(H5PPermissionsEnum::H5P_AUTHOR_DELETE) && $h5PContent->user_id == $user->getKey()));
}

public function update(?User $user, H5PContent $h5PContent ): bool
public function update(?User $user, H5PContent $h5PContent): bool
{
if ($user && $user->can(H5PPermissionsEnum::H5P_AUTHOR_UPDATE) && !$user->can(H5PPermissionsEnum::H5P_UPDATE)) {
return $h5PContent->user_id == $user->getKey();
Expand Down
21 changes: 21 additions & 0 deletions tests/Api/ContentApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,27 @@ public function testContentDelete(): void
$response->assertStatus(404);
}

public function testContentDeleteByAuthor(): void
{
$this->authenticateAsUser();
$this->user->givePermissionTo(H5PPermissionsEnum::H5P_AUTHOR_DELETE);

$library = H5PLibrary::factory()->create(['runnable' => 1]);
$content = H5PContent::factory()->create([
'user_id' => $this->user->getKey(),
'library_id' => $library->getKey()
]);

$this->actingAs($this->user, 'api')
->delete('/api/admin/hh5p/content/' . $content->getKey())
->assertStatus(200);

$otherContent = H5PContent::factory()->create(['library_id' => $library->getKey()]);
$this->actingAs($this->user, 'api')
->delete('/api/admin/hh5p/content/' . $otherContent->getKey())
->assertForbidden();
}

public function testContentShow(): void
{
$this->authenticateAsAdmin();
Expand Down

0 comments on commit 357a1df

Please sign in to comment.