Remove user from project before deletion (#10008)

Co-authored-by: Suresh Kumar Anaparti <sureshkumar.anaparti@gmail.com>
This commit is contained in:
Fabricio Duarte 2024-12-04 04:06:22 -03:00 committed by GitHub
parent 7cfeab1a6b
commit ef1a58d837
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 143 additions and 1 deletions

View File

@ -47,6 +47,8 @@ public interface ProjectAccountDao extends GenericDao<ProjectAccountVO, Long> {
void removeAccountFromProjects(long accountId); void removeAccountFromProjects(long accountId);
void removeUserFromProjects(long userId);
boolean canUserModifyProject(long projectId, long accountId, long userId); boolean canUserModifyProject(long projectId, long accountId, long userId);
List<ProjectAccountVO> listUsersOrAccountsByRole(long id); List<ProjectAccountVO> listUsersOrAccountsByRole(long id);

View File

@ -194,6 +194,17 @@ public class ProjectAccountDaoImpl extends GenericDaoBase<ProjectAccountVO, Long
} }
} }
@Override
public void removeUserFromProjects(long userId) {
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();
sc.setParameters("userId", userId);
int removedCount = remove(sc);
if (removedCount > 0) {
s_logger.debug(String.format("Removed user [%s] from %s project(s).", userId, removedCount));
}
}
@Override @Override
public boolean canUserModifyProject(long projectId, long accountId, long userId) { public boolean canUserModifyProject(long projectId, long accountId, long userId) {
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create(); SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();

View File

@ -33,6 +33,7 @@ import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.upgrade.dao.Upgrade41910to41920;
import com.cloud.utils.FileUtil; import com.cloud.utils.FileUtil;
import org.apache.cloudstack.utils.CloudStackVersion; import org.apache.cloudstack.utils.CloudStackVersion;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -227,6 +228,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
.next("4.18.0.0", new Upgrade41800to41810()) .next("4.18.0.0", new Upgrade41800to41810())
.next("4.18.1.0", new Upgrade41810to41900()) .next("4.18.1.0", new Upgrade41810to41900())
.next("4.19.0.0", new Upgrade41900to41910()) .next("4.19.0.0", new Upgrade41900to41910())
.next("4.19.1.0", new Upgrade41910to41920())
.build(); .build();
} }

View File

@ -0,0 +1,66 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.upgrade.dao;
import com.cloud.utils.exception.CloudRuntimeException;
import java.io.InputStream;
import java.sql.Connection;
public class Upgrade41910to41920 implements DbUpgrade {
@Override
public String[] getUpgradableVersionRange() {
return new String[]{"4.19.1.0", "4.19.2.0"};
}
@Override
public String getUpgradedVersion() {
return "4.19.2.0";
}
@Override
public boolean supportsRollingUpgrade() {
return false;
}
@Override
public InputStream[] getPrepareScripts() {
final String scriptFile = "META-INF/db/schema-41910to41920.sql";
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
if (script == null) {
throw new CloudRuntimeException("Unable to find " + scriptFile);
}
return new InputStream[]{script};
}
@Override
public void performDataMigration(Connection conn) {
}
@Override
public InputStream[] getCleanupScripts() {
final String scriptFile = "META-INF/db/schema-41910to41920-cleanup.sql";
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
if (script == null) {
throw new CloudRuntimeException("Unable to find " + scriptFile);
}
return new InputStream[]{script};
}
}

View File

@ -0,0 +1,23 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
--;
-- Schema upgrade cleanup from 4.19.1.0 to 4.19.2.0
--;
-- Delete `project_account` entries for users that were removed
DELETE FROM `cloud`.`project_account` WHERE `user_id` IN (SELECT `id` FROM `cloud`.`user` WHERE `removed`);

View File

@ -0,0 +1,20 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
--;
-- Schema upgrade from 4.19.1.0 to 4.19.2.0
--;

View File

@ -2087,7 +2087,15 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
// don't allow to delete the user from the account of type Project // don't allow to delete the user from the account of type Project
checkAccountAndAccess(user, account); checkAccountAndAccess(user, account);
return _userDao.remove(deleteUserCmd.getId()); return Transaction.execute((TransactionCallback<Boolean>) status -> deleteAndCleanupUser(user));
}
protected boolean deleteAndCleanupUser(User user) {
long userId = user.getId();
_projectAccountDao.removeUserFromProjects(userId);
return _userDao.remove(userId);
} }
@Override @Override

View File

@ -1046,4 +1046,14 @@ public class AccountManagerImplTest extends AccountManagetImplTestBase {
Assert.assertEquals(userAccountVOList.size(), userAccounts.size()); Assert.assertEquals(userAccountVOList.size(), userAccounts.size());
Assert.assertEquals(userAccountVOList.get(0), userAccounts.get(0)); Assert.assertEquals(userAccountVOList.get(0), userAccounts.get(0));
} }
@Test
public void deleteAndCleanupUserTestRemovesUserFromProjects() {
long userId = userVoMock.getId();
Mockito.doNothing().when(_projectAccountDao).removeUserFromProjects(userId);
accountManagerImpl.deleteAndCleanupUser(userVoMock);
Mockito.verify(_projectAccountDao).removeUserFromProjects(userId);
}
} }