From 2f431504fe817f3f72b0b535a16749de66f8a757 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Tue, 22 Nov 2016 13:25:27 +0530 Subject: [PATCH] creating groups from ownership and changing group owner This commit includes following changes, Creating groups using the device ownership Changing the default system group owner to wso2.system.user --- .../mgt/common/group/mgt/DeviceGroup.java | 3 +- .../group/mgt/DeviceGroupConstants.java | 12 ------- .../DeviceManagementProviderServiceImpl.java | 31 ++++++------------- 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java index bb59bd1798..6617ab3bc8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java @@ -50,9 +50,8 @@ public class DeviceGroup implements Serializable { public DeviceGroup() {} - public DeviceGroup(String name, String description) { + public DeviceGroup(String name) { this.name = name; - this.description = description; } public int getGroupId() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java index 488c8034e2..aab0fc16f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java @@ -79,16 +79,4 @@ public class DeviceGroupConstants { public static final String[] DEFAULT_VIEW_EVENTS_PERMISSIONS = {"/permission/device-mgt/user/groups/device_events"}; } - - /** - * Holds the constants related to default (System Generated) groups. - */ - public static class DefaultGroups { - public static final String BYOD_GROUP_NAME = "BYOD"; - public static final String BYOD_GROUP_DESCRIPTION = "This is the default group for BYOD (Bring Your Own Device)" - + " type devices."; - public static final String COPE_GROUP_NAME = "COPE"; - public static final String COPE_GROUP_DESCRIPTION = "This is the default group for COPE (Corporate Owned) type" - + " devices."; - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 6c62345aaa..eeac1846d4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -19,8 +19,8 @@ package org.wso2.carbon.device.mgt.core.service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; @@ -1846,30 +1846,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * Adds the enrolled devices to the default groups based on ownership * * @param deviceIdentifier of the device. - * @param ownerShip of the device. + * @param ownership of the device. * @throws DeviceManagementException If error occurred in adding the device to the group. */ - private void addDeviceToGroups(DeviceIdentifier deviceIdentifier, EnrolmentInfo.OwnerShip ownerShip) + private void addDeviceToGroups(DeviceIdentifier deviceIdentifier, EnrolmentInfo.OwnerShip ownership) throws DeviceManagementException { GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); - DeviceGroup defaultGroup = null; try { - if (ownerShip == EnrolmentInfo.OwnerShip.BYOD) { - defaultGroup = createDefaultGroup(groupManagementProviderService, - DeviceGroupConstants.DefaultGroups.BYOD_GROUP_NAME, - DeviceGroupConstants.DefaultGroups.BYOD_GROUP_DESCRIPTION); - } else if (ownerShip == EnrolmentInfo.OwnerShip.COPE) { - defaultGroup = createDefaultGroup(groupManagementProviderService, - DeviceGroupConstants.DefaultGroups.COPE_GROUP_NAME, - DeviceGroupConstants.DefaultGroups.COPE_GROUP_DESCRIPTION); - } + DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString()); if (defaultGroup != null) { groupManagementProviderService.addDevice(defaultGroup.getGroupId(), deviceIdentifier); } } catch (DeviceNotFoundException e) { throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(), e); - } catch (GroupManagementException | UserStoreException e) { + } catch (GroupManagementException e) { throw new DeviceManagementException("An error occurred when adding the device to the group.", e); } } @@ -1879,18 +1870,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * * @param service {@link GroupManagementProviderService} instance. * @param groupName of the group to create. - * @param groupDescription of the group to create. * @return Group with details. * @throws GroupManagementException */ - private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName, - String groupDescription) throws GroupManagementException, UserStoreException { + private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName) + throws GroupManagementException { DeviceGroup defaultGroup = service.getGroup(groupName); if (defaultGroup == null) { - defaultGroup = new DeviceGroup(groupName, groupDescription); - defaultGroup.setOwner( - PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() - .getAdminUserName()); + defaultGroup = new DeviceGroup(groupName); + // Setting system level user (wso2.system.user) as the owner + defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME); defaultGroup.setDateOfCreation(new Date().getTime()); defaultGroup.setDateOfLastUpdate(new Date().getTime()); try {