This commit is contained in:
Surya Srinivasan 2026-03-09 13:14:43 +00:00 committed by GitHub
commit 75a8c3603e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 7 deletions

View File

@ -2158,16 +2158,33 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
return deleteAccount(account, callerUserId, caller);
}
protected void checkIfAccountManagesProjects(long accountId) {
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
if (!CollectionUtils.isEmpty(managedProjectIds)) {
throw new InvalidParameterValueException(String.format(
"Unable to delete account [%s], because it manages the following project(s): %s. Please, remove the account from these projects or demote it to a regular project role first.",
accountId, managedProjectIds
));
protected void checkIfAccountManagesProjects(long accountId) {
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
if (CollectionUtils.isEmpty(managedProjectIds)) {
return;
}
List<Long> activeManagedProjects = new ArrayList<>();
for (Long projectId : managedProjectIds) {
ProjectVO project = _projectDao.findById(projectId);
if (project != null && project.getRemoved() == null) {
activeManagedProjects.add(projectId);
}
}
if (!activeManagedProjects.isEmpty()) {
throw new InvalidParameterValueException(
String.format(
"Unable to delete account [%s], because it manages the following project(s): %s. Please, remove the account from these projects or demote it to a regular project role first.",
accountId, activeManagedProjects
)
);
}
}
protected boolean isDeleteNeeded(AccountVO account, long accountId, Account caller) {
if (account == null) {
logger.info(String.format("The account, identified by id %d, doesn't exist", accountId ));

View File

@ -27,6 +27,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.acl.Role;
@ -74,6 +76,7 @@ import com.cloud.vm.UserVmManagerImpl;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.snapshot.VMSnapshotVO;
import com.cloud.projects.ProjectVO;
public class AccountManagerImplTest extends AccountManagentImplTestBase {
@ -1624,6 +1627,23 @@ public class AccountManagerImplTest extends AccountManagentImplTestBase {
accountManagerImpl.checkCallerApiPermissionsForUserOrAccountOperations(accountMock);
}
@Test
public void testCheckIfAccountManagesOnlyDeletedProjectsDoesNotThrow() {
long accountId = 42L;
long projectId = 100L;
Mockito.when(projectAccountDao.listAdministratedProjectIds(accountId))
.thenReturn(List.of(projectId));
ProjectVO deletedProject = Mockito.mock(ProjectVO.class);
Mockito.when(deletedProject.getRemoved()).thenReturn(new Date());
Mockito.when(projectDao.findById(projectId))
.thenReturn(deletedProject);
accountManager.checkIfAccountManagesProjects(accountId);
}
@Test(expected = InvalidParameterValueException.class)
public void testPasswordChangeRequiredWithSamlThrowsException() {
accountManagerImpl.createUser(