From cecdda4dbf91e11209d131d7024570a8eda6bf6b Mon Sep 17 00:00:00 2001 From: inoshperera Date: Tue, 3 Mar 2020 21:18:09 +0530 Subject: [PATCH 1/2] Add group and roles --- .../device/details/DeviceDetailsWrapper.java | 20 +++++ .../impl/DeviceInformationManagerImpl.java | 86 +++++++++++++++---- .../GroupManagementProviderService.java | 10 +++ .../GroupManagementProviderServiceImpl.java | 36 ++++++++ .../mgt/core/util/DeviceManagerUtil.java | 31 +++++++ .../configuration.hbs | 15 ++++ .../public/js/platform-configuration.js | 13 ++- 7 files changed, 194 insertions(+), 17 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java index 80f637c880..975d27b50f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.common.device.details; import com.google.gson.Gson; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import java.util.List; @@ -31,6 +32,25 @@ public class DeviceDetailsWrapper { DeviceLocation location; int tenantId; + List groups; + String [] role; + + public List getGroups() { + return groups; + } + + public void setGroups(List groups) { + this.groups = groups; + } + + public String [] getRole() { + return role; + } + + public void setRole(String [] role) { + this.role = role; + } + public int getTenantId() { return tenantId; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 4d479b5207..dc3f5547a1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -22,16 +22,21 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -40,11 +45,15 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManag import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.HashMap; import java.util.List; @@ -57,6 +66,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream"; + private static final String IS_EVENT_PUBLISHING_ENABED = "isEventPublishingEnabled"; public DeviceInformationManagerImpl() { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); @@ -69,6 +79,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { Device device = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider().getDevice(deviceId, false); + publishEvents(device, deviceInfo); + DeviceManagementDAOFactory.beginTransaction(); DeviceInfo newDeviceInfo; DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), @@ -94,18 +106,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { device.getEnrolmentInfo().getId()); DeviceManagementDAOFactory.commitTransaction(); - String reportingHost = System.getProperty(DeviceManagementConstants.Report - .REPORTING_EVENT_HOST); - if (reportingHost != null && !reportingHost.isEmpty()) { - DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); - deviceDetailsWrapper.setDevice(device); - deviceDetailsWrapper.setDeviceInfo(deviceInfo); - deviceDetailsWrapper.getJSONString(); - - HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), - reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); - } - //TODO :: This has to be fixed by adding the enrollment ID. if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; @@ -151,14 +151,54 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (DataPublisherConfigurationException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); - } catch (EventPublishingException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while sending events", e); - } finally { + }finally { DeviceManagementDAOFactory.closeConnection(); } } + private void publishEvents(Device device, DeviceInfo deviceInfo) { + String reportingHost = System.getProperty(DeviceManagementConstants.Report + .REPORTING_EVENT_HOST); + if (reportingHost != null && !reportingHost.isEmpty() && isPublishingEnabledForTenant()) { + try { + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setDevice(device); + deviceDetailsWrapper.setDeviceInfo(deviceInfo); + deviceDetailsWrapper.getJSONString(); + GroupManagementProviderService groupManagementService = DeviceManagementDataHolder + .getInstance().getGroupManagementProviderService(); + + List groups = groupManagementService.getGroups(device, false); + if (groups != null && groups.size() > 0) { + deviceDetailsWrapper.setGroups(groups); + } + + String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext() + .getUsername()); + if (rolesOfUser != null && rolesOfUser.length > 0) { + deviceDetailsWrapper.setRole(rolesOfUser); + } + + HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), + reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); + } catch (EventPublishingException e) { + log.error("Error occurred while sending events", e); + } catch (GroupManagementException e) { + log.error("Error occurred while getting group list", e); + } catch (UserStoreException e) { + log.error("Error occurred while getting role list", e); + } + } + } + + private boolean isPublishingEnabledForTenant() { + Object configuration = DeviceManagerUtil.getConfiguration(IS_EVENT_PUBLISHING_ENABED); + if (configuration != null) { + return Boolean.valueOf(configuration.toString()); + } + return false; + } + @Override public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { Device device = getDevice(deviceId); @@ -393,5 +433,19 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { return newDeviceInfo; } + private String[] getRolesOfUser(String userName) throws UserStoreException { + UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String[] roleList; + if (userRealm != null) { + userRealm.getUserStoreManager().getRoleNames(); + roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); + } else { + String msg = "User realm is not initiated. Logged in user: " + userName; + log.error(msg); + throw new UserStoreException(msg); + } + return roleList; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 4300ce232b..cca1768bd1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -243,6 +243,16 @@ public interface GroupManagementProviderService { */ List getGroups(DeviceIdentifier deviceIdentifier, boolean requireGroupProps) throws GroupManagementException; + /** + * Get groups which contains particular device. + * + * @param device interested devoce. + * @return groups contain the device. + * @throws GroupManagementException + */ + public List getGroups(Device device, boolean requireGroupProps) + throws GroupManagementException; + /** * Checks for the default group existence and create group based on device ownership. * @param groupName of the group diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index efe71421db..0fc3e978c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -938,6 +938,42 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } + @Override + public List getGroups(Device device, boolean requireGroupProps) + throws GroupManagementException { + if (device.getDeviceIdentifier() == null) { + String msg = "Received empty device identifier for getGroups"; + log.error(msg); + throw new GroupManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Get groups of device " + device.getDeviceIdentifier()); + } + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + GroupManagementDAOFactory.openConnection(); + List deviceGroups = groupDAO.getGroups(device.getId(), tenantId); + if (requireGroupProps) { + if (deviceGroups != null && !deviceGroups.isEmpty()) { + for (DeviceGroup group : deviceGroups) { + populateGroupProperties(group, tenantId); + } + } + } + return deviceGroups; + } catch (GroupManagementDAOException | SQLException e) { + String msg = "Error occurred while retrieving device groups."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getGroups"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + /** * {@inheritDoc} */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 1890d399cd..c137432f8e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -38,6 +38,10 @@ import org.wso2.carbon.device.mgt.common.ApplicationRegistration; import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; @@ -53,6 +57,8 @@ import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; +import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; @@ -95,6 +101,7 @@ import java.util.concurrent.TimeUnit; public final class DeviceManagerUtil { private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); + public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; private static boolean isDeviceCacheInitialized = false; @@ -694,4 +701,28 @@ public final class DeviceManagerUtil { JWTClient jwtClient = jwtClientManagerService.getJWTClient(); return jwtClient.getAccessToken(clientId, clientSecret, deviceOwner, scopes); } + + public static Object getConfiguration(String key) { + + PlatformConfigurationManagementService configMgtService = + new PlatformConfigurationManagementServiceImpl(); + + try { + PlatformConfiguration tenantConfiguration = configMgtService.getConfiguration + (GENERAL_CONFIG_RESOURCE_PATH); + List configuration = tenantConfiguration.getConfiguration(); + + if (configuration != null && !configuration.isEmpty()) { + for (ConfigurationEntry cEntry : configuration) { + if (key.equalsIgnoreCase(cEntry.getName())) { + return cEntry.getValue(); + } + } + } + } catch (ConfigurationManagementException e) { + log.error("Error while getting the configurations from registry.", e); + return null; + } + return null; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs index 8e4131fa19..b927bfadb9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs @@ -109,6 +109,21 @@ class="form-control" placeholder="[ Required Field ]"> +
+ + +
diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js index 9e4176066b..a9bf2bb255 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js @@ -22,7 +22,8 @@ $(document).ready(function () { var configParams = { "NOTIFIER_TYPE": "notifierType", - "NOTIFIER_FREQUENCY": "notifierFrequency" + "NOTIFIER_FREQUENCY": "notifierFrequency", + "IS_EVENT_PUBLISHING_ENABLED": "isEventPublishingEnabled" }; var responseCodes = { @@ -51,6 +52,8 @@ $(document).ready(function () { var config = data.configuration[i]; if (config.name == configParams["NOTIFIER_FREQUENCY"]) { $("input#monitoring-config-frequency").val(config.value / 1000); + } else if (config.name == configParams["IS_EVENT_PUBLISHING_ENABLED"]) { + $("select#publish-for-analytics").val(config.value); } } } @@ -65,6 +68,7 @@ $(document).ready(function () { */ $("button#save-general-btn").click(function () { var notifierFrequency = $("input#monitoring-config-frequency").val(); + var publishEvents = $("select#publish-for-analytics").val(); var errorMsgWrapper = "#email-config-error-msg"; var errorMsg = "#email-config-error-msg span"; @@ -84,6 +88,13 @@ $(document).ready(function () { "contentType": "text" }; + var publishEventsDetails = { + "name": configParams["IS_EVENT_PUBLISHING_ENABLED"], + "value": publishEvents, + "contentType": "text" + }; + + configList.push(publishEventsDetails); configList.push(monitorFrequency); addConfigFormData.configuration = configList; From 05fa975887752c470c8f5218d1c9457ca8813891 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Tue, 3 Mar 2020 21:44:14 +0530 Subject: [PATCH 2/2] mend --- .../mgt/impl/DeviceInformationManagerImpl.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 90a2b2a934..f5b7d141dc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -124,18 +124,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); DeviceManagementDAOFactory.commitTransaction(); - String reportingHost = System.getProperty(DeviceManagementConstants.Report - .REPORTING_EVENT_HOST); - if (reportingHost != null && !reportingHost.isEmpty()) { - DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); - deviceDetailsWrapper.setDevice(device); - deviceDetailsWrapper.setDeviceInfo(deviceInfo); - deviceDetailsWrapper.getJSONString(); - - HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), - reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); - } - //TODO :: This has to be fixed by adding the enrollment ID. if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; @@ -181,9 +169,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (DataPublisherConfigurationException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); - } catch (EventPublishingException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while sending events", e); } finally { DeviceManagementDAOFactory.closeConnection(); }