Merge branch 'master' into 'master'

insert user improvement for Google EMM

See merge request entgra/carbon-device-mgt-plugins!111
merge-requests/112/head
Dharmakeerthi Lasantha 5 years ago
commit d89fd35cb4

@ -110,11 +110,15 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme
+ " , deviceId : " + deviceId); + " , deviceId : " + deviceId);
} }
} catch (OperationManagementException | InvalidDeviceException e) { } catch (OperationManagementException | InvalidDeviceException e) {
throw new RemoteSessionManagementException("Error occurred while adding initial operation for the " + String msg = "Error occurred while adding initial operation for the " +
"device Type : " + deviceType + " , deviceId : " + deviceId); "device Type : " + deviceType + " , deviceId : " + deviceId;
log.error(msg, e);
throw new RemoteSessionManagementException(msg);
} catch (DeviceAccessAuthorizationException e) { } catch (DeviceAccessAuthorizationException e) {
throw new RemoteSessionManagementException("Error occurred while device access authorization for the " + String msg = "Error occurred while device access authorization for the " +
"device Type : " + deviceType + " , " + "deviceId : " + deviceId); "device Type : " + deviceType + " , " + "deviceId : " + deviceId;
log.error(msg, e);
throw new RemoteSessionManagementException(msg);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

@ -99,11 +99,27 @@ public class AndroidEnterpriseServiceImpl implements AndroidEnterpriseService {
.build(); .build();
} }
String token;
try {
token = insertUser(enterpriseUser);
if (token == null) {
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Error when fetching token").build())
.build();
}
} catch (EnterpriseServiceException e) {
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Error when saving user").build()).build();
}
return Response.status(Response.Status.OK).entity(token).build();
}
public String insertUser(EnterpriseUser enterpriseUser) throws EnterpriseServiceException {
EnterpriseConfigs enterpriseConfigs = AndroidEnterpriseUtils.getEnterpriseConfigs(); EnterpriseConfigs enterpriseConfigs = AndroidEnterpriseUtils.getEnterpriseConfigs();
String token; String token;
boolean deviceIdExist = false; boolean deviceIdExist = false;
try {
String googleUserId; String googleUserId;
List<AndroidEnterpriseUser> androidEnterpriseUsers = AndroidAPIUtils.getAndroidPluginService() List<AndroidEnterpriseUser> androidEnterpriseUsers = AndroidAPIUtils.getAndroidPluginService()
.getEnterpriseUser(CarbonContext.getThreadLocalCarbonContext().getUsername()); .getEnterpriseUser(CarbonContext.getThreadLocalCarbonContext().getUsername());
@ -136,17 +152,9 @@ public class AndroidEnterpriseServiceImpl implements AndroidEnterpriseService {
AndroidAPIUtils.getAndroidPluginService().addEnterpriseUser(androidEnterpriseUser); AndroidAPIUtils.getAndroidPluginService().addEnterpriseUser(androidEnterpriseUser);
} }
if (token == null) {
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Error when fetching token").build())
.build();
}
} catch (EnterpriseServiceException e) { return token;
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Error when saving user").build()).build();
}
return Response.status(Response.Status.OK).entity(token).build();
} }
@Override @Override

@ -54,9 +54,11 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceExce
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.mobile.android.impl.EnterpriseServiceException;
import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; import org.wso2.carbon.mdm.services.android.bean.ErrorResponse;
import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidApplication; import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidApplication;
import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidDevice; import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidDevice;
import org.wso2.carbon.mdm.services.android.bean.wrapper.EnterpriseUser;
import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException;
import org.wso2.carbon.mdm.services.android.services.DeviceManagementService; import org.wso2.carbon.mdm.services.android.services.DeviceManagementService;
import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils;
@ -92,6 +94,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
private static final String OPERATION_ERROR_STATUS = "ERROR"; private static final String OPERATION_ERROR_STATUS = "ERROR";
private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class); private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class);
public static final String GOOGLE_AFW_EMM_ANDROID_ID = "googleEMMAndroidId";
public static final String GOOGLE_AFW_DEVICE_ID = "googleEMMDeviceId";
@PUT @PUT
@Path("/{id}/applications") @Path("/{id}/applications")
@ -233,6 +237,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build());
} }
try { try {
String token = null;
Device device = new Device(); Device device = new Device();
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
device.setEnrolmentInfo(androidDevice.getEnrolmentInfo()); device.setEnrolmentInfo(androidDevice.getEnrolmentInfo());
@ -244,6 +249,27 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
device.setFeatures(androidDevice.getFeatures()); device.setFeatures(androidDevice.getFeatures());
device.setProperties(androidDevice.getProperties()); device.setProperties(androidDevice.getProperties());
String googleEMMAndroidId = null;
String googleEMMDeviceId = null;
if (androidDevice.getProperties() != null) {
for (Device.Property property : androidDevice.getProperties()) {
if (property.getName().equals(GOOGLE_AFW_EMM_ANDROID_ID)) {
googleEMMAndroidId = property.getValue();
} else if (property.getName().equals(GOOGLE_AFW_DEVICE_ID)) {
googleEMMDeviceId = property.getValue();
}
}
if (googleEMMAndroidId != null && googleEMMDeviceId != null) {
EnterpriseUser user = new EnterpriseUser();
user.setAndroidPlayDeviceId(googleEMMAndroidId);
user.setEmmDeviceIdentifier(googleEMMDeviceId);
AndroidEnterpriseServiceImpl enterpriseService = new AndroidEnterpriseServiceImpl();
token = enterpriseService.insertUser(user);
}
}
boolean status = AndroidAPIUtils.getDeviceManagementService().enrollDevice(device); boolean status = AndroidAPIUtils.getDeviceManagementService().enrollDevice(device);
if (status) { if (status) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(androidDevice.getDeviceIdentifier(), DeviceIdentifier deviceIdentifier = new DeviceIdentifier(androidDevice.getDeviceIdentifier(),
@ -301,8 +327,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
Message responseMessage = new Message(); Message responseMessage = new Message();
responseMessage.setResponseCode(Response.Status.OK.toString()); responseMessage.setResponseCode(Response.Status.OK.toString());
if (token == null) {
responseMessage.setResponseMessage("Android device, which carries the id '" + responseMessage.setResponseMessage("Android device, which carries the id '" +
androidDevice.getDeviceIdentifier() + "' has successfully been enrolled"); androidDevice.getDeviceIdentifier() + "' has successfully been enrolled");
} else {
responseMessage.setResponseMessage("Google response token" + token);
}
return Response.status(Response.Status.OK).entity(responseMessage).build(); return Response.status(Response.Status.OK).entity(responseMessage).build();
} else { } else {
Message responseMessage = new Message(); Message responseMessage = new Message();
@ -339,6 +369,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (EnterpriseServiceException e) {
String msg = "Error occurred while adding user via Google Apis '" +
androidDevice.getDeviceIdentifier() + "'";
log.error(msg, e);
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
} }

@ -479,7 +479,8 @@ $(document).ready(function () {
showPopup(); showPopup();
$("a#remove-unenroll-yes-link").click(function () { $("a#remove-unenroll-yes-link").click(function () {
var serverDetails = $("input#afw-server-details").val() + "/api/device-mgt/android/v1.0/enterprise/324/unenroll"; var serverDetails = $("input#afw-server-details").val()
+ "/api/android-for-work/v1.0/google/enterprise/unenroll/" + $("input#afw-enterprise-id").val();
var emmToken = $("input#afw-backend-token").val(); var emmToken = $("input#afw-backend-token").val();
unenroll(serverDetails, emmToken); unenroll(serverDetails, emmToken);
hidePopup(); hidePopup();

Loading…
Cancel
Save