Skip to content

Commit

Permalink
prevent non admin users changing admin signatures (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored Dec 8, 2023
1 parent c1a9cc3 commit f608268
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Access/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class UserPolicy extends AbstractPolicy
{
public function editSignature(User $actor, User $user)
{
if ($user->isAdmin() && !$actor->isAdmin()) {
return $this->deny();
}

if ($actor->id === $user->id || $actor->can('user.editSignature')) {
return $this->allow();
}
Expand Down
38 changes: 36 additions & 2 deletions tests/integration/api/UserAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public function setUp(): void
$this->prepareDatabase([
'users' => [
$this->normalUser(),
['id' => 3, 'username' => 'moderator', 'email' => '[email protected]', 'is_email_confirmed' => true]
['id' => 3, 'username' => 'moderator', 'email' => '[email protected]', 'is_email_confirmed' => true],
['id' => 4, 'username' => 'admin2', 'email' => '[email protected]', 'is_email_confirmed' => true],
],
'group_user' => [
['user_id' => 3, 'group_id' => 4]
['user_id' => 3, 'group_id' => 4],
['user_id' => 4, 'group_id' => 1],
],
'group_permission' => [
['group_id' => 4, 'permission' => 'user.editSignature']
Expand Down Expand Up @@ -92,4 +94,36 @@ public function user_with_permission_can_edit_others_signature()

$this->assertTrue($json['data']['attributes']['canEditSignature']);
}

/**
* @test
*/
public function user_with_permission_cannot_edit_admin_signature()
{
$response = $this->send(
$this->request('GET', '/api/users/1', ['authenticatedAs' => 3])
);

$this->assertEquals(200, $response->getStatusCode());

$json = json_decode($response->getBody(), true);

$this->assertFalse($json['data']['attributes']['canEditSignature']);
}

/**
* @test
*/
public function admin_can_edit_admin_signature()
{
$response = $this->send(
$this->request('GET', '/api/users/1', ['authenticatedAs' => 4])
);

$this->assertEquals(200, $response->getStatusCode());

$json = json_decode($response->getBody(), true);

$this->assertTrue($json['data']['attributes']['canEditSignature']);
}
}

0 comments on commit f608268

Please sign in to comment.