From 9a99b5804f6302dffed32f30816d2cd3fa632d3c Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Wed, 10 Jul 2024 09:54:26 +0530 Subject: [PATCH 1/4] Fix user, role app installations failing --- .../core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 6ca1095faf..a04da1dd27 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -982,7 +982,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "e.TENANT_ID = ? AND " + "LOWER(e.OWNER) = LOWER(?) AND " + "e.STATUS IN (", - ")) e1 ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); + ")) e1 WHERE d.ID = e1.DEVICE_ID ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); deviceStatuses.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); From 8e33eebbf6ea7087a519353eb6bf46f1bdfb45a0 Mon Sep 17 00:00:00 2001 From: ashvini Date: Fri, 28 Jun 2024 12:38:35 +0530 Subject: [PATCH 2/4] Add tenant application artifact deletion configuration Fix code errors Remove spaces Refactor code --- .../common/services/ApplicationManager.java | 1 + .../mgt/core/impl/ApplicationManagerImpl.java | 35 ++++++++++++------- .../api/admin/UserManagementAdminService.java | 11 ++++-- .../admin/UserManagementAdminServiceImpl.java | 5 ++- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index 4a1be2ea1e..bf3bdc5d40 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -547,4 +547,5 @@ public interface ApplicationManager { */ void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException; void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException; + void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException; } 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java index efb55e60cb..f6d0fc228f 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java @@ -4424,7 +4424,6 @@ public class ApplicationManagerImpl implements ApplicationManager { spApplicationDAO.deleteSPApplicationMappingByTenant(tenantId); spApplicationDAO.deleteIdentityServerByTenant(tenantId); applicationDAO.deleteApplicationsByTenant(tenantId); - APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { @@ -4449,12 +4448,6 @@ public class ApplicationManagerImpl implements ApplicationManager { + " of tenant ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } catch (ApplicationStorageManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while deleting App folder of tenant" - + " of tenant ID: " + tenantId ; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -4499,7 +4492,6 @@ public class ApplicationManagerImpl implements ApplicationManager { spApplicationDAO.deleteSPApplicationMappingByTenant(tenantId); spApplicationDAO.deleteIdentityServerByTenant(tenantId); applicationDAO.deleteApplicationsByTenant(tenantId); - APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { @@ -4524,15 +4516,32 @@ public class ApplicationManagerImpl implements ApplicationManager { + " of tenant ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException { + int tenantId; + try{ + TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); + tenantId = tenantInfoBean.getTenantId(); + + } catch (Exception e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain + "when trying to delete application artifacts of tenant"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + try { + APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); } catch (ApplicationStorageManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while deleting App folder of tenant" + String msg = "Error occurred while deleting Application folders of tenant" + " of tenant ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } - } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java index 5d324aeb02..08b51ac9c4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java @@ -298,8 +298,13 @@ public interface UserManagementAdminService { name = "tenantDomain", value = "The domain of the tenant to be deleted.", required = true) - @PathParam("tenantDomain") - String tenantDomain); - + String tenantDomain, + @ApiParam( + name = "deleteAppArtifacts", + value = "Flag to indicate whether to delete application artifacts.", + required = false) + @QueryParam("deleteAppArtifacts") + @DefaultValue("false") + boolean deleteAppArtifacts); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index d4f34d8e4a..d0e82d7fb3 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -91,7 +91,7 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic @DELETE @Path("/domain/{tenantDomain}") @Override - public Response deleteTenantByDomain(@PathParam("tenantDomain") String tenantDomain) { + public Response deleteTenantByDomain(@PathParam("tenantDomain") String tenantDomain, @QueryParam("deleteAppArtifacts") boolean deleteAppArtifacts) { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); if (tenantId != MultitenantConstants.SUPER_TENANT_ID){ @@ -99,6 +99,9 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic log.error(msg); return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } else { + if(deleteAppArtifacts){ + DeviceMgtAPIUtils.getApplicationManager().deleteApplicationArtifactsByTenantDomain(tenantDomain); + } DeviceMgtAPIUtils.getApplicationManager().deleteApplicationDataByTenantDomain(tenantDomain); DeviceMgtAPIUtils.getDeviceManagementService().deleteDeviceDataByTenantDomain(tenantDomain); TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); From d566cf966a1c68ae353a5fabff7e995b04210ca3 Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Sat, 6 Jul 2024 15:49:41 +0530 Subject: [PATCH 3/4] Add Microsoft Store integration for Windows devices --- .../mgt/core/impl/ApplicationManagerImpl.java | 2 + .../application/mgt/core/util/Constants.java | 1 + .../device/mgt/common/MDMAppConstants.java | 2 + .../app/mgt/windows/AppStoreApplication.java | 49 +++++ .../core/util/MDMWindowsOperationUtil.java | 184 ++++++++---------- .../src/main/resources/conf/mdm-ui-config.xml | 1 + 6 files changed, 139 insertions(+), 100 deletions(-) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/windows/AppStoreApplication.java 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java index efb55e60cb..721ab4decb 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java @@ -769,6 +769,8 @@ public class ApplicationManagerImpl implements ApplicationManager { return Constants.GOOGLE_PLAY_STORE_URL; } else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) { return Constants.APPLE_STORE_URL; + } else if (DeviceTypes.WINDOWS.toString().equalsIgnoreCase(deviceType)) { + return Constants.MICROSOFT_STORE_URL; } else { throw new IllegalArgumentException("No such device with the name " + deviceType); } 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/util/Constants.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/Constants.java index b5b5fd5154..0b22db0668 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/Constants.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/Constants.java @@ -74,6 +74,7 @@ public class Constants { public static final String IS_USER_ABLE_TO_VIEW_ALL_ROLES = "isUserAbleToViewAllRoles"; public static final String GOOGLE_PLAY_STORE_URL = "https://play.google.com/store/apps/details?id="; public static final String APPLE_STORE_URL = "https://itunes.apple.com/country/app/app-name/id"; + public static final String MICROSOFT_STORE_URL = "https://apps.microsoft.com/detail/"; public static final String GOOGLE_PLAY_SYNCED_APP = "GooglePlaySyncedApp"; // Subscription task related constants diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/MDMAppConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/MDMAppConstants.java index 60d9503c97..da1714ede0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/MDMAppConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/MDMAppConstants.java @@ -58,6 +58,8 @@ public class MDMAppConstants { } public static final String INSTALL_ENTERPRISE_APPLICATION = "INSTALL_ENTERPRISE_APPLICATION"; public static final String UNINSTALL_ENTERPRISE_APPLICATION = "UNINSTALL_ENTERPRISE_APPLICATION"; + public static final String INSTALL_STORE_APPLICATION = "INSTALL_STORE_APPLICATION"; + public static final String UNINSTALL_STORE_APPLICATION = "UNINSTALL_STORE_APPLICATION"; public static final String INSTALL_WEB_CLIP_APPLICATION = "INSTALL_WEB_CLIP"; public static final String UNINSTALL_WEB_CLIP_APPLICATION = "UNINSTALL_WEB_CLIP"; //App type constants related to window device type diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/windows/AppStoreApplication.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/windows/AppStoreApplication.java new file mode 100644 index 0000000000..85a6cbd9a1 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/windows/AppStoreApplication.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018 - 2024, 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.device.mgt.common.app.mgt.windows; + +import com.google.gson.Gson; +import java.io.Serializable; + +public class AppStoreApplication implements Serializable { + + private String type; + private String packageIdentifier; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getPackageIdentifier() { + return packageIdentifier; + } + + public void setPackageIdentifier(String packageIdentifier) { + this.packageIdentifier = packageIdentifier; + } + + public String toJSON() { + Gson gson = new Gson(); + return gson.toJson(this); + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/MDMWindowsOperationUtil.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/MDMWindowsOperationUtil.java index a1f0e0e31d..14050b6ea4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/MDMWindowsOperationUtil.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/MDMWindowsOperationUtil.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.MDMAppConstants; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App; +import io.entgra.device.mgt.core.device.mgt.common.app.mgt.windows.AppStoreApplication; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.windows.EnterpriseApplication; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.windows.HostedAppxApplication; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.windows.HostedMSIApplication; @@ -62,64 +63,26 @@ public class MDMWindowsOperationUtil { switch (application.getType()) { case ENTERPRISE: - operation.setCode(MDMAppConstants.WindowsConstants.INSTALL_ENTERPRISE_APPLICATION); EnterpriseApplication enterpriseApplication = new EnterpriseApplication(); - if (appType.equalsIgnoreCase(MDMAppConstants.WindowsConstants.APPX)) { - HostedAppxApplication hostedAppxApplication = new HostedAppxApplication(); - List dependencyPackageList = new ArrayList<>(); - for (int i = 0; i < metaJsonArray.size(); i++) { - JsonElement metaElement = metaJsonArray.get(i); - JsonObject metaObject = metaElement.getAsJsonObject(); - - if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_URI.equals(metaObject.get("key").getAsString())) { - hostedAppxApplication.setPackageUri(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_FAMILY_NAME.equals(metaObject.get("key").getAsString())) { - hostedAppxApplication.setPackageFamilyName(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.APPX_DEPENDENCY_PACKAGE_URL.equals(metaObject.get("key").getAsString()) - && metaObject.has("value")) { - dependencyPackageList.add(metaObject.get("value").getAsString().trim()); - hostedAppxApplication.setDependencyPackageUri(dependencyPackageList); - } - else if (MDMAppConstants.WindowsConstants.APPX_CERTIFICATE_HASH.equals(metaObject.get("key").getAsString()) - && metaObject.has("value")) { - hostedAppxApplication.setCertificateHash(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.APPX_ENCODED_CERT_CONTENT.equals(metaObject.get("key").getAsString()) - && metaObject.has("value")) { - hostedAppxApplication.setEncodedCertificate(metaObject.get("value").getAsString().trim()); - } - } - enterpriseApplication.setHostedAppxApplication(hostedAppxApplication); - - } else if (appType.equalsIgnoreCase(MDMAppConstants.WindowsConstants.MSI)) { - HostedMSIApplication hostedMSIApplication = new HostedMSIApplication(); - for (int i = 0; i < metaJsonArray.size(); i++) { - JsonElement metaElement = metaJsonArray.get(i); - JsonObject metaObject = metaElement.getAsJsonObject(); - if (MDMAppConstants.WindowsConstants.MSI_PRODUCT_ID.equals(metaObject.get("key").getAsString())) { - hostedMSIApplication.setProductId(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.MSI_CONTENT_URI.equals(metaObject.get("key").getAsString())) { - hostedMSIApplication.setContentUrl(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.MSI_FILE_HASH.equals(metaObject.get("key").getAsString())) { - hostedMSIApplication.setFileHash(metaObject.get("value").getAsString().trim()); - } - } - enterpriseApplication.setHostedMSIApplication(hostedMSIApplication); - } + createEnterpriseAppPayload(appType, metaJsonArray, enterpriseApplication); + operation.setCode(MDMAppConstants.WindowsConstants.INSTALL_ENTERPRISE_APPLICATION); operation.setPayLoad(enterpriseApplication.toJSON()); break; + case PUBLIC: + AppStoreApplication appStoreApplication = new AppStoreApplication(); + appStoreApplication.setType(application.getType().toString()); + appStoreApplication.setPackageIdentifier(application.getIdentifier()); + operation.setCode(MDMAppConstants.WindowsConstants.INSTALL_STORE_APPLICATION); + operation.setPayLoad(appStoreApplication.toJSON()); + break; case WEB_CLIP: - operation.setCode(MDMAppConstants.WindowsConstants.INSTALL_WEB_CLIP_APPLICATION); WebClipApplication webClipApplication = new WebClipApplication(); webClipApplication.setUrl(application.getLocation()); webClipApplication.setName(application.getName()); webClipApplication.setIcon(application.getIconImage()); webClipApplication.setProperties(application.getProperties()); webClipApplication.setType(application.getType().toString()); + operation.setCode(MDMAppConstants.WindowsConstants.INSTALL_WEB_CLIP_APPLICATION); operation.setPayLoad(webClipApplication.toJSON()); break; default: @@ -148,64 +111,26 @@ public class MDMWindowsOperationUtil { switch (application.getType()) { case ENTERPRISE: - operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_ENTERPRISE_APPLICATION); EnterpriseApplication enterpriseApplication = new EnterpriseApplication(); - if (appType.equalsIgnoreCase(MDMAppConstants.WindowsConstants.APPX)) { - HostedAppxApplication hostedAppxApplication = new HostedAppxApplication(); - List dependencyPackageList = new ArrayList<>(); - for (int i = 0; i < metaJsonArray.size(); i++) { - JsonElement metaElement = metaJsonArray.get(i); - JsonObject metaObject = metaElement.getAsJsonObject(); - - if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_URI.equals(metaObject.get("key").getAsString())) { - hostedAppxApplication.setPackageUri(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_FAMILY_NAME.equals(metaObject.get("key").getAsString())) { - hostedAppxApplication.setPackageFamilyName(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.APPX_DEPENDENCY_PACKAGE_URL.equals(metaObject.get("key").getAsString()) - && metaObject.has("value")) { - dependencyPackageList.add(metaObject.get("value").getAsString().trim()); - hostedAppxApplication.setDependencyPackageUri(dependencyPackageList); - } - else if (MDMAppConstants.WindowsConstants.APPX_CERTIFICATE_HASH.equals(metaObject.get("key").getAsString()) - && metaObject.has("value")) { - hostedAppxApplication.setCertificateHash(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.APPX_ENCODED_CERT_CONTENT.equals(metaObject.get("key").getAsString()) - && metaObject.has("value")) { - hostedAppxApplication.setEncodedCertificate(metaObject.get("value").getAsString().trim()); - } - } - enterpriseApplication.setHostedAppxApplication(hostedAppxApplication); - - } else if (appType.equalsIgnoreCase(MDMAppConstants.WindowsConstants.MSI)) { - HostedMSIApplication hostedMSIApplication = new HostedMSIApplication(); - for (int i = 0; i < metaJsonArray.size(); i++) { - JsonElement metaElement = metaJsonArray.get(i); - JsonObject metaObject = metaElement.getAsJsonObject(); - if (MDMAppConstants.WindowsConstants.MSI_PRODUCT_ID.equals(metaObject.get("key").getAsString())) { - hostedMSIApplication.setProductId(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.MSI_CONTENT_URI.equals(metaObject.get("key").getAsString())) { - hostedMSIApplication.setContentUrl(metaObject.get("value").getAsString().trim()); - } - else if (MDMAppConstants.WindowsConstants.MSI_FILE_HASH.equals(metaObject.get("key").getAsString())) { - hostedMSIApplication.setFileHash(metaObject.get("value").getAsString().trim()); - } - } - enterpriseApplication.setHostedMSIApplication(hostedMSIApplication); - } + createEnterpriseAppPayload(appType, metaJsonArray, enterpriseApplication); + operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_ENTERPRISE_APPLICATION); operation.setPayLoad(enterpriseApplication.toJSON()); break; + case PUBLIC: + AppStoreApplication appStoreApplication = new AppStoreApplication(); + appStoreApplication.setType(application.getType().toString()); + appStoreApplication.setPackageIdentifier(application.getIdentifier()); + operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_STORE_APPLICATION); + operation.setPayLoad(appStoreApplication.toJSON()); + break; case WEB_CLIP: - operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_WEB_CLIP_APPLICATION); WebClipApplication webClipApplication = new WebClipApplication(); webClipApplication.setUrl(application.getLocation()); webClipApplication.setName(application.getName()); webClipApplication.setIcon(application.getIconImage()); webClipApplication.setProperties(application.getProperties()); webClipApplication.setType(application.getType().toString()); + operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_WEB_CLIP_APPLICATION); operation.setPayLoad(webClipApplication.toJSON()); default: String msg = "Application type " + application.getType() + " is not supported"; @@ -216,6 +141,67 @@ public class MDMWindowsOperationUtil { return operation; } + /** + * Helper method to create enterprise APPX and MSI app payloads for both install and uninstall operations + * @param appType contains whether the app type is APPX or MSI + * @param metaJsonArray JSON array containing metadata of APPX and MSI apps + * @param enterpriseApplication {@link EnterpriseApplication} contains operation payload content that will be sent to the device + */ + private static void createEnterpriseAppPayload(String appType, JsonArray metaJsonArray, EnterpriseApplication enterpriseApplication) { + + JsonElement metaElement; + JsonObject metaObject; + if (MDMAppConstants.WindowsConstants.APPX.equalsIgnoreCase(appType)) { + HostedAppxApplication hostedAppxApplication = new HostedAppxApplication(); + List dependencyPackageList = new ArrayList<>(); + + for (int i = 0; i < metaJsonArray.size(); i++) { + metaElement = metaJsonArray.get(i); + metaObject = metaElement.getAsJsonObject(); + + if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_URI.equals(metaObject.get("key").getAsString())) { + hostedAppxApplication.setPackageUri(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_FAMILY_NAME.equals(metaObject.get("key").getAsString())) { + hostedAppxApplication.setPackageFamilyName(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.APPX_DEPENDENCY_PACKAGE_URL.equals(metaObject.get("key").getAsString()) + && metaObject.has("value")) { + dependencyPackageList.add(metaObject.get("value").getAsString().trim()); + hostedAppxApplication.setDependencyPackageUri(dependencyPackageList); + } + else if (MDMAppConstants.WindowsConstants.APPX_CERTIFICATE_HASH.equals(metaObject.get("key").getAsString()) + && metaObject.has("value")) { + hostedAppxApplication.setCertificateHash(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.APPX_ENCODED_CERT_CONTENT.equals(metaObject.get("key").getAsString()) + && metaObject.has("value")) { + hostedAppxApplication.setEncodedCertificate(metaObject.get("value").getAsString().trim()); + } + } + enterpriseApplication.setHostedAppxApplication(hostedAppxApplication); + + } else if (MDMAppConstants.WindowsConstants.MSI.equalsIgnoreCase(appType)) { + HostedMSIApplication hostedMSIApplication = new HostedMSIApplication(); + + for (int i = 0; i < metaJsonArray.size(); i++) { + metaElement = metaJsonArray.get(i); + metaObject = metaElement.getAsJsonObject(); + + if (MDMAppConstants.WindowsConstants.MSI_PRODUCT_ID.equals(metaObject.get("key").getAsString())) { + hostedMSIApplication.setProductId(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.MSI_CONTENT_URI.equals(metaObject.get("key").getAsString())) { + hostedMSIApplication.setContentUrl(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.MSI_FILE_HASH.equals(metaObject.get("key").getAsString())) { + hostedMSIApplication.setFileHash(metaObject.get("value").getAsString().trim()); + } + } + enterpriseApplication.setHostedMSIApplication(hostedMSIApplication); + } + } + /** * Method to get the installer file extension type for windows type apps(either appx or msi) * @@ -223,8 +209,7 @@ public class MDMWindowsOperationUtil { * @return string extension of the windows app types(either appx or msi) */ public static String windowsAppType(String installerName) { - String extension = installerName.substring(installerName.lastIndexOf(".") + 1); - return extension; + return installerName.substring(installerName.lastIndexOf(".") + 1); } /** @@ -234,8 +219,7 @@ public class MDMWindowsOperationUtil { * @return the metaData Json String as Json Array */ public static JsonArray jsonStringToArray(String metaData) { - JsonArray metaJsonArray = new JsonParser().parse(metaData).getAsJsonArray(); - return metaJsonArray; + return new JsonParser().parse(metaData).getAsJsonArray(); } diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index 32dc388789..65ffa660c8 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -387,6 +387,7 @@ win:ops:device-info win:ops:security-info win:ops:firewall-info + win:microsoft-store:search admin:tenant:view dm:admin:devices:usage:view and:ops:clear-app From e1a47c68fbbf3822cf47669b6aef2197d2d16aad Mon Sep 17 00:00:00 2001 From: ashvini Date: Tue, 2 Jul 2024 14:20:57 +0530 Subject: [PATCH 4/4] Get TenantMgtAdminService as an osgi service Resolve comments Remove source reference Change error messages Make changes in code Add license Resolve comments Add java doc comments Resolve comments Resolve comment --- .../common/services/ApplicationManager.java | 14 +++++ .../pom.xml | 5 ++ .../mgt/core/impl/ApplicationManagerImpl.java | 57 +++++++------------ ...ApplicationManagementServiceComponent.java | 24 ++++++++ .../mgt/core/internal/DataHolder.java | 11 ++++ .../admin/UserManagementAdminServiceImpl.java | 14 ++--- .../mgt/api/jaxrs/util/DeviceMgtAPIUtils.java | 19 +++++++ .../common/spi/TenantManagerAdminService.java | 27 +++++++++ .../pom.xml | 1 + .../core/tenant/mgt/core/TenantManager.java | 1 + .../impl/TenantManagerAdminServiceImpl.java | 57 +++++++++++++++++++ .../internal/TenantMgtServiceComponent.java | 9 ++- .../listener/DeviceMgtTenantListener.java | 8 --- 13 files changed, 195 insertions(+), 52 deletions(-) create mode 100644 components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java create mode 100644 components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index bf3bdc5d40..7e13353980 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -546,6 +546,20 @@ public interface ApplicationManager { * @throws ApplicationManagementException thrown if an error occurs when deleting data */ void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException; + + /** + * Delete all application related data of a tenant by tenant Domain + * + * @param tenantDomain Domain of the Tenant + * @throws ApplicationManagementException thrown if an error occurs when deleting data + */ void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException; + + /** + * Delete all Application artifacts related to a tenant by Tenant Domain + * + * @param tenantDomain Domain of the Tenant + * @throws ApplicationManagementException thrown if an error occurs when deleting app folders + */ void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml index 057345defc..b273d85a82 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml @@ -81,6 +81,7 @@ com.dd.*, io.entgra.device.mgt.core.identity.jwt.client.extension.*, io.entgra.device.mgt.core.apimgt.application.extension.*, + io.entgra.device.mgt.core.tenant.mgt.common.*, org.apache.commons.httpclient, org.apache.commons.httpclient.methods, org.apache.commons.validator.routines @@ -389,6 +390,10 @@ org.wso2.carbon.tenant.mgt compile + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.tenant.mgt.common + 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java index f6d0fc228f..5131eeeed8 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java @@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringEscapeUtils; @@ -96,8 +97,6 @@ import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementEx import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.stratos.common.beans.TenantInfoBean; -import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; @@ -4456,19 +4455,9 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException { int tenantId; - try{ - TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); - TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); - tenantId = tenantInfoBean.getTenantId(); - - } catch (Exception e) { - String msg = "Error getting tenant ID from domain: " - + tenantDomain; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } - try { + tenantId = DataHolder.getInstance().getTenantManagerAdminService().getTenantId(tenantDomain); + ConnectionManagerUtil.beginDBTransaction(); vppApplicationDAO.deleteAssociationByTenant(tenantId); @@ -4495,51 +4484,49 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { - String msg = "Error occurred while observing the database connection to delete applications for tenant with ID: " - + tenantId; + String msg = "Error occurred while observing the database connection to delete applications for tenant with " + + "domain: " + tenantDomain; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Database access error is occurred when getting applications for tenant with ID: " + tenantId; + String msg = "Database access error is occurred when getting applications for tenant with domain: " + + tenantDomain; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (LifeCycleManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while deleting life-cycle state data of application releases of the tenant" - + " of ID: " + tenantId ; + + " of domain: " + tenantDomain ; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ReviewManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while deleting reviews of application releases of the applications" - + " of tenant ID: " + tenantId ; + + " of tenant of domain: " + tenantDomain ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (Exception e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @Override public void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException { int tenantId; - try{ - TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); - TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); - tenantId = tenantInfoBean.getTenantId(); - - } catch (Exception e) { - String msg = "Error getting tenant ID from domain: " - + tenantDomain + "when trying to delete application artifacts of tenant"; + try { + tenantId = DataHolder.getInstance().getTenantManagerAdminService().getTenantId(tenantDomain); + DataHolder.getInstance().getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); + } catch (ApplicationStorageManagementException e) { + String msg = "Error deleting app artifacts of tenant of domain: " + tenantDomain ; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } - try { - APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting Application folders of tenant" - + " of tenant ID: " + tenantId ; + } catch (TenantMgtException e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain + " when trying to delete application artifacts of tenant"; log.error(msg, e); throw new ApplicationManagementException(msg, e); } 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/internal/ApplicationManagementServiceComponent.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java index 20cd9febbc..d1b8ff8288 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java @@ -33,7 +33,9 @@ import io.entgra.device.mgt.core.application.mgt.core.impl.FileTransferServiceIm import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.application.mgt.core.task.ScheduledAppSubscriptionTaskManager; import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil; +import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -71,6 +73,12 @@ import java.util.List; * policy="dynamic" * bind="setTaskService" * unbind="unsetTaskService" + * @scr.reference name="io.entgra.device.mgt.core.tenant.manager" + * interface="io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService" + * cardinality="0..1" + * policy="dynamic" + * bind="setTenantManagementAdminService" + * unbind="unsetTenantManagementAdminService" */ @SuppressWarnings("unused") public class ApplicationManagementServiceComponent { @@ -196,4 +204,20 @@ public class ApplicationManagementServiceComponent { } DataHolder.getInstance().setTaskService(null); } + + @SuppressWarnings("unused") + protected void setTenantManagementAdminService(TenantManagerAdminService tenantManagerAdminService) { + if (log.isDebugEnabled()) { + log.debug("Setting Tenant management admin Service"); + } + DataHolder.getInstance().setTenantManagerAdminService(tenantManagerAdminService); + } + + @SuppressWarnings("unused") + protected void unsetTenantManagementAdminService(TenantManagerAdminService tenantManagerAdminService) { + if (log.isDebugEnabled()) { + log.debug("Un setting Tenant management admin service"); + } + DataHolder.getInstance().setTenantManagerAdminService(null); + } } 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/internal/DataHolder.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java index 80416dcd59..c67db70fb6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java @@ -27,6 +27,8 @@ import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionMan import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager; import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.user.core.service.RealmService; @@ -57,6 +59,7 @@ public class DataHolder { private TaskService taskService; private FileTransferService fileTransferService; + private TenantManagerAdminService tenantManagerAdminService; private static final DataHolder applicationMgtDataHolder = new DataHolder(); @@ -163,4 +166,12 @@ public class DataHolder { public void setFileTransferService(FileTransferService fileTransferService) { this.fileTransferService = fileTransferService; } + + public TenantManagerAdminService getTenantManagerAdminService() { + return tenantManagerAdminService; + } + + public void setTenantManagerAdminService(TenantManagerAdminService tenantManagerAdminService) { + this.tenantManagerAdminService = tenantManagerAdminService; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index d0e82d7fb3..91ed3f6d42 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; @@ -29,9 +30,6 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.CredentialManagementR import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.stratos.common.exception.StratosException; -import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; -import org.wso2.carbon.user.api.UserStoreException; import javax.validation.constraints.Size; import javax.ws.rs.*; @@ -99,18 +97,20 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic log.error(msg); return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } else { - if(deleteAppArtifacts){ + if (deleteAppArtifacts) { DeviceMgtAPIUtils.getApplicationManager().deleteApplicationArtifactsByTenantDomain(tenantDomain); } DeviceMgtAPIUtils.getApplicationManager().deleteApplicationDataByTenantDomain(tenantDomain); DeviceMgtAPIUtils.getDeviceManagementService().deleteDeviceDataByTenantDomain(tenantDomain); - TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); - tenantMgtAdminService.deleteTenant(tenantDomain); + DeviceMgtAPIUtils.getTenantManagerAdminService().deleteTenant(tenantDomain); String msg = "Tenant Deletion process has been initiated for tenant:" + tenantDomain; + if (log.isDebugEnabled()) { + log.debug(msg); + } return Response.status(Response.Status.OK).entity(msg).build(); } - } catch (StratosException | UserStoreException e) { + } catch (TenantMgtException e) { String msg = "Error deleting tenant: " + tenantDomain; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java index 885d521389..5451572519 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionMan import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuthorizationService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionManagerServiceImpl; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.java.security.SSLProtocolSocketFactory; @@ -163,6 +164,7 @@ public class DeviceMgtAPIUtils { private static volatile ApplicationManager applicationManager; private static volatile APIPublisherService apiPublisher; + private static volatile TenantManagerAdminService tenantManagerAdminService; static { String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password"); @@ -1243,4 +1245,21 @@ public class DeviceMgtAPIUtils { } return isPermitted; } + + public static TenantManagerAdminService getTenantManagerAdminService(){ + if(tenantManagerAdminService == null) { + synchronized (DeviceMgtAPIUtils.class) { + if (tenantManagerAdminService == null) { + tenantManagerAdminService = (TenantManagerAdminService) PrivilegedCarbonContext.getThreadLocalCarbonContext(). + getOSGiService(TenantManagerAdminService.class, null); + if (tenantManagerAdminService == null) { + String msg = "Tenant Manager Admin Service is null"; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return tenantManagerAdminService; + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java new file mode 100644 index 0000000000..0b93e7ca5b --- /dev/null +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 - 2024, 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.tenant.mgt.common.spi; + +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; + +public interface TenantManagerAdminService { + + void deleteTenant(String tenantDomain) throws TenantMgtException; + int getTenantId(String tenantDomain) throws TenantMgtException; +} diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml index 33d4c9969c..f89aa3cf39 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml @@ -60,6 +60,7 @@ org.wso2.carbon.stratos.common.beans, org.wso2.carbon.stratos.common.exception, org.wso2.carbon.stratos.common.listeners, + org.wso2.carbon.tenant.mgt.services, io.entgra.device.mgt.core.device.mgt.common.metadata.mgt, io.entgra.device.mgt.core.device.mgt.common.exceptions, io.entgra.device.mgt.core.device.mgt.common.permission.mgt, diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java index ac3ee406f9..ea26974300 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java @@ -57,4 +57,5 @@ public interface TenantManager { * @throws TenantMgtException Throws when deleting Tenant related device data */ void deleteTenantDeviceData(int tenantId) throws TenantMgtException; + } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java new file mode 100644 index 0000000000..b96535da25 --- /dev/null +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2018 - 2024, 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.tenant.mgt.core.impl; + +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.stratos.common.exception.StratosException; +import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; +import org.wso2.carbon.user.api.UserStoreException; + + +public class TenantManagerAdminServiceImpl implements TenantManagerAdminService { + + private static final Log log = LogFactory.getLog(TenantManagerAdminServiceImpl.class); + + private static final TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + + @Override + public void deleteTenant(String tenantDomain) throws TenantMgtException { + try { + tenantMgtAdminService.deleteTenant(tenantDomain); + } catch (StratosException | UserStoreException e) { + String msg = "Error occurred while deleting tenant of domain: " + tenantDomain; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } + } + + @Override + public int getTenantId(String tenantDomain) throws TenantMgtException { + try { + return tenantMgtAdminService.getTenant(tenantDomain).getTenantId(); + } catch (Exception e){ + String msg = "Error occurred while getting tenant ID of domain: " + tenantDomain; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } + } +} diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java index fa2f26c972..c6ad710cb8 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -20,8 +20,10 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerService; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; +import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerAdminServiceImpl; import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl; import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerServiceImpl; import io.entgra.device.mgt.core.tenant.mgt.core.listener.DeviceMgtTenantListener; @@ -59,11 +61,14 @@ public class TenantMgtServiceComponent { try { TenantManagerService tenantManagerService = new TenantManagerServiceImpl(); componentContext.getBundleContext(). - registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null); + registerService(TenantManagerService.class.getName(), tenantManagerService, null); + TenantManagerAdminService tenantManagerAdminService = new TenantManagerAdminServiceImpl(); + componentContext.getBundleContext(). + registerService(TenantManagerAdminService.class.getName(), tenantManagerAdminService, null); TenantManager tenantManager = new TenantManagerImpl(); TenantMgtDataHolder.getInstance().setTenantManager(tenantManager); WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); - componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), + componentContext.getBundleContext().registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java index 257206f84f..978e69b0e4 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -88,13 +88,5 @@ public class DeviceMgtTenantListener implements TenantMgtListener { @Override public void onPreDelete(int i) throws StratosException { // Any work to be performed before a tenant is deleted - TenantManager tenantManager = TenantMgtDataHolder.getInstance().getTenantManager(); - try{ - tenantManager.deleteTenantDeviceData(i); - tenantManager.deleteTenantApplicationData(i); - } catch (TenantMgtException e) { - String msg = "Error occurred while deleting tenant data"; - log.error(msg, e); - } } }