From 2e799a545feb33dfd4618f38e9d5ed31e53cd615 Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 28 Jun 2023 21:43:06 +0530 Subject: [PATCH] Resolve comments --- .../mgt/core/dao/VppApplicationDAO.java | 24 ++++++++++++-- .../vpp/GenericVppApplicationDAOImpl.java | 31 +++++++++++++++---- .../impl/vpp/OracleVppApplicationDAOImpl.java | 22 +++++++++++-- .../impl/vpp/PostgreSQLVppApplicationDAO.java | 22 +++++++++++-- .../vpp/SQLServerVppApplicationDAOImpl.java | 18 +++++++++++ .../core/impl/VppApplicationManagerImpl.java | 12 +++++-- 6 files changed, 113 insertions(+), 16 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java index ae2b75e515..6ebec3f0e7 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -6,9 +24,9 @@ import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManag public interface VppApplicationDAO { - int addVppUser(VppUserDTO userDTO) throws ApplicationManagementDAOException; + int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException; - VppUserDTO updateVppUser(VppUserDTO userDTO) throws ApplicationManagementDAOException; + VppUserDTO updateVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException; - VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementDAOException; + VppUserDTO getUserByDMUsername(String emmUsername, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java index 3dc356b764..2bafc338f3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -16,7 +34,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); @Override - public int addVppUser(VppUserDTO userDTO) + public int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { int vppUserId = -1; String sql = "INSERT INTO " @@ -38,7 +56,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp long currentTime = System.currentTimeMillis(); stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); @@ -65,7 +83,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } } - public VppUserDTO updateVppUser(VppUserDTO userDTO) + public VppUserDTO updateVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { String sql = "UPDATE " @@ -87,7 +105,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); @@ -112,7 +130,7 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } } - public VppUserDTO getUserByDMUsername(String emmUsername) + public VppUserDTO getUserByDMUsername(String emmUsername, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT " + "ID, " @@ -126,11 +144,12 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp + "MANAGED_ID, " + "TEMP_PASSWORD " + "FROM AP_VPP_USER " - + "WHERE DM_USERNAME = ?"; + + "WHERE DM_USERNAME = ? AND TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, emmUsername); + stmt.setInt(2, tenantId); try (ResultSet rs = stmt.executeQuery()) { return DAOUtil.loadVppUser(rs); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java index fd5f5cc9d3..480f18006e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/OracleVppApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -13,7 +31,7 @@ public class OracleVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); @Override - public int addVppUser(VppUserDTO userDTO) + public int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { int vppUserId = -1; String sql = "INSERT INTO " @@ -35,7 +53,7 @@ public class OracleVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { long currentTime = System.currentTimeMillis(); stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java index 7dd20ff6f4..5d9fe7a8f6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/PostgreSQLVppApplicationDAO.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; @@ -12,7 +30,7 @@ public class PostgreSQLVppApplicationDAO extends GenericVppApplicationDAOImpl { private static final Log log = LogFactory.getLog(GenericVppApplicationDAOImpl.class); - public int addVppUser(VppUserDTO userDTO) + public int addVppUser(VppUserDTO userDTO, int tenantId) throws ApplicationManagementDAOException { int vppUserId = -1; String sql = "INSERT INTO " @@ -34,7 +52,7 @@ public class PostgreSQLVppApplicationDAO extends GenericVppApplicationDAOImpl { long currentTime = System.currentTimeMillis(); stmt.setString(1, userDTO.getClientUserId()); stmt.setString(2, userDTO.getDmUsername()); - stmt.setInt(3, userDTO.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, userDTO.getEmail()); stmt.setString(5, userDTO.getInviteCode()); stmt.setString(6, userDTO.getStatus()); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java index dc110b03f4..da4e4828da 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/SQLServerVppApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.application.mgt.core.dao.impl.vpp; public class SQLServerVppApplicationDAOImpl extends GenericVppApplicationDAOImpl { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java index 011a2e3868..468fc17304 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/VppApplicationManagerImpl.java @@ -45,6 +45,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.VppHttpUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpStatus; +import org.wso2.carbon.context.PrivilegedCarbonContext; import java.io.IOException; @@ -77,6 +78,9 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public VppUserDTO addUser(VppUserDTO userDTO) throws ApplicationManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + // Call the API to add try { VppItuneUserDTO ituneUserDTO = userDTO; @@ -105,7 +109,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { log.error("userDTO " + userDTO.toString()); try { ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.addVppUser(userDTO) != -1) { + if (vppApplicationDAO.addVppUser(userDTO, tenantId) != -1) { ConnectionManagerUtil.commitDBTransaction(); return userDTO; } @@ -140,9 +144,10 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - return vppApplicationDAO.getUserByDMUsername(emmUsername); + return vppApplicationDAO.getUserByDMUsername(emmUsername, tenantId); } catch (DBConnectionException e) { String msg = "DB Connection error occurs while getting vpp User data related to EMM user " + emmUsername + "."; log.error(msg, e); @@ -158,6 +163,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { @Override public void updateUser(VppUserDTO userDTO) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); VppItuneUserDTO ituneUserDTO = userDTO; VppItuneUserRequestWrapper wrapper = new VppItuneUserRequestWrapper(); wrapper.getUser().add(ituneUserDTO); @@ -173,7 +179,7 @@ public class VppApplicationManagerImpl implements VPPApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); - if (vppApplicationDAO.updateVppUser(userDTO) == null) { + if (vppApplicationDAO.updateVppUser(userDTO, tenantId) == null) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Unable to update the Vpp user " +userDTO.getId(); log.error(msg);