diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 8331edd653d..c048bd44225 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -22,6 +22,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceDetailsWrapper; @@ -232,9 +233,19 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDetailsWrapper.setGroups(groups); } - String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(CarbonContext - .getThreadLocalCarbonContext() - .getUsername()); + String username = CarbonContext.getThreadLocalCarbonContext().getUsername(); + if (StringUtils.isEmpty(username)) { + 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) { deviceDetailsWrapper.setRole(rolesOfUser); } @@ -248,6 +259,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { log.error("Error occurred while getting group list", e); } catch (UserStoreException 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 { if(log.isTraceEnabled()) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/HttpReportingUtil.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/HttpReportingUtil.java index 9b565b8048f..288d0c0fc51 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/HttpReportingUtil.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/HttpReportingUtil.java @@ -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.LogFactory; import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; 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.core.DeviceManagementConstants; import java.io.IOException; +import java.net.ConnectException; public class HttpReportingUtil { @@ -56,6 +58,9 @@ public class HttpReportingUtil { apiEndpoint.setEntity(requestEntity); HttpResponse response = client.execute(apiEndpoint); return response.getStatusLine().getStatusCode(); + } catch (ConnectException e) { + log.error("Connection refused to API endpoint: " + endpoint, e); + return HttpStatus.SC_SERVICE_UNAVAILABLE; } catch (IOException e) { throw new EventPublishingException("Error occurred when " + "invoking API. API endpoint: " + endpoint, e);