Skip to content

Commit e63e07a

Browse files
clean up network permissions when an account is deleted
1 parent a331157 commit e63e07a

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
lines changed

engine/schema/src/main/java/org/apache/cloudstack/network/dao/NetworkPermissionDao.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public interface NetworkPermissionDao extends GenericDao<NetworkPermissionVO, Lo
4040
*/
4141
void removeAllPermissions(long networkId);
4242

43+
/**
44+
* Removes all network permissions associated with a given account.
45+
*
46+
* @param accountId The ID of the account from which all network permissions will be removed.
47+
*/
48+
void removeAccountPermissions(long accountId);
49+
4350
/**
4451
* Find a Network permission by networkId, accountName, and domainId
4552
*

engine/schema/src/main/java/org/apache/cloudstack/network/dao/NetworkPermissionDaoImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class NetworkPermissionDaoImpl extends GenericDaoBase<NetworkPermissionVO
3535

3636
private SearchBuilder<NetworkPermissionVO> NetworkAndAccountSearch;
3737
private SearchBuilder<NetworkPermissionVO> NetworkIdSearch;
38+
private SearchBuilder<NetworkPermissionVO> accountSearch;
3839
private GenericSearchBuilder<NetworkPermissionVO, Long> FindNetworkIdsByAccount;
3940

4041
protected NetworkPermissionDaoImpl() {
@@ -47,6 +48,10 @@ protected NetworkPermissionDaoImpl() {
4748
NetworkIdSearch.and("networkId", NetworkIdSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
4849
NetworkIdSearch.done();
4950

51+
accountSearch = createSearchBuilder();
52+
accountSearch.and("accountId", accountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
53+
accountSearch.done();
54+
5055
FindNetworkIdsByAccount = createSearchBuilder(Long.class);
5156
FindNetworkIdsByAccount.select(null, SearchCriteria.Func.DISTINCT, FindNetworkIdsByAccount.entity().getNetworkId());
5257
FindNetworkIdsByAccount.and("account", FindNetworkIdsByAccount.entity().getAccountId(), SearchCriteria.Op.IN);
@@ -71,6 +76,16 @@ public void removeAllPermissions(long networkId) {
7176
expunge(sc);
7277
}
7378

79+
@Override
80+
public void removeAccountPermissions(long accountId) {
81+
SearchCriteria<NetworkPermissionVO> sc = accountSearch.create();
82+
sc.setParameters("accountId", accountId);
83+
int networkPermissionRemoved = expunge(sc);
84+
if (networkPermissionRemoved > 0) {
85+
s_logger.debug(String.format("Removed [%s] network permission(s) for the account with Id [%s]", networkPermissionRemoved, accountId));
86+
}
87+
}
88+
7489
@Override
7590
public NetworkPermissionVO findByNetworkAndAccount(long networkId, long accountId) {
7691
SearchCriteria<NetworkPermissionVO> sc = NetworkAndAccountSearch.create();

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,9 @@ protected boolean cleanupAccount(AccountVO account, long callerUserId, Account c
873873
// delete the account from project accounts
874874
_projectAccountDao.removeAccountFromProjects(accountId);
875875

876+
// Delete account's network permissions
877+
networkPermissionDao.removeAccountPermissions(accountId);
878+
876879
if (account.getType() != Account.Type.PROJECT) {
877880
// delete the account from group
878881
_messageBus.publish(_name, MESSAGE_REMOVE_ACCOUNT_EVENT, PublishScope.LOCAL, accountId);
@@ -1865,7 +1868,6 @@ public boolean deleteUserAccount(long accountId) {
18651868
}
18661869

18671870
checkIfAccountManagesProjects(accountId);
1868-
checkIfAccountHasNetworkPermissions(accountId);
18691871

18701872
CallContext.current().putContextParameter(Account.class, account.getUuid());
18711873

@@ -1876,22 +1878,12 @@ protected void checkIfAccountManagesProjects(long accountId) {
18761878
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
18771879
if (!CollectionUtils.isEmpty(managedProjectIds)) {
18781880
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.",
1881+
"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.",
18801882
accountId, managedProjectIds
18811883
));
18821884
}
18831885
}
18841886

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-
18951887
private boolean isDeleteNeeded(AccountVO account, long accountId, Account caller) {
18961888
if (account == null) {
18971889
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: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,24 +1210,6 @@ public void checkIfAccountManagesProjectsTestNotThrowExceptionWhenTheAccountIsNo
12101210
accountManagerImpl.checkIfAccountManagesProjects(accountId);
12111211
}
12121212

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-
12311213
@Test(expected = InvalidParameterValueException.class)
12321214
public void checkIfAccountManagesProjectsTestThrowExceptionWhenTheAccountIsAProjectAdministrator() {
12331215
long accountId = 1L;

0 commit comments

Comments
 (0)