mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-17 20:52:14 +01:00
Restrict removing owner from organization
This commit is contained in:
10
app/Exceptions/Api/CanNotRemoveOwnerFromOrganization.php
Normal file
10
app/Exceptions/Api/CanNotRemoveOwnerFromOrganization.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Exceptions\Api;
|
||||||
|
|
||||||
|
class CanNotRemoveOwnerFromOrganization extends ApiException
|
||||||
|
{
|
||||||
|
public const string KEY = 'can_not_remove_owner_from_organization';
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Http\Controllers\Api\V1;
|
namespace App\Http\Controllers\Api\V1;
|
||||||
|
|
||||||
use App\Enums\Role;
|
use App\Enums\Role;
|
||||||
|
use App\Exceptions\Api\CanNotRemoveOwnerFromOrganization;
|
||||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||||
use App\Exceptions\Api\UserNotPlaceholderApiException;
|
use App\Exceptions\Api\UserNotPlaceholderApiException;
|
||||||
use App\Http\Requests\V1\Member\MemberIndexRequest;
|
use App\Http\Requests\V1\Member\MemberIndexRequest;
|
||||||
@@ -72,7 +73,7 @@ class MemberController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Remove a member of the organization.
|
* Remove a member of the organization.
|
||||||
*
|
*
|
||||||
* @throws AuthorizationException|EntityStillInUseApiException
|
* @throws AuthorizationException|EntityStillInUseApiException|CanNotRemoveOwnerFromOrganization
|
||||||
*
|
*
|
||||||
* @operationId removeMember
|
* @operationId removeMember
|
||||||
*/
|
*/
|
||||||
@@ -86,6 +87,9 @@ class MemberController extends Controller
|
|||||||
if (ProjectMember::query()->whereBelongsToOrganization($organization)->where('user_id', $member->user_id)->exists()) {
|
if (ProjectMember::query()->whereBelongsToOrganization($organization)->where('user_id', $member->user_id)->exists()) {
|
||||||
throw new EntityStillInUseApiException('member', 'project_member');
|
throw new EntityStillInUseApiException('member', 'project_member');
|
||||||
}
|
}
|
||||||
|
if ($member->role === Role::Owner->value) {
|
||||||
|
throw new CanNotRemoveOwnerFromOrganization();
|
||||||
|
}
|
||||||
|
|
||||||
$member->delete();
|
$member->delete();
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use App\Exceptions\Api\CanNotRemoveOwnerFromOrganization;
|
||||||
use App\Exceptions\Api\EntityStillInUseApiException;
|
use App\Exceptions\Api\EntityStillInUseApiException;
|
||||||
use App\Exceptions\Api\InactiveUserCanNotBeUsedApiException;
|
use App\Exceptions\Api\InactiveUserCanNotBeUsedApiException;
|
||||||
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
|
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
|
||||||
@@ -17,5 +18,6 @@ return [
|
|||||||
InactiveUserCanNotBeUsedApiException::KEY => 'Inactive user can not be used',
|
InactiveUserCanNotBeUsedApiException::KEY => 'Inactive user can not be used',
|
||||||
UserIsAlreadyMemberOfProjectApiException::KEY => 'User is already a member of the project',
|
UserIsAlreadyMemberOfProjectApiException::KEY => 'User is already a member of the project',
|
||||||
EntityStillInUseApiException::KEY => 'The :modelToDelete is still used by a :modelInUse and can not be deleted.',
|
EntityStillInUseApiException::KEY => 'The :modelToDelete is still used by a :modelInUse and can not be deleted.',
|
||||||
|
CanNotRemoveOwnerFromOrganization::KEY => 'Can not remove owner from organization',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -141,6 +141,23 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
|||||||
$response->assertStatus(403);
|
$response->assertStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_destroy_member_fails_if_member_is_owner(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$data = $this->createUserWithPermission([
|
||||||
|
'members:delete',
|
||||||
|
]);
|
||||||
|
$memberToDelete = Member::factory()->forOrganization($data->organization)->role(Role::Owner)->create();
|
||||||
|
Passport::actingAs($data->user);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$response = $this->deleteJson(route('api.v1.members.destroy', [$data->organization->getKey(), $memberToDelete->getKey()]));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$response->assertStatus(400);
|
||||||
|
$response->assertJsonPath('message', 'Can not remove owner from organization');
|
||||||
|
}
|
||||||
|
|
||||||
public function test_destroy_member_fails_if_member_is_not_part_of_org(): void
|
public function test_destroy_member_fails_if_member_is_not_part_of_org(): void
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace Tests\Unit\Service\Import;
|
|||||||
use App\Models\Organization;
|
use App\Models\Organization;
|
||||||
use App\Service\Import\ImportService;
|
use App\Service\Import\ImportService;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class ImportServiceTest extends TestCase
|
class ImportServiceTest extends TestCase
|
||||||
@@ -16,6 +17,7 @@ class ImportServiceTest extends TestCase
|
|||||||
public function test_import_gets_importer_from_provider_runs_importer_and_returns_report(): void
|
public function test_import_gets_importer_from_provider_runs_importer_and_returns_report(): void
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
|
Storage::fake('s3');
|
||||||
$organization = Organization::factory()->create();
|
$organization = Organization::factory()->create();
|
||||||
$data = file_get_contents(storage_path('tests/toggl_time_entries_import_test_1.csv'));
|
$data = file_get_contents(storage_path('tests/toggl_time_entries_import_test_1.csv'));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user