Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public interface ProjectAccountDao extends GenericDao<ProjectAccountVO, Long> {

void removeAccountFromProjects(long accountId);

void removeUserFromProjects(long userId);

boolean canUserModifyProject(long projectId, long accountId, long userId);

List<ProjectAccountVO> listUsersOrAccountsByRole(long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@
}
}

@Override
public void removeUserFromProjects(long userId) {
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();
sc.setParameters("userId", userId);

Check warning on line 200 in engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDaoImpl.java#L198-L200

Added lines #L198 - L200 were not covered by tests

int removedCount = remove(sc);

Check warning on line 202 in engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L202 was not covered by tests
if (removedCount > 0) {
s_logger.debug(String.format("Removed user [%s] from %s project(s).", userId, removedCount));

Check warning on line 204 in engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L204 was not covered by tests
}
}

Check warning on line 206 in engine/schema/src/main/java/com/cloud/projects/dao/ProjectAccountDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L206 was not covered by tests

@Override
public boolean canUserModifyProject(long projectId, long accountId, long userId) {
SearchCriteria<ProjectAccountVO> sc = AllFieldsSearch.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import javax.inject.Inject;

import com.cloud.upgrade.dao.Upgrade41910to41920;
import com.cloud.utils.FileUtil;
import org.apache.cloudstack.utils.CloudStackVersion;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -227,6 +228,7 @@ public DatabaseUpgradeChecker() {
.next("4.18.0.0", new Upgrade41800to41810())
.next("4.18.1.0", new Upgrade41810to41900())
.next("4.19.0.0", new Upgrade41900to41910())
.next("4.19.1.0", new Upgrade41910to41920())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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"};
}

Check warning on line 29 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L27-L29

Added lines #L27 - L29 were not covered by tests

@Override
public String getUpgradedVersion() {
return "4.19.2.0";
}

@Override
public boolean supportsRollingUpgrade() {
return false;
}

Check warning on line 39 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L37-L39

Added lines #L37 - L39 were not covered by tests

@Override
public InputStream[] getPrepareScripts() {
final String scriptFile = "META-INF/db/schema-41910to41920.sql";
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);

Check warning on line 44 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L42-L44

Added lines #L42 - L44 were not covered by tests
if (script == null) {
throw new CloudRuntimeException("Unable to find " + scriptFile);

Check warning on line 46 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L46

Added line #L46 was not covered by tests
}

return new InputStream[]{script};
}

Check warning on line 50 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L49-L50

Added lines #L49 - L50 were not covered by tests

@Override
public void performDataMigration(Connection conn) {
}

Check warning on line 54 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L53-L54

Added lines #L53 - L54 were not covered by tests

@Override
public InputStream[] getCleanupScripts() {
final String scriptFile = "META-INF/db/schema-41910to41920-cleanup.sql";
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);

Check warning on line 59 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L57-L59

Added lines #L57 - L59 were not covered by tests
if (script == null) {
throw new CloudRuntimeException("Unable to find " + scriptFile);

Check warning on line 61 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L61

Added line #L61 was not covered by tests
}

return new InputStream[]{script};
}

Check warning on line 65 in engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41910to41920.java#L64-L65

Added lines #L64 - L65 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -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`);
Original file line number Diff line number Diff line change
@@ -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
--;
10 changes: 9 additions & 1 deletion server/src/main/java/com/cloud/user/AccountManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,15 @@

// don't allow to delete the user from the account of type Project
checkAccountAndAccess(user, account);
return _userDao.remove(deleteUserCmd.getId());
return Transaction.execute((TransactionCallback<Boolean>) status -> deleteAndCleanupUser(user));
}

Check warning on line 2091 in server/src/main/java/com/cloud/user/AccountManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/user/AccountManagerImpl.java#L2090-L2091

Added lines #L2090 - L2091 were not covered by tests

protected boolean deleteAndCleanupUser(User user) {
long userId = user.getId();

_projectAccountDao.removeUserFromProjects(userId);

return _userDao.remove(userId);
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions server/src/test/java/com/cloud/user/AccountManagerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1046,4 +1046,14 @@ public void testGetActiveUserAccountByEmail() {
Assert.assertEquals(userAccountVOList.size(), userAccounts.size());
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);
}
}
Loading