Skip to content

Commit 17349df

Browse files
Alena ProkharchykAlena Prokharchyk
authored andcommitted
bug 13695: deleteAccount - remove account from projects as a part of account cleanup process
status 13695: resolved fixed Reviewed-by: Frank
1 parent 67f074e commit 17349df

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎server/src/com/cloud/projects/dao/ProjectAccountDao.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public interface ProjectAccountDao extends GenericDao<ProjectAccountVO, Long> {
3636

3737
List<Long> listPermittedAccountIds(long accountId);
3838

39-
List<Long> listAdministratedProjects(long adminAccountId);
39+
List<Long> listAdministratedProjectIds(long adminAccountId);
4040

4141
Long countByAccountIdAndRole(long accountId, ProjectAccount.Role role);
42+
43+
void removeAccountFromProjects(long accountId);
4244
}

‎server/src/com/cloud/projects/dao/ProjectAccountDaoImpl.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import javax.ejb.Local;
2323

24+
import org.apache.log4j.Logger;
25+
2426
import com.cloud.projects.ProjectAccount;
2527
import com.cloud.projects.ProjectAccountVO;
2628
import com.cloud.utils.db.GenericDaoBase;
@@ -36,6 +38,7 @@ public class ProjectAccountDaoImpl extends GenericDaoBase<ProjectAccountVO, Long
3638
final GenericSearchBuilder<ProjectAccountVO, Long> AdminSearch;
3739
final GenericSearchBuilder<ProjectAccountVO, Long> ProjectAccountSearch;
3840
final GenericSearchBuilder<ProjectAccountVO, Long> CountByRoleSearch;
41+
public static final Logger s_logger = Logger.getLogger(ProjectAccountDaoImpl.class.getName());
3942

4043
protected ProjectAccountDaoImpl() {
4144
AllFieldsSearch = createSearchBuilder();
@@ -124,7 +127,7 @@ public List<Long> listPermittedAccountIds(long accountId) {
124127
}
125128

126129
@Override
127-
public List<Long> listAdministratedProjects(long adminAccountId) {
130+
public List<Long> listAdministratedProjectIds(long adminAccountId) {
128131
SearchCriteria<Long> sc = AdminSearch.create();
129132
sc.setParameters("role", ProjectAccount.Role.Admin);
130133
sc.setParameters("accountId", adminAccountId);
@@ -138,5 +141,16 @@ public Long countByAccountIdAndRole(long accountId, ProjectAccount.Role role) {
138141
sc.setParameters("role", role);
139142
return customSearch(sc, null).get(0);
140143
}
144+
145+
@Override
146+
public void removeAccountFromProjects(long accountId) {
147+
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();
148+
sc.setParameters("accountId", accountId);
149+
150+
int rowsRemoved = remove(sc);
151+
if (rowsRemoved > 0) {
152+
s_logger.debug("Removed account id=" + accountId + " from " + rowsRemoved + " projects");
153+
}
154+
}
141155

142156
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,20 +445,15 @@ public boolean lockAccount(long accountId) {
445445
@Override
446446
public boolean deleteAccount(AccountVO account, long callerUserId, Account caller) {
447447
long accountId = account.getId();
448-
448+
449+
//delete the account record
449450
if (!_accountDao.remove(accountId)) {
450451
s_logger.error("Unable to delete account " + accountId);
451452
return false;
452453
}
453454

454-
List<UserVO> users = _userDao.listByAccount(accountId);
455-
456-
for (UserVO user : users) {
457-
_userDao.remove(user.getId());
458-
}
459-
460455
if (s_logger.isDebugEnabled()) {
461-
s_logger.debug("Remove account " + accountId);
456+
s_logger.debug("Removed account " + accountId);
462457
}
463458

464459
return cleanupAccount(account, callerUserId, caller);
@@ -468,8 +463,20 @@ public boolean deleteAccount(AccountVO account, long callerUserId, Account calle
468463
public boolean cleanupAccount(AccountVO account, long callerUserId, Account caller) {
469464
long accountId = account.getId();
470465
boolean accountCleanupNeeded = false;
471-
466+
472467
try {
468+
//cleanup the users from the account
469+
List<UserVO> users = _userDao.listByAccount(accountId);
470+
for (UserVO user : users) {
471+
if (!_userDao.remove(user.getId())) {
472+
s_logger.error("Unable to delete user: " + user + " as a part of account " + account + " cleanup");
473+
accountCleanupNeeded = true;
474+
}
475+
}
476+
477+
//delete the account from project accounts
478+
_projectAccountDao.removeAccountFromProjects(accountId);
479+
473480
// delete all vm groups belonging to accont
474481
List<InstanceGroupVO> groups = _vmGroupDao.listByAccountId(accountId);
475482
for (InstanceGroupVO group : groups) {
@@ -582,7 +589,7 @@ public boolean cleanupAccount(AccountVO account, long callerUserId, Account call
582589
}
583590

584591
// delete account specific Virtual vlans (belong to system Public Network) - only when networks are cleaned
585-
// up
592+
// up
586593
// successfully
587594
if (networksDeleted) {
588595
if (!_configMgr.deleteAccountSpecificVirtualRanges(accountId)) {
@@ -1010,7 +1017,7 @@ public boolean deleteUserAccount(long accountId) {
10101017
}
10111018

10121019
// Account that manages project(s) can't be removed
1013-
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjects(accountId);
1020+
List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
10141021
if (!managedProjectIds.isEmpty()) {
10151022
StringBuilder projectIds = new StringBuilder();
10161023
for (Long projectId : managedProjectIds) {

0 commit comments

Comments
 (0)