|
74 | 74 | import org.apache.cloudstack.framework.messagebus.MessageBus; |
75 | 75 | import org.apache.cloudstack.framework.messagebus.PublishScope; |
76 | 76 | import org.apache.cloudstack.managed.context.ManagedContextRunnable; |
| 77 | +import org.apache.cloudstack.network.dao.NetworkPermissionDao; |
77 | 78 | import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao; |
78 | 79 | import org.apache.cloudstack.resourcedetail.UserDetailVO; |
79 | 80 | import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao; |
@@ -298,6 +299,8 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M |
298 | 299 | private SSHKeyPairDao _sshKeyPairDao; |
299 | 300 | @Inject |
300 | 301 | private UserDataDao userDataDao; |
| 302 | + @Inject |
| 303 | + private NetworkPermissionDao networkPermissionDao; |
301 | 304 |
|
302 | 305 | private List<QuerySelector> _querySelectors; |
303 | 306 |
|
@@ -1857,26 +1860,38 @@ public boolean deleteUserAccount(long accountId) { |
1857 | 1860 | // If the user is a System user, return an error. We do not allow this |
1858 | 1861 | AccountVO account = _accountDao.findById(accountId); |
1859 | 1862 |
|
1860 | | - if (! isDeleteNeeded(account, accountId, caller)) { |
| 1863 | + if (!isDeleteNeeded(account, accountId, caller)) { |
1861 | 1864 | return true; |
1862 | 1865 | } |
1863 | 1866 |
|
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); |
1874 | 1869 |
|
1875 | 1870 | CallContext.current().putContextParameter(Account.class, account.getUuid()); |
1876 | 1871 |
|
1877 | 1872 | return deleteAccount(account, callerUserId, caller); |
1878 | 1873 | } |
1879 | 1874 |
|
| 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 | + |
1880 | 1895 | private boolean isDeleteNeeded(AccountVO account, long accountId, Account caller) { |
1881 | 1896 | if (account == null) { |
1882 | 1897 | s_logger.info(String.format("The account, identified by id %d, doesn't exist", accountId )); |
|
0 commit comments