Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ public void execute() {
CallContext.current().setEventDetails("Account ID: " + (account != null ? account.getUuid() : getId())); // Account not found is already handled by service

boolean result = _regionService.deleteUserAccount(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
setResponseObject(response);
} else {
if (!result) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete user account and all corresponding users");
}
SuccessResponse response = new SuccessResponse(getCommandName());
setResponseObject(response);
}

@Override
Expand Down
11 changes: 9 additions & 2 deletions server/src/main/java/com/cloud/user/AccountManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,14 @@ public boolean deleteUserAccount(long accountId) {
// If the user is a System user, return an error. We do not allow this
AccountVO account = _accountDao.findById(accountId);

if (! isDeleteNeeded(account, accountId, caller)) {
if (caller.getId() == accountId) {
Domain domain = _domainDao.findById(account.getDomainId());
throw new InvalidParameterValueException(String.format("Deletion of your own account is not allowed. To delete account %s (ID: %s, Domain: %s), " +
"request to another user with permissions to perform the operation.",
account.getAccountName(), account.getUuid(), domain.getUuid()));
}

if (!isDeleteNeeded(account, accountId, caller)) {
return true;
}

Expand All @@ -1835,7 +1842,7 @@ public boolean deleteUserAccount(long accountId) {
return deleteAccount(account, callerUserId, caller);
}

private boolean isDeleteNeeded(AccountVO account, long accountId, Account caller) {
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 ));
return false;
Expand Down
32 changes: 32 additions & 0 deletions server/src/test/java/com/cloud/user/AccountManagerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,38 @@ public void deleteUserAccountCleanup() {
Mockito.verify(_accountDao, Mockito.atLeastOnce()).markForCleanup(Mockito.eq(42l));
}

@Test (expected = InvalidParameterValueException.class)
public void deleteUserAccountTestIfAccountIdIsEqualToCallerIdShouldThrowException() {
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
CallContext callContextMock = Mockito.mock(CallContext.class);
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
long accountId = 1L;

Mockito.doReturn(accountVoMock).when(callContextMock).getCallingAccount();
Mockito.doReturn(accountVoMock).when(_accountDao).findById(Mockito.anyLong());
Mockito.doReturn(domainVoMock).when(_domainDao).findById(Mockito.anyLong());
Mockito.doReturn(1L).when(accountVoMock).getId();

accountManagerImpl.deleteUserAccount(accountId);
}
}
@Test
Comment thread
lucas-a-martins marked this conversation as resolved.
public void deleteUserAccountTestIfAccountIdIsNotEqualToCallerAccountIdShouldNotThrowException() {
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)){
Comment thread
lucas-a-martins marked this conversation as resolved.
Outdated
CallContext callContextMock = Mockito.mock(CallContext.class);
callContextMocked.when((CallContext::current)).thenReturn(callContextMock);
Comment thread
lucas-a-martins marked this conversation as resolved.
Outdated
long accountId = 1L;

Mockito.doReturn(accountVoMock).when(callContextMock).getCallingAccount();
Mockito.doReturn(accountVoMock).when(_accountDao).findById(Mockito.anyLong());
Mockito.doReturn(2L).when(accountVoMock).getId();
Mockito.doReturn(true).when(accountManagerImpl).isDeleteNeeded(Mockito.any(), Mockito.anyLong(), Mockito.any());
Mockito.doReturn(new ArrayList<Long>()).when(_projectAccountDao).listAdministratedProjectIds(Mockito.anyLong());

accountManagerImpl.deleteUserAccount(accountId);
}
}

@Test (expected = InvalidParameterValueException.class)
public void deleteUserTestIfUserIdIsEqualToCallerIdShouldThrowException() {
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
Expand Down