diff --git a/app/Enums/ExceptionsEnums/Exceptions.php b/app/Enums/ExceptionsEnums/Exceptions.php index 2cf1b634f..e8f73f99c 100644 --- a/app/Enums/ExceptionsEnums/Exceptions.php +++ b/app/Enums/ExceptionsEnums/Exceptions.php @@ -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 { diff --git a/app/Services/RoleService.php b/app/Services/RoleService.php index 6c9676734..4116e3427 100644 --- a/app/Services/RoleService.php +++ b/app/Services/RoleService.php @@ -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); } diff --git a/tests/Feature/RoleTest.php b/tests/Feature/RoleTest.php index 734887fbd..c41e15fdb 100644 --- a/tests/Feature/RoleTest.php +++ b/tests/Feature/RoleTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\Enums\ExceptionsEnums\Exceptions; use App\Enums\RoleType; use App\Models\Permission; use App\Models\Role; @@ -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 */