Merge pull request 'Fix analytics publishing for Windows devices' (#240) from navodzoysa/device-mgt-core:fix-windows-analytics into master

Reviewed-on: community/device-mgt-core#240
revert
commit 51f1bf1280

@ -22,6 +22,7 @@ import org.apache.commons.lang.StringUtils;
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.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceDetailsWrapper; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceDetailsWrapper;
@ -232,9 +233,19 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceDetailsWrapper.setGroups(groups); deviceDetailsWrapper.setGroups(groups);
} }
String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(CarbonContext String username = CarbonContext.getThreadLocalCarbonContext().getUsername();
.getThreadLocalCarbonContext() if (StringUtils.isEmpty(username)) {
.getUsername()); boolean isUserAuthorized = DeviceManagementDataHolder.getInstance().
getDeviceAccessAuthorizationService().isUserAuthorized(
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()),
device.getEnrolmentInfo().getOwner()
);
if (isUserAuthorized) {
username = device.getEnrolmentInfo().getOwner();
}
}
String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(username);
if (rolesOfUser != null && rolesOfUser.length > 0) { if (rolesOfUser != null && rolesOfUser.length > 0) {
deviceDetailsWrapper.setRole(rolesOfUser); deviceDetailsWrapper.setRole(rolesOfUser);
} }
@ -248,6 +259,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
log.error("Error occurred while getting group list", e); log.error("Error occurred while getting group list", e);
} catch (UserStoreException e) { } catch (UserStoreException e) {
log.error("Error occurred while getting role list", e); log.error("Error occurred while getting role list", e);
} catch (DeviceAccessAuthorizationException e) {
log.error("User with name '" + device.getEnrolmentInfo().getOwner() +
"' is unauthorized to publish events for device with the id '" +
device.getDeviceIdentifier() + "'", e);
} }
} else { } else {
if(log.isTraceEnabled()) { if(log.isTraceEnabled()) {

@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.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.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
@ -31,6 +32,7 @@ import org.json.JSONObject;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.EventPublishingException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.EventPublishingException;
import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException;
public class HttpReportingUtil { public class HttpReportingUtil {
@ -56,6 +58,9 @@ public class HttpReportingUtil {
apiEndpoint.setEntity(requestEntity); apiEndpoint.setEntity(requestEntity);
HttpResponse response = client.execute(apiEndpoint); HttpResponse response = client.execute(apiEndpoint);
return response.getStatusLine().getStatusCode(); return response.getStatusLine().getStatusCode();
} catch (ConnectException e) {
log.error("Connection refused to API endpoint: " + endpoint, e);
return HttpStatus.SC_SERVICE_UNAVAILABLE;
} catch (IOException e) { } catch (IOException e) {
throw new EventPublishingException("Error occurred when " + throw new EventPublishingException("Error occurred when " +
"invoking API. API endpoint: " + endpoint, e); "invoking API. API endpoint: " + endpoint, e);

Loading…
Cancel
Save