diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index fe05de200ea..5b07d0a0498 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -1737,12 +1737,24 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "AP_APP.NAME = ? AND " + "AP_APP.DEVICE_TYPE_ID = ? AND " + "AP_APP.TENANT_ID = ?"; + if(deviceTypeId != -1){ + sql = "SELECT AP_APP.ID AS ID " + + "FROM AP_APP " + + "WHERE " + + "AP_APP.NAME = ? AND " + + "AP_APP.TENANT_ID = ?"; + } try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)){ - stmt.setString(1, appName); - stmt.setInt(2, deviceTypeId); - stmt.setInt(3, tenantId); + if(deviceTypeId != -1){ + stmt.setString(1, appName); + stmt.setInt(2, tenantId); + } else { + stmt.setString(1, appName); + stmt.setInt(2, deviceTypeId); + stmt.setInt(3, tenantId); + } try (ResultSet rs = stmt.executeQuery()){ return rs.next(); } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java index b6bec326553..74d423a8732 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1130,9 +1130,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { return MDMWindowsOperationUtil.createInstallAppOperation(app); } else { - String msg = "Invalid Action is found. Action: " + action; - log.error(msg); - throw new ApplicationManagementException(msg); + if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + return MDMWindowsOperationUtil.createUninstallAppOperation(app); + } else { + String msg = "Invalid Action is found. Action: " + action; + log.error(msg); + throw new ApplicationManagementException(msg); + } } } else { String msg = "Invalid device type is found. Device Type: " + deviceType; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 3fed8e75049..8927ea533d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -218,7 +218,11 @@ public class GroupManagementServiceImpl implements GroupManagementService { log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (GroupNotExistException e) { - String msg = "There is another group already exists with name '" + deviceGroup.getName() + "'."; + String msg = "Group doesn't exist with ID '" + deviceGroup.getGroupId() + "'."; + log.warn(msg); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } catch (GroupAlreadyExistException e) { + String msg = "Group already exists with name '" + deviceGroup.getName() + "'."; log.warn(msg); return Response.status(Response.Status.CONFLICT).entity(msg).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java index 2d430c09f6d..9602d70f559 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java @@ -205,7 +205,7 @@ public class GroupManagementServiceImplTest { } @Test(description = "This method tests the functionality of updateGroup method under various conditions") - public void testUpdateGroup() throws GroupManagementException, GroupNotExistException { + public void testUpdateGroup() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); DeviceGroup deviceGroup = new DeviceGroup(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java index 40b647d6eab..d3a90cfcc84 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java @@ -57,6 +57,7 @@ public class MDMAppConstants { throw new AssertionError(); } public static final String INSTALL_ENTERPRISE_APPLICATION = "INSTALL_ENTERPRISE_APPLICATION"; + public static final String UNINSTALL_ENTERPRISE_APPLICATION = "UNINSTALL_ENTERPRISE_APPLICATION"; //App type constants related to window device type public static final String MSI = "MSI"; public static final String APPX = "APPX"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 7584b5fea7f..06375ca2a18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -727,7 +727,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " - + "WHERE GROUP_NAME = ? AND TENANT_ID = ?"; + + "WHERE LOWER(GROUP_NAME) = LOWER(?) AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, groupName); stmt.setInt(2, tenantId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index c5e7417c866..5028f2560b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -71,7 +71,8 @@ public interface GroupManagementProviderService { * @param groupId of the group. * @throws GroupManagementException */ - void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupNotExistException; + void updateGroup(DeviceGroup deviceGroup, int groupId) + throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException; /** * Delete existing device group. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index c9802b5f292..cd560eef40c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -180,7 +180,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public void updateGroup(DeviceGroup deviceGroup, int groupId) - throws GroupManagementException, GroupNotExistException { + throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { if (deviceGroup == null) { String msg = "Received incomplete data for updateGroup"; log.error(msg); @@ -194,6 +194,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.beginTransaction(); DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); if (existingGroup != null) { + boolean existingGroupName = this.groupDAO.getGroup(deviceGroup.getName(), tenantId) != null; + if (existingGroupName) { + throw new GroupAlreadyExistException("Group already exists with name '" + deviceGroup.getName() + "'."); + } List groupsToUpdate = new ArrayList<>(); String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), DeviceGroupConstants.HierarchicalGroup.SEPERATOR); String parentPath = ""; @@ -247,7 +251,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while initiating transaction."; log.error(msg, e); throw new GroupManagementException(msg, e); - } catch (GroupNotExistException ex) { + } catch (GroupNotExistException | GroupAlreadyExistException ex) { throw ex; } catch (Exception e) { String msg = "Error occurred in updating the device group with ID - '" + groupId + "'."; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java index 48645bbc8d5..18436e048dd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java @@ -45,7 +45,7 @@ public class MDMWindowsOperationUtil { private static final Log log = LogFactory.getLog(MDMWindowsOperationUtil.class); /** - * This method is used to create Install Authentication operation. + * This method is used to create Install Application operation. * * @param application MobileApp application * @return operation object @@ -120,6 +120,82 @@ public class MDMWindowsOperationUtil { return operation; } + /** + * This method is used to create Uninstall Application operation. + * + * @param application MobileApp application + * @return operation object + * @throws UnknownApplicationTypeException + */ + public static Operation createUninstallAppOperation(App application) throws UnknownApplicationTypeException { + + ProfileOperation operation = new ProfileOperation(); + operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_ENTERPRISE_APPLICATION); + operation.setType(Operation.Type.PROFILE); + String appType = windowsAppType(application.getName()); + String metaData = application.getMetaData(); + JsonArray metaJsonArray = jsonStringToArray(metaData); + + switch (application.getType()) { + case ENTERPRISE: + 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); + } + operation.setPayLoad(enterpriseApplication.toJSON()); + break; + default: + String msg = "Application type " + application.getType() + " is not supported"; + log.error(msg); + throw new UnknownApplicationTypeException(msg); + } + + return operation; + } + /** * Method to get the installer file extension type for windows type apps(either appx or msi) * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index 3073b5ee3da..977c1cb62d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -103,7 +103,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest } @Test(dependsOnMethods = "createGroup") - public void updateGroup() throws GroupManagementException, GroupNotExistException { + public void updateGroup() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName(), false); deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); @@ -116,19 +116,19 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest // Rename again to use in different place. @Test(dependsOnMethods = "updateGroup") - public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException { + public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName() + "_UPDATED", true); deviceGroup.setName(TestUtils.createDeviceGroup1().getName()); groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); } - @Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) - public void updateGroupError() throws GroupManagementException, GroupNotExistException { + @Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class, GroupAlreadyExistException.class}) + public void updateGroupError() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { groupManagementProviderService.updateGroup(null, 1); } - @Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) - public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException { + @Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class, GroupAlreadyExistException.class}) + public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName(), false); deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); groupManagementProviderService.updateGroup(deviceGroup, 6); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/mdm-ui-config.xml.j2 b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/mdm-ui-config.xml.j2 index a4d0bbf7c21..8ec602013e0 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/mdm-ui-config.xml.j2 +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/mdm-ui-config.xml.j2 @@ -231,6 +231,7 @@ perm:windows:lock-reset perm:windows:reboot perm:windows:location + perm:android:authenticate-account {% if mdm_ui_conf.scopes is defined %} {%- for scope in mdm_ui_conf.scopes -%} {{scope}}