Skip to content

Commit

Permalink
Only regular roles joinable
Browse files Browse the repository at this point in the history
  • Loading branch information
Witold Wiśniewski committed Sep 21, 2023
1 parent 9b066f6 commit d56f873
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Enums/ExceptionsEnums/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ final class Exceptions extends Enum
public const CLIENT_PROVIDER_MERGE_TOKEN_MISMATCH =
'Provider merge token is for an account with different email address';
public const CLIENT_JOINING_NON_JOINABLE_ROLE = 'Can\'t join to a non joinable role';
public const CLIENT_UPDATE_NOT_REGULAR_JOINABLE = 'Can\'t update is_joinable field in role types other than regular';

public static function getCode(string $value): int
{
Expand Down
8 changes: 8 additions & 0 deletions app/Services/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function update(Role $role, RoleUpdateDto $dto): Role
{
$user = Auth::user();

if (
!($dto->getIsJoinable() instanceof Missing)
&& $role->type->isNot(RoleType::REGULAR)
&& $dto->getIsJoinable() !== $role->is_joinable
) {
throw new ClientException(Exceptions::CLIENT_UPDATE_NOT_REGULAR_JOINABLE);
}

if (!$user?->hasAllPermissions($role->getAllPermissions())) {
throw new ClientException(Exceptions::CLIENT_UPDATE_ROLE_WITHOUT_PERMISSION);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Feature/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Enums\ExceptionsEnums\Exceptions;
use App\Enums\RoleType;
use App\Models\Permission;
use App\Models\Role;
Expand Down Expand Up @@ -1073,6 +1074,35 @@ public function testUpdateToRegistrationRole($user): void
]);
}

/**
* @dataProvider authProvider
*/
public function testUpdateOwnerIsJoinable($user): void
{
$this->{$user}->givePermissionTo('roles.edit');

$role = Role::create([
'name' => 'role1',
'description' => 'Role 1',
'is_joinable' => false,
]);
$role->type = RoleType::OWNER;
$role->save();

$this
->actingAs($this->{$user})
->patchJson('/roles/id:' . $role->getKey(), [
'name' => 'test_role',
'is_joinable' => true,
])
->assertUnprocessable()
->assertJsonFragment([
'key' => Exceptions::coerce(Exceptions::CLIENT_UPDATE_NOT_REGULAR_JOINABLE)->key,
])->assertJsonFragment([
'message' => Exceptions::CLIENT_UPDATE_NOT_REGULAR_JOINABLE,
]);
}

/**
* @dataProvider authProvider
*/
Expand Down

0 comments on commit d56f873

Please sign in to comment.