From ec9451d4444fc3dfe22bdb1a6805f4a5098767e2 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Sun, 24 Feb 2019 16:22:00 +1100 Subject: [PATCH 1/3] Retrieve devices that only matches the app platform In the current implementation, all active devices are retreived to the app detail page regardless of the app type/platform this lets users select devices which are incompatible with the app. This fix resolves this issue by only fetching the devices which are compatible with the app type. Resolves product-iots#54 --- .../ApplicationOperationsImpl.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java index 7e2aeb6cc..619328b13 100644 --- a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java +++ b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java @@ -243,28 +243,30 @@ public class ApplicationOperationsImpl implements ApplicationOperations { /** * @param applicationOperationDevice holds the information needs to retrieve device list. * @return List of devices - * @throws MobileApplicationException + * @throws MobileApplicationException If unexpected error occur in getting devices or if app platform is not supported. */ public List getDevices(ApplicationOperationDevice applicationOperationDevice) throws MobileApplicationException { List devices; - List deviceList = null; + List deviceList; try { DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils .getDeviceManagementService(applicationOperationDevice.getTenantId()); final String username = applicationOperationDevice.getCurrentUser().getUsername(); - if (MDMAppConstants.WEBAPP.equals - (applicationOperationDevice.getPlatform())) { - deviceList = deviceManagementService. - getDevicesOfUser(username); - } else { - deviceList = deviceManagementService. - getDevicesOfUser(username, - MDMAppConstants.ANDROID); - deviceList.addAll(deviceManagementService. - getDevicesOfUser(username, - MDMAppConstants.IOS)); + final String platform = applicationOperationDevice.getPlatform(); + switch (platform) { + case MDMAppConstants.WEBAPP: + deviceList = deviceManagementService.getDevicesOfUser(username); + break; + case MDMAppConstants.ANDROID: + deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID); + break; + case MDMAppConstants.IOS: + deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS); + break; + default: + throw new MobileApplicationException("App platform:" + platform + "is not supported."); } devices = new ArrayList<>(deviceList.size()); if(log.isDebugEnabled()){ From 6470efcb31baab09a69ca062f23ef62282e17588 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Sun, 24 Feb 2019 16:41:23 +1100 Subject: [PATCH 2/3] Fix formatting --- .../ApplicationOperationsImpl.java | 483 +++++++++--------- 1 file changed, 232 insertions(+), 251 deletions(-) diff --git a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java index 619328b13..eb2cad167 100644 --- a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java +++ b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java @@ -57,236 +57,220 @@ import java.util.List; import java.util.Properties; public class ApplicationOperationsImpl implements ApplicationOperations { - - private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class); - public static final String MEDIA_TYPE_XML = "application/xml"; - public static final String INSTALL = "install"; - public static final String UNINSTALL = "uninstall"; - - /** - * @param applicationOperationAction holds the information needs to perform an action on mdm. - * @throws MobileApplicationException - */ - public String performAction(ApplicationOperationAction applicationOperationAction) - throws MobileApplicationException { - if (log.isDebugEnabled()) { - log.debug(applicationOperationAction.getAction() + " action is triggered for " + - applicationOperationAction.getType() +"."); - } - - Operation operation = null; - List deviceIdentifiers = new ArrayList<>(); - List deviceList; - if (MDMAppConstants.USER.equals(applicationOperationAction.getType())) { - String userName = null; - try { - for (String param : applicationOperationAction.getParams()) { - userName = param; - - deviceList = MDMServiceAPIUtils - .getDeviceManagementService(applicationOperationAction.getTenantId()). - getDevicesOfUser(userName); - - for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { + private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class); + private static final String MEDIA_TYPE_XML = "application/xml"; + private static final String INSTALL = "install"; + private static final String UNINSTALL = "uninstall"; + + /** + * @param applicationOperationAction holds the information needs to perform an action on mdm. + * @throws MobileApplicationException If error occurred while performing the action. + */ + public String performAction(ApplicationOperationAction applicationOperationAction) + throws MobileApplicationException { + if (log.isDebugEnabled()) { + log.debug(applicationOperationAction.getAction() + " action is triggered for " + + applicationOperationAction.getType() + "."); + } + Operation operation = null; + List deviceIdentifiers = new ArrayList<>(); + List deviceList; + if (MDMAppConstants.USER.equals(applicationOperationAction.getType())) { + String userName = null; + try { + for (String param : applicationOperationAction.getParams()) { + userName = param; + deviceList = MDMServiceAPIUtils + .getDeviceManagementService(applicationOperationAction.getTenantId()). + getDevicesOfUser(userName); + for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { if (MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) || applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())) { if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo(). getStatus().toString())) { - deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); - } - } - } - } - } catch (DeviceManagementException devEx) { - String errorMsg = "Error occurred fetch device for user " + userName + - " at app installation"; + deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); + } + } + } + } + } catch (DeviceManagementException devEx) { + String errorMsg = "Error occurred fetch device for user " + userName + " at app installation"; logError(errorMsg, devEx); - throw new MobileApplicationException(errorMsg, devEx); - } - } else if (MDMAppConstants.ROLE.equals(applicationOperationAction.getType())) { - String userRole = null; - try { - for (String param : applicationOperationAction.getParams()) { - userRole = param; - - deviceList = MDMServiceAPIUtils - .getDeviceManagementService(applicationOperationAction.getTenantId()). - getAllDevicesOfRole(userRole); - - for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { - if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) { - deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); - } - } - } - } catch (DeviceManagementException devMgtEx) { - String errorMsg = "Error occurred fetch device for user role " + userRole + - " at app installation"; + throw new MobileApplicationException(errorMsg, devEx); + } + } else if (MDMAppConstants.ROLE.equals(applicationOperationAction.getType())) { + String userRole = null; + try { + for (String param : applicationOperationAction.getParams()) { + userRole = param; + deviceList = MDMServiceAPIUtils + .getDeviceManagementService(applicationOperationAction.getTenantId()). + getAllDevicesOfRole(userRole); + for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { + if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) { + deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); + } + } + } + } catch (DeviceManagementException devMgtEx) { + String errorMsg = "Error occurred fetch device for user role " + userRole + " at app installation"; logError(errorMsg, devMgtEx); - throw new MobileApplicationException(errorMsg, devMgtEx); - } - - } else if (MDMAppConstants.DEVICE.equals(applicationOperationAction.getType())) { - DeviceIdentifier deviceIdentifier; - for (String param : applicationOperationAction.getParams()) { - deviceIdentifier = new DeviceIdentifier(); - if (isValidJSON(param)) { - JSONParser parser = new JSONParser(); - try { - JSONObject parsedObj = (JSONObject) parser.parse(param); - deviceIdentifier.setId((String) parsedObj.get(MDMAppConstants.ID)); - deviceIdentifier.setType((String) parsedObj.get(MDMAppConstants.TYPE)); - deviceIdentifiers.add(deviceIdentifier); - } catch (ParseException e) { - logError("Device Identifier is not valid json object.", e); - throw new MobileApplicationException(e); - } - - } - } - } else { - throw new IllegalStateException("invalid type is received from app store."); - } - App app = applicationOperationAction.getApp(); - MobileApp mobileApp = new MobileApp(); - mobileApp.setId(app.getId()); - mobileApp.setType(MobileAppTypes.valueOf(app.getType().toUpperCase())); - mobileApp.setAppIdentifier(app.getAppIdentifier()); - mobileApp.setIconImage(app.getIconImage()); - mobileApp.setIdentifier(app.getIdentifier()); - mobileApp.setLocation(app.getLocation()); - mobileApp.setName(app.getName()); - mobileApp.setPackageName(app.getPackageName()); - mobileApp.setPlatform(app.getPlatform()); - mobileApp.setVersion(app.getVersion()); - Properties properties = new Properties(); - - if (MDMAppConstants.IOS.equals(app.getPlatform())) { - if (MDMAppConstants.ENTERPRISE.equals(app.getType())) { - properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); - properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); - } else if (MDMAppConstants.IOSConstants.PUBLIC.equals(app.getType())) { - properties.put(MDMAppConstants.IOSConstants.I_TUNES_ID, app.getIdentifier()); - properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); - properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); - } else if (MDMAppConstants.WEBAPP.equals(app.getType())) { - properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName()); - properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); - } - } else if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) { - properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName()); - properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); - } - mobileApp.setProperties(properties); - Activity activity = null; - try { - if (deviceIdentifiers.size() > 0) { - if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.ANDROID.toString())) { - if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) { - operation = AndroidApplicationOperationUtil - .createInstallAppOperation(mobileApp, applicationOperationAction.getSchedule()); - } else if (MDMAppConstants.UPDATE.equals(applicationOperationAction.getAction())) { - operation = AndroidApplicationOperationUtil - .createUpdateAppOperation(mobileApp, applicationOperationAction.getSchedule()); - } else { - operation = AndroidApplicationOperationUtil - .createAppUninstallOperation(mobileApp, applicationOperationAction.getSchedule()); - } - } else if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.IOS.toString())) { - if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) { - operation = - IOSApplicationOperationUtil.createInstallAppOperation(mobileApp); - } else { - if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) { - operation = IOSApplicationOperationUtil.createWebClipUninstallOperation(mobileApp); - } else { - operation = IOSApplicationOperationUtil.createAppUninstallOperation(mobileApp); - } - } - } + throw new MobileApplicationException(errorMsg, devMgtEx); + } + } else if (MDMAppConstants.DEVICE.equals(applicationOperationAction.getType())) { + DeviceIdentifier deviceIdentifier; + for (String param : applicationOperationAction.getParams()) { + deviceIdentifier = new DeviceIdentifier(); + if (isValidJSON(param)) { + JSONParser parser = new JSONParser(); + try { + JSONObject parsedObj = (JSONObject) parser.parse(param); + deviceIdentifier.setId((String) parsedObj.get(MDMAppConstants.ID)); + deviceIdentifier.setType((String) parsedObj.get(MDMAppConstants.TYPE)); + deviceIdentifiers.add(deviceIdentifier); + } catch (ParseException e) { + logError("Device Identifier is not valid json object.", e); + throw new MobileApplicationException(e); + } + } + } + } else { + throw new IllegalStateException("invalid type is received from app store."); + } + App app = applicationOperationAction.getApp(); + MobileApp mobileApp = new MobileApp(); + mobileApp.setId(app.getId()); + mobileApp.setType(MobileAppTypes.valueOf(app.getType().toUpperCase())); + mobileApp.setAppIdentifier(app.getAppIdentifier()); + mobileApp.setIconImage(app.getIconImage()); + mobileApp.setIdentifier(app.getIdentifier()); + mobileApp.setLocation(app.getLocation()); + mobileApp.setName(app.getName()); + mobileApp.setPackageName(app.getPackageName()); + mobileApp.setPlatform(app.getPlatform()); + mobileApp.setVersion(app.getVersion()); + Properties properties = new Properties(); + + if (MDMAppConstants.IOS.equals(app.getPlatform())) { + if (MDMAppConstants.ENTERPRISE.equals(app.getType())) { + properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); + properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); + } else if (MDMAppConstants.IOSConstants.PUBLIC.equals(app.getType())) { + properties.put(MDMAppConstants.IOSConstants.I_TUNES_ID, app.getIdentifier()); + properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); + properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); + } else if (MDMAppConstants.WEBAPP.equals(app.getType())) { + properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName()); + properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); + } + } else if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) { + properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName()); + properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); + } + mobileApp.setProperties(properties); + Activity activity = null; + try { + if (deviceIdentifiers.size() > 0) { + if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.ANDROID.toString())) { + if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) { + operation = AndroidApplicationOperationUtil + .createInstallAppOperation(mobileApp, applicationOperationAction.getSchedule()); + } else if (MDMAppConstants.UPDATE.equals(applicationOperationAction.getAction())) { + operation = AndroidApplicationOperationUtil + .createUpdateAppOperation(mobileApp, applicationOperationAction.getSchedule()); + } else { + operation = AndroidApplicationOperationUtil + .createAppUninstallOperation(mobileApp, applicationOperationAction.getSchedule()); + } + } else if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.IOS.toString())) { + if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) { + operation = + IOSApplicationOperationUtil.createInstallAppOperation(mobileApp); + } else { + if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) { + operation = IOSApplicationOperationUtil.createWebClipUninstallOperation(mobileApp); + } else { + operation = IOSApplicationOperationUtil.createAppUninstallOperation(mobileApp); + } + } + } activity = MDMServiceAPIUtils.getAppManagementService(applicationOperationAction.getTenantId()) .installApplicationForDevices(operation, deviceIdentifiers); + } + if (activity != null) { + return activity.getActivityId(); + } + return null; + } catch (DeviceApplicationException mdmExce) { + logError("Error in creating operation object using app.", mdmExce); + throw new MobileApplicationException(mdmExce.getMessage()); + } catch (ApplicationManagementException appMgtExce) { + logError("Error in app installation.", appMgtExce); + throw new MobileApplicationException(appMgtExce.getErrorMessage()); + } + } + /** + * Create a new device identifier from Device object. + * + * @param device device which is to be retrieved type and id + * @return created device identifier + */ + private static DeviceIdentifier getDeviceIdentifierByDevice(org.wso2.carbon.device.mgt.common.Device device) { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentifier()); + deviceIdentifier.setType(device.getType()); + return deviceIdentifier; + } - } - - if(activity != null){ - return activity.getActivityId(); - } - - return null; - - } catch (DeviceApplicationException mdmExce) { - logError("Error in creating operation object using app.", mdmExce); - throw new MobileApplicationException(mdmExce.getMessage()); - } catch (ApplicationManagementException appMgtExce) { - logError("Error in app installation.", appMgtExce); - throw new MobileApplicationException(appMgtExce.getErrorMessage()); - } - - } - - /** - * Create a new device identifier from Device object. - * @param device device which is to be retrieved type and id - * @return created device identifier - */ - private static DeviceIdentifier getDeviceIdentifierByDevice( - org.wso2.carbon.device.mgt.common.Device device) { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(device.getDeviceIdentifier()); - deviceIdentifier.setType(device.getType()); - - return deviceIdentifier; - } - - /** - * @param applicationOperationDevice holds the information needs to retrieve device list. - * @return List of devices - * @throws MobileApplicationException If unexpected error occur in getting devices or if app platform is not supported. - */ - public List getDevices(ApplicationOperationDevice applicationOperationDevice) - throws MobileApplicationException { - - List devices; - List deviceList; - try { - DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils - .getDeviceManagementService(applicationOperationDevice.getTenantId()); - final String username = applicationOperationDevice.getCurrentUser().getUsername(); - final String platform = applicationOperationDevice.getPlatform(); - switch (platform) { - case MDMAppConstants.WEBAPP: - deviceList = deviceManagementService.getDevicesOfUser(username); - break; - case MDMAppConstants.ANDROID: - deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID); - break; - case MDMAppConstants.IOS: - deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS); - break; - default: - throw new MobileApplicationException("App platform:" + platform + "is not supported."); - } - devices = new ArrayList<>(deviceList.size()); - if(log.isDebugEnabled()){ - log.debug("device list got from mdm "+ deviceList.toString()); - } - for (org.wso2.carbon.device.mgt.common.Device commonDevice : deviceList) { - if (MDMAppConstants.ACTIVE - .equals(commonDevice.getEnrolmentInfo().getStatus().toString(). - toLowerCase())) { - Device device = new Device(); - org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier deviceIdentifier = - new org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier(); - deviceIdentifier.setId(commonDevice.getDeviceIdentifier()); - deviceIdentifier.setType(commonDevice.getType()); - device.setDeviceIdentifier(deviceIdentifier); - device.setName(commonDevice.getName()); - device.setModel(commonDevice.getName()); - device.setType(MDMAppConstants.MOBILE_DEVICE); - String imgUrl; - if (MDMAppConstants.ANDROID.equalsIgnoreCase(commonDevice.getType())) { + /** + * @param applicationOperationDevice holds the information needs to retrieve device list. + * @return List of devices + * @throws MobileApplicationException If an error occurred in getting devices or if app platform is not supported. + */ + public List getDevices(ApplicationOperationDevice applicationOperationDevice) + throws MobileApplicationException { + List devices; + List deviceList; + try { + DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils + .getDeviceManagementService(applicationOperationDevice.getTenantId()); + final String username = applicationOperationDevice.getCurrentUser().getUsername(); + final String platform = applicationOperationDevice.getPlatform(); + switch (platform) { + case MDMAppConstants.WEBAPP: + deviceList = deviceManagementService.getDevicesOfUser(username); + break; + case MDMAppConstants.ANDROID: + deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID); + break; + case MDMAppConstants.IOS: + deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS); + break; + default: + String msg = "App platform:" + platform + "is not supported."; + log.error(msg); + throw new MobileApplicationException(msg); + } + devices = new ArrayList<>(deviceList.size()); + if (log.isDebugEnabled()) { + log.debug("device list got from mdm " + deviceList.toString()); + } + for (org.wso2.carbon.device.mgt.common.Device commonDevice : deviceList) { + if (MDMAppConstants.ACTIVE + .equals(commonDevice.getEnrolmentInfo().getStatus().toString(). + toLowerCase())) { + Device device = new Device(); + org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier deviceIdentifier = + new org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier(); + deviceIdentifier.setId(commonDevice.getDeviceIdentifier()); + deviceIdentifier.setType(commonDevice.getType()); + device.setDeviceIdentifier(deviceIdentifier); + device.setName(commonDevice.getName()); + device.setModel(commonDevice.getName()); + device.setType(MDMAppConstants.MOBILE_DEVICE); + String imgUrl; + if (MDMAppConstants.ANDROID.equalsIgnoreCase(commonDevice.getType())) { imgUrl = String.format(applicationOperationDevice.getConfigParams() .get(MDMAppConstants.IMAGE_URL), MDMAppConstants.NEXUS); @@ -299,38 +283,38 @@ public class ApplicationOperationsImpl implements ApplicationOperations { .get(MDMAppConstants.IMAGE_URL), MDMAppConstants.NONE); } - device.setImage(imgUrl); - device.setPlatform(commonDevice.getType()); - devices.add(device); - } - } - } catch (DeviceManagementException e) { - logError("Error While retrieving Device List.", e); - throw new MobileApplicationException(e.getMessage()); + device.setImage(imgUrl); + device.setPlatform(commonDevice.getType()); + devices.add(device); + } + } + } catch (DeviceManagementException e) { + logError("Error While retrieving Device List.", e); + throw new MobileApplicationException(e.getMessage()); - } - return devices; - } + } + return devices; + } - private boolean isValidJSON(String json) { - JSONParser parser = new JSONParser(); - try { - parser.parse(json); - } catch (ParseException e) { - return false; - } - return true; - } + private boolean isValidJSON(String json) { + JSONParser parser = new JSONParser(); + try { + parser.parse(json); + } catch (ParseException e) { + return false; + } + return true; + } - private void logError(String errorMessage, Throwable e) { - if (log.isDebugEnabled()) { - log.error(errorMessage, e); - } else { - log.error(errorMessage); - } - } + private void logError(String errorMessage, Throwable e) { + if (log.isDebugEnabled()) { + log.error(errorMessage, e); + } else { + log.error(errorMessage); + } + } - public static UserStoreManager getUserStoreManager() throws UserStoreException { + private static UserStoreManager getUserStoreManager() throws UserStoreException { RealmService realmService; UserStoreManager userStoreManager; PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); @@ -368,7 +352,6 @@ public class ApplicationOperationsImpl implements ApplicationOperations { @Override public void run() { - try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); @@ -462,6 +445,4 @@ public class ApplicationOperationsImpl implements ApplicationOperations { } } } - } - From 7ab68e2c99ab40b8ed2695d1505da3b966323f65 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Fri, 1 Mar 2019 18:54:58 +1100 Subject: [PATCH 3/3] Fix issue of app installation with admin user In the current implementation, devices owned by the logged in user is fetched as the device list. When an admin user logged in, even though the user has permission to view and install apps to other users devices the devices does not get fetched if the admin user is not the owner. This resolves the above issue by fetching all devices if the user has admin permission. Resolves product-iots#55 --- .../ApplicationOperationsImpl.java | 28 +++++++++++++------ .../mdmmgt/util/MDMServiceAPIUtils.java | 21 ++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java index eb2cad167..25dbe4242 100644 --- a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java +++ b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/ApplicationOperationsImpl.java @@ -39,6 +39,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.Platform; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.appmgt.mobile.utils.User; @@ -233,25 +235,32 @@ public class ApplicationOperationsImpl implements ApplicationOperations { List devices; List deviceList; try { - DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils - .getDeviceManagementService(applicationOperationDevice.getTenantId()); + final int tenantId = applicationOperationDevice.getTenantId(); final String username = applicationOperationDevice.getCurrentUser().getUsername(); final String platform = applicationOperationDevice.getPlatform(); + DeviceAccessAuthorizationService deviceAccessAuthorizationService = MDMServiceAPIUtils + .getDeviceAccessAuthorizationService(applicationOperationDevice.getTenantId()); + DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils + .getDeviceManagementService(tenantId); + boolean isAdmin = deviceAccessAuthorizationService.isDeviceAdminUser(); + switch (platform) { case MDMAppConstants.WEBAPP: - deviceList = deviceManagementService.getDevicesOfUser(username); + deviceList = isAdmin ? deviceManagementService.getAllDevices() : + deviceManagementService.getDevicesOfUser(username); break; case MDMAppConstants.ANDROID: - deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID); + deviceList = isAdmin ? deviceManagementService.getAllDevices(MDMAppConstants.ANDROID) : + deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID); break; case MDMAppConstants.IOS: - deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS); + deviceList = isAdmin ? deviceManagementService.getAllDevices(MDMAppConstants.IOS) : + deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS); break; default: - String msg = "App platform:" + platform + "is not supported."; - log.error(msg); - throw new MobileApplicationException(msg); + throw new MobileApplicationException("App platform: [" + platform + "] is not supported."); } + devices = new ArrayList<>(deviceList.size()); if (log.isDebugEnabled()) { log.debug("device list got from mdm " + deviceList.toString()); @@ -291,7 +300,8 @@ public class ApplicationOperationsImpl implements ApplicationOperations { } catch (DeviceManagementException e) { logError("Error While retrieving Device List.", e); throw new MobileApplicationException(e.getMessage()); - + } catch (DeviceAccessAuthorizationException e) { + throw new MobileApplicationException("Error while checking user permissions", e); } return devices; } diff --git a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/mdmmgt/util/MDMServiceAPIUtils.java b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/mdmmgt/util/MDMServiceAPIUtils.java index 922f01732..9bf9822eb 100644 --- a/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/mdmmgt/util/MDMServiceAPIUtils.java +++ b/components/extensions/appm-connector/org.wso2.carbon.appmgt.mdm.osgiconnector/src/main/java/org/wso2/carbon/appmgt/mdm/osgiconnector/mdmmgt/util/MDMServiceAPIUtils.java @@ -19,6 +19,7 @@ package org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -49,6 +50,26 @@ public class MDMServiceAPIUtils { return deviceManagementProviderService; } + /** + * Returns the DeviceAccessAuthorizationService osgi service. + * + * @param tenantId tenant id + * @return {@link DeviceAccessAuthorizationService} + */ + public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService(int tenantId) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + ctx.setTenantId(tenantId, true); + DeviceAccessAuthorizationService deviceAccessAuthorizationService = + (DeviceAccessAuthorizationService) ctx + .getOSGiService(DeviceAccessAuthorizationService.class, null); + if (deviceAccessAuthorizationService == null) { + String msg = "Device Access Authorization service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceAccessAuthorizationService; + } + /** * Returns the ApplicationManagementProviderService osgi service. *