Merge branch 'iots-54' into 'master'

Fix issues with APPM Connector

Closes product-iots#55 and product-iots#54

See merge request entgra/carbon-device-mgt-plugins!18
revert-dabc3590
Dharmakeerthi Lasantha 6 years ago
commit 0b1deff245

@ -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.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.Platform; 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.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.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.appmgt.mobile.utils.User; import org.wso2.carbon.appmgt.mobile.utils.User;
@ -57,15 +59,14 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
public class ApplicationOperationsImpl implements ApplicationOperations { public class ApplicationOperationsImpl implements ApplicationOperations {
private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class); private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class);
public static final String MEDIA_TYPE_XML = "application/xml"; private static final String MEDIA_TYPE_XML = "application/xml";
public static final String INSTALL = "install"; private static final String INSTALL = "install";
public static final String UNINSTALL = "uninstall"; private static final String UNINSTALL = "uninstall";
/** /**
* @param applicationOperationAction holds the information needs to perform an action on mdm. * @param applicationOperationAction holds the information needs to perform an action on mdm.
* @throws MobileApplicationException * @throws MobileApplicationException If error occurred while performing the action.
*/ */
public String performAction(ApplicationOperationAction applicationOperationAction) public String performAction(ApplicationOperationAction applicationOperationAction)
throws MobileApplicationException { throws MobileApplicationException {
@ -73,7 +74,6 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
log.debug(applicationOperationAction.getAction() + " action is triggered for " + log.debug(applicationOperationAction.getAction() + " action is triggered for " +
applicationOperationAction.getType() + "."); applicationOperationAction.getType() + ".");
} }
Operation operation = null; Operation operation = null;
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
List<org.wso2.carbon.device.mgt.common.Device> deviceList; List<org.wso2.carbon.device.mgt.common.Device> deviceList;
@ -82,11 +82,9 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
try { try {
for (String param : applicationOperationAction.getParams()) { for (String param : applicationOperationAction.getParams()) {
userName = param; userName = param;
deviceList = MDMServiceAPIUtils deviceList = MDMServiceAPIUtils
.getDeviceManagementService(applicationOperationAction.getTenantId()). .getDeviceManagementService(applicationOperationAction.getTenantId()).
getDevicesOfUser(userName); getDevicesOfUser(userName);
for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { for (org.wso2.carbon.device.mgt.common.Device device : deviceList) {
if (MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) || if (MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) ||
applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())) { applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())) {
@ -98,8 +96,7 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
} }
} catch (DeviceManagementException devEx) { } catch (DeviceManagementException devEx) {
String errorMsg = "Error occurred fetch device for user " + userName + String errorMsg = "Error occurred fetch device for user " + userName + " at app installation";
" at app installation";
logError(errorMsg, devEx); logError(errorMsg, devEx);
throw new MobileApplicationException(errorMsg, devEx); throw new MobileApplicationException(errorMsg, devEx);
} }
@ -108,11 +105,9 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
try { try {
for (String param : applicationOperationAction.getParams()) { for (String param : applicationOperationAction.getParams()) {
userRole = param; userRole = param;
deviceList = MDMServiceAPIUtils deviceList = MDMServiceAPIUtils
.getDeviceManagementService(applicationOperationAction.getTenantId()). .getDeviceManagementService(applicationOperationAction.getTenantId()).
getAllDevicesOfRole(userRole); getAllDevicesOfRole(userRole);
for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { for (org.wso2.carbon.device.mgt.common.Device device : deviceList) {
if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) { if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) {
deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); deviceIdentifiers.add(getDeviceIdentifierByDevice(device));
@ -120,12 +115,10 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
} }
} catch (DeviceManagementException devMgtEx) { } catch (DeviceManagementException devMgtEx) {
String errorMsg = "Error occurred fetch device for user role " + userRole + String errorMsg = "Error occurred fetch device for user role " + userRole + " at app installation";
" at app installation";
logError(errorMsg, devMgtEx); logError(errorMsg, devMgtEx);
throw new MobileApplicationException(errorMsg, devMgtEx); throw new MobileApplicationException(errorMsg, devMgtEx);
} }
} else if (MDMAppConstants.DEVICE.equals(applicationOperationAction.getType())) { } else if (MDMAppConstants.DEVICE.equals(applicationOperationAction.getType())) {
DeviceIdentifier deviceIdentifier; DeviceIdentifier deviceIdentifier;
for (String param : applicationOperationAction.getParams()) { for (String param : applicationOperationAction.getParams()) {
@ -141,7 +134,6 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
logError("Device Identifier is not valid json object.", e); logError("Device Identifier is not valid json object.", e);
throw new MobileApplicationException(e); throw new MobileApplicationException(e);
} }
} }
} }
} else { } else {
@ -206,16 +198,11 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
activity = MDMServiceAPIUtils.getAppManagementService(applicationOperationAction.getTenantId()) activity = MDMServiceAPIUtils.getAppManagementService(applicationOperationAction.getTenantId())
.installApplicationForDevices(operation, deviceIdentifiers); .installApplicationForDevices(operation, deviceIdentifiers);
} }
if (activity != null) { if (activity != null) {
return activity.getActivityId(); return activity.getActivityId();
} }
return null; return null;
} catch (DeviceApplicationException mdmExce) { } catch (DeviceApplicationException mdmExce) {
logError("Error in creating operation object using app.", mdmExce); logError("Error in creating operation object using app.", mdmExce);
throw new MobileApplicationException(mdmExce.getMessage()); throw new MobileApplicationException(mdmExce.getMessage());
@ -223,49 +210,57 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
logError("Error in app installation.", appMgtExce); logError("Error in app installation.", appMgtExce);
throw new MobileApplicationException(appMgtExce.getErrorMessage()); throw new MobileApplicationException(appMgtExce.getErrorMessage());
} }
} }
/** /**
* Create a new device identifier from Device object. * Create a new device identifier from Device object.
*
* @param device device which is to be retrieved type and id * @param device device which is to be retrieved type and id
* @return created device identifier * @return created device identifier
*/ */
private static DeviceIdentifier getDeviceIdentifierByDevice( private static DeviceIdentifier getDeviceIdentifierByDevice(org.wso2.carbon.device.mgt.common.Device device) {
org.wso2.carbon.device.mgt.common.Device device) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(device.getDeviceIdentifier()); deviceIdentifier.setId(device.getDeviceIdentifier());
deviceIdentifier.setType(device.getType()); deviceIdentifier.setType(device.getType());
return deviceIdentifier; return deviceIdentifier;
} }
/** /**
* @param applicationOperationDevice holds the information needs to retrieve device list. * @param applicationOperationDevice holds the information needs to retrieve device list.
* @return List of devices * @return List of devices
* @throws MobileApplicationException * @throws MobileApplicationException If an error occurred in getting devices or if app platform is not supported.
*/ */
public List<Device> getDevices(ApplicationOperationDevice applicationOperationDevice) public List<Device> getDevices(ApplicationOperationDevice applicationOperationDevice)
throws MobileApplicationException { throws MobileApplicationException {
List<Device> devices; List<Device> devices;
List<org.wso2.carbon.device.mgt.common.Device> deviceList = null; List<org.wso2.carbon.device.mgt.common.Device> deviceList;
try { try {
DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils final int tenantId = applicationOperationDevice.getTenantId();
.getDeviceManagementService(applicationOperationDevice.getTenantId());
final String username = applicationOperationDevice.getCurrentUser().getUsername(); final String username = applicationOperationDevice.getCurrentUser().getUsername();
if (MDMAppConstants.WEBAPP.equals final String platform = applicationOperationDevice.getPlatform();
(applicationOperationDevice.getPlatform())) { DeviceAccessAuthorizationService deviceAccessAuthorizationService = MDMServiceAPIUtils
deviceList = deviceManagementService. .getDeviceAccessAuthorizationService(applicationOperationDevice.getTenantId());
getDevicesOfUser(username); DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils
} else { .getDeviceManagementService(tenantId);
deviceList = deviceManagementService. boolean isAdmin = deviceAccessAuthorizationService.isDeviceAdminUser();
getDevicesOfUser(username,
MDMAppConstants.ANDROID); switch (platform) {
deviceList.addAll(deviceManagementService. case MDMAppConstants.WEBAPP:
getDevicesOfUser(username, deviceList = isAdmin ? deviceManagementService.getAllDevices() :
MDMAppConstants.IOS)); deviceManagementService.getDevicesOfUser(username);
break;
case MDMAppConstants.ANDROID:
deviceList = isAdmin ? deviceManagementService.getAllDevices(MDMAppConstants.ANDROID) :
deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID);
break;
case MDMAppConstants.IOS:
deviceList = isAdmin ? deviceManagementService.getAllDevices(MDMAppConstants.IOS) :
deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS);
break;
default:
throw new MobileApplicationException("App platform: [" + platform + "] is not supported.");
} }
devices = new ArrayList<>(deviceList.size()); devices = new ArrayList<>(deviceList.size());
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("device list got from mdm " + deviceList.toString()); log.debug("device list got from mdm " + deviceList.toString());
@ -305,7 +300,8 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
logError("Error While retrieving Device List.", e); logError("Error While retrieving Device List.", e);
throw new MobileApplicationException(e.getMessage()); throw new MobileApplicationException(e.getMessage());
} catch (DeviceAccessAuthorizationException e) {
throw new MobileApplicationException("Error while checking user permissions", e);
} }
return devices; return devices;
} }
@ -328,7 +324,7 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
} }
public static UserStoreManager getUserStoreManager() throws UserStoreException { private static UserStoreManager getUserStoreManager() throws UserStoreException {
RealmService realmService; RealmService realmService;
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
@ -366,7 +362,6 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
@Override @Override
public void run() { public void run() {
try { try {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
@ -460,6 +455,4 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
} }
} }
} }

@ -19,6 +19,7 @@ package org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.util;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.app.mgt.ApplicationManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -49,6 +50,26 @@ public class MDMServiceAPIUtils {
return deviceManagementProviderService; 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. * Returns the ApplicationManagementProviderService osgi service.
* *

Loading…
Cancel
Save