Skip to content

Commit 68d247d

Browse files
winterhazelsureshanaparti
authored andcommitted
Remove user from project before deletion (apache#10008)
Co-authored-by: Suresh Kumar Anaparti <[email protected]>
1 parent cc55b11 commit 68d247d

File tree

8 files changed

+138
-17
lines changed

8 files changed

+138
-17
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
@@ -192,6 +192,17 @@ public void removeAccountFromProjects(long accountId) {
192192
}
193193
}
194194

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

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

Lines changed: 1 addition & 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;
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
@@ -2207,7 +2207,15 @@ public boolean deleteUser(DeleteUserCmd deleteUserCmd) {
22072207

22082208
// don't allow to delete the user from the account of type Project
22092209
checkAccountAndAccess(user, account);
2210-
return _userDao.remove(id);
2210+
return Transaction.execute((TransactionCallback<Boolean>) status -> deleteAndCleanupUser(user));
2211+
}
2212+
2213+
protected boolean deleteAndCleanupUser(User user) {
2214+
long userId = user.getId();
2215+
2216+
_projectAccountDao.removeUserFromProjects(userId);
2217+
2218+
return _userDao.remove(userId);
22112219
}
22122220

22132221
@Override

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,22 +1177,12 @@ public void testGetActiveUserAccountByEmail() {
11771177
}
11781178

11791179
@Test
1180-
public void testDeleteWebhooksForAccount() {
1181-
try (MockedStatic<ComponentContext> mockedComponentContext = Mockito.mockStatic(ComponentContext.class)) {
1182-
WebhookHelper webhookHelper = Mockito.mock(WebhookHelper.class);
1183-
Mockito.doNothing().when(webhookHelper).deleteWebhooksForAccount(Mockito.anyLong());
1184-
mockedComponentContext.when(() -> ComponentContext.getDelegateComponentOfType(WebhookHelper.class))
1185-
.thenReturn(webhookHelper);
1186-
accountManagerImpl.deleteWebhooksForAccount(1L);
1187-
}
1188-
}
1180+
public void deleteAndCleanupUserTestRemovesUserFromProjects() {
1181+
long userId = userVoMock.getId();
1182+
Mockito.doNothing().when(_projectAccountDao).removeUserFromProjects(userId);
11891183

1190-
@Test
1191-
public void testDeleteWebhooksForAccountNoBean() {
1192-
try (MockedStatic<ComponentContext> mockedComponentContext = Mockito.mockStatic(ComponentContext.class)) {
1193-
mockedComponentContext.when(() -> ComponentContext.getDelegateComponentOfType(WebhookHelper.class))
1194-
.thenThrow(NoSuchBeanDefinitionException.class);
1195-
accountManagerImpl.deleteWebhooksForAccount(1L);
1196-
}
1184+
accountManagerImpl.deleteAndCleanupUser(userVoMock);
1185+
1186+
Mockito.verify(_projectAccountDao).removeUserFromProjects(userId);
11971187
}
11981188
}

0 commit comments

Comments
 (0)