Skip to content

Commit ef1a58d

Browse files
Remove user from project before deletion (#10008)
Co-authored-by: Suresh Kumar Anaparti <[email protected]>
1 parent 7cfeab1 commit ef1a58d

File tree

8 files changed

+143
-1
lines changed

8 files changed

+143
-1
lines changed

engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public interface ProjectAccountDao extends GenericDao<ProjectAccountVO, Long> {
4747

4848
void removeAccountFromProjects(long accountId);
4949

50+
void removeUserFromProjects(long userId);
51+
5052
boolean canUserModifyProject(long projectId, long accountId, long userId);
5153

5254
List<ProjectAccountVO> listUsersOrAccountsByRole(long id);

engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDaoImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ public void removeAccountFromProjects(long accountId) {
194194
}
195195
}
196196

197+
@Override
198+
public void removeUserFromProjects(long userId) {
199+
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();
200+
sc.setParameters("userId", userId);
201+
202+
int removedCount = remove(sc);
203+
if (removedCount > 0) {
204+
s_logger.debug(String.format("Removed user [%s] from %s project(s).", userId, removedCount));
205+
}
206+
}
207+
197208
@Override
198209
public boolean canUserModifyProject(long projectId, long accountId, long userId) {
199210
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();

engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import javax.inject.Inject;
3535

36+
import com.cloud.upgrade.dao.Upgrade41910to41920;
3637
import com.cloud.utils.FileUtil;
3738
import org.apache.cloudstack.utils.CloudStackVersion;
3839
import org.apache.commons.lang3.StringUtils;
@@ -227,6 +228,7 @@ public DatabaseUpgradeChecker() {
227228
.next("4.18.0.0", new Upgrade41800to41810())
228229
.next("4.18.1.0", new Upgrade41810to41900())
229230
.next("4.19.0.0", new Upgrade41900to41910())
231+
.next("4.19.1.0", new Upgrade41910to41920())
230232
.build();
231233
}
232234

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.upgrade.dao;
18+
19+
import com.cloud.utils.exception.CloudRuntimeException;
20+
21+
import java.io.InputStream;
22+
import java.sql.Connection;
23+
24+
public class Upgrade41910to41920 implements DbUpgrade {
25+
26+
@Override
27+
public String[] getUpgradableVersionRange() {
28+
return new String[]{"4.19.1.0", "4.19.2.0"};
29+
}
30+
31+
@Override
32+
public String getUpgradedVersion() {
33+
return "4.19.2.0";
34+
}
35+
36+
@Override
37+
public boolean supportsRollingUpgrade() {
38+
return false;
39+
}
40+
41+
@Override
42+
public InputStream[] getPrepareScripts() {
43+
final String scriptFile = "META-INF/db/schema-41910to41920.sql";
44+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
45+
if (script == null) {
46+
throw new CloudRuntimeException("Unable to find " + scriptFile);
47+
}
48+
49+
return new InputStream[]{script};
50+
}
51+
52+
@Override
53+
public void performDataMigration(Connection conn) {
54+
}
55+
56+
@Override
57+
public InputStream[] getCleanupScripts() {
58+
final String scriptFile = "META-INF/db/schema-41910to41920-cleanup.sql";
59+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
60+
if (script == null) {
61+
throw new CloudRuntimeException("Unable to find " + scriptFile);
62+
}
63+
64+
return new InputStream[]{script};
65+
}
66+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade cleanup from 4.19.1.0 to 4.19.2.0
20+
--;
21+
22+
-- Delete `project_account` entries for users that were removed
23+
DELETE FROM `cloud`.`project_account` WHERE `user_id` IN (SELECT `id` FROM `cloud`.`user` WHERE `removed`);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade from 4.19.1.0 to 4.19.2.0
20+
--;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,15 @@ public boolean deleteUser(DeleteUserCmd deleteUserCmd) {
20872087

20882088
// don't allow to delete the user from the account of type Project
20892089
checkAccountAndAccess(user, account);
2090-
return _userDao.remove(deleteUserCmd.getId());
2090+
return Transaction.execute((TransactionCallback<Boolean>) status -> deleteAndCleanupUser(user));
2091+
}
2092+
2093+
protected boolean deleteAndCleanupUser(User user) {
2094+
long userId = user.getId();
2095+
2096+
_projectAccountDao.removeUserFromProjects(userId);
2097+
2098+
return _userDao.remove(userId);
20912099
}
20922100

20932101
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,4 +1046,14 @@ public void testGetActiveUserAccountByEmail() {
10461046
Assert.assertEquals(userAccountVOList.size(), userAccounts.size());
10471047
Assert.assertEquals(userAccountVOList.get(0), userAccounts.get(0));
10481048
}
1049+
1050+
@Test
1051+
public void deleteAndCleanupUserTestRemovesUserFromProjects() {
1052+
long userId = userVoMock.getId();
1053+
Mockito.doNothing().when(_projectAccountDao).removeUserFromProjects(userId);
1054+
1055+
accountManagerImpl.deleteAndCleanupUser(userVoMock);
1056+
1057+
Mockito.verify(_projectAccountDao).removeUserFromProjects(userId);
1058+
}
10491059
}

0 commit comments

Comments
 (0)