Skip to content

Commit a331157

Browse files
check whether account has network permissions before attempting to delete it
1 parent afc95f1 commit a331157

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.apache.cloudstack.framework.messagebus.MessageBus;
7575
import org.apache.cloudstack.framework.messagebus.PublishScope;
7676
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
77+
import org.apache.cloudstack.network.dao.NetworkPermissionDao;
7778
import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao;
7879
import org.apache.cloudstack.resourcedetail.UserDetailVO;
7980
import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao;
@@ -298,6 +299,8 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
298299
private SSHKeyPairDao _sshKeyPairDao;
299300
@Inject
300301
private UserDataDao userDataDao;
302+
@Inject
303+
private NetworkPermissionDao networkPermissionDao;
301304

302305
private List<QuerySelector> _querySelectors;
303306

@@ -1857,26 +1860,38 @@ public boolean deleteUserAccount(long accountId) {
18571860
// If the user is a System user, return an error. We do not allow this
18581861
AccountVO account = _accountDao.findById(accountId);
18591862

1860-
if (! isDeleteNeeded(account, accountId, caller)) {
1863+
if (!isDeleteNeeded(account, accountId, caller)) {
18611864
return true;
18621865
}
18631866

1864-
// Account that manages project(s) can't be removed
1865-
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
1866-
if (!managedProjectIds.isEmpty()) {
1867-
StringBuilder projectIds = new StringBuilder();
1868-
for (Long projectId : managedProjectIds) {
1869-
projectIds.append(projectId).append(", ");
1870-
}
1871-
1872-
throw new InvalidParameterValueException("The account id=" + accountId + " manages project(s) with ids " + projectIds + "and can't be removed");
1873-
}
1867+
checkIfAccountManagesProjects(accountId);
1868+
checkIfAccountHasNetworkPermissions(accountId);
18741869

18751870
CallContext.current().putContextParameter(Account.class, account.getUuid());
18761871

18771872
return deleteAccount(account, callerUserId, caller);
18781873
}
18791874

1875+
protected void checkIfAccountManagesProjects(long accountId) {
1876+
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
1877+
if (!CollectionUtils.isEmpty(managedProjectIds)) {
1878+
throw new InvalidParameterValueException(String.format(
1879+
"Unable to delete account [%s], because it manages the following project(s): %s. Please, remove the account from these projects first.",
1880+
accountId, managedProjectIds
1881+
));
1882+
}
1883+
}
1884+
1885+
protected void checkIfAccountHasNetworkPermissions(long accountId) {
1886+
List<Long> networkIds = networkPermissionDao.listPermittedNetworkIdsByAccounts(List.of(accountId));
1887+
if (!CollectionUtils.isEmpty(networkIds)) {
1888+
throw new InvalidParameterValueException(String.format(
1889+
"Unable to delete account [%s], because it has network permissions for the following network(s): %s. Please, remove the network permissions first.",
1890+
accountId, networkIds
1891+
));
1892+
}
1893+
}
1894+
18801895
private boolean isDeleteNeeded(AccountVO account, long accountId, Account caller) {
18811896
if (account == null) {
18821897
s_logger.info(String.format("The account, identified by id %d, doesn't exist", accountId ));

server/src/test/java/com/cloud/user/AccountManagerImplTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,4 +1200,40 @@ public void testValidateRoleAdminCannotEscalateAdminFromNonRootDomain() {
12001200
Mockito.when(roleService.findRole(2L)).thenReturn(callerRole);
12011201
accountManagerImpl.validateRoleChange(account, newRole, caller);
12021202
}
1203+
1204+
@Test
1205+
public void checkIfAccountManagesProjectsTestNotThrowExceptionWhenTheAccountIsNotAProjectAdministrator() {
1206+
long accountId = 1L;
1207+
List<Long> managedProjectIds = new ArrayList<>();
1208+
1209+
Mockito.when(_projectAccountDao.listAdministratedProjectIds(accountId)).thenReturn(managedProjectIds);
1210+
accountManagerImpl.checkIfAccountManagesProjects(accountId);
1211+
}
1212+
1213+
@Test(expected = InvalidParameterValueException.class)
1214+
public void checkIfAccountHasNetworkPermissionsTestThrowExceptionWhenTheAccountHasNetworkPermissions() {
1215+
long accountId = 1L;
1216+
List<Long> networkIds = List.of(1L);
1217+
1218+
Mockito.when(networkPermissionDaoMock.listPermittedNetworkIdsByAccounts(List.of(accountId))).thenReturn(networkIds);
1219+
accountManagerImpl.checkIfAccountHasNetworkPermissions(accountId);
1220+
}
1221+
1222+
@Test
1223+
public void checkIfAccountHasNetworkPermissionsTestNotThrowExceptionWhenTheAccountDoesNotHaveNetworkPermissions() {
1224+
long accountId = 1L;
1225+
List<Long> networkIds = new ArrayList<>();
1226+
1227+
Mockito.when(networkPermissionDaoMock.listPermittedNetworkIdsByAccounts(List.of(accountId))).thenReturn(networkIds);
1228+
accountManagerImpl.checkIfAccountHasNetworkPermissions(accountId);
1229+
}
1230+
1231+
@Test(expected = InvalidParameterValueException.class)
1232+
public void checkIfAccountManagesProjectsTestThrowExceptionWhenTheAccountIsAProjectAdministrator() {
1233+
long accountId = 1L;
1234+
List<Long> managedProjectIds = List.of(1L);
1235+
1236+
Mockito.when(_projectAccountDao.listAdministratedProjectIds(accountId)).thenReturn(managedProjectIds);
1237+
accountManagerImpl.checkIfAccountManagesProjects(accountId);
1238+
}
12031239
}

server/src/test/java/com/cloud/user/AccountManagetImplTestBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.apache.cloudstack.engine.service.api.OrchestrationService;
6666
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
6767
import org.apache.cloudstack.framework.messagebus.MessageBus;
68+
import org.apache.cloudstack.network.dao.NetworkPermissionDao;
6869
import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao;
6970
import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao;
7071
import org.junit.After;
@@ -195,6 +196,8 @@ public class AccountManagetImplTestBase {
195196
SSHKeyPairDao _sshKeyPairDao;
196197
@Mock
197198
UserDataDao userDataDao;
199+
@Mock
200+
NetworkPermissionDao networkPermissionDaoMock;
198201

199202
@Spy
200203
@InjectMocks

0 commit comments

Comments
 (0)