From c29bf24a756ff75c0975bb3da78e0a18e2efe37d Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 17 Nov 2016 17:54:25 +0530 Subject: [PATCH 1/4] Adding mobile devices to default groupes based on device ownership Adding mobile devices to default system generated groups based on the device ownership (BYOD, COPE) --- .../mgt/common/group/mgt/DeviceGroup.java | 7 ++ .../group/mgt/DeviceGroupConstants.java | 12 ++++ .../DeviceManagementProviderServiceImpl.java | 72 ++++++++++++++++++- .../GroupManagementProviderService.java | 25 ++++++- .../GroupManagementProviderServiceImpl.java | 49 +++++++++++++ 5 files changed, 161 insertions(+), 4 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 d4417bf58a..bb59bd1798 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 @@ -48,6 +48,13 @@ public class DeviceGroup implements Serializable { private Long dateOfLastUpdate; private String owner; + public DeviceGroup() {} + + public DeviceGroup(String name, String description) { + this.name = name; + this.description = description; + } + public int getGroupId() { return id; } 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 aab0fc16f3..488c8034e2 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,4 +79,16 @@ 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 1811cc36f3..37d9cb9fe3 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 @@ -20,11 +20,16 @@ package org.wso2.carbon.device.mgt.core.service; 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.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; 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.DeviceGroupConstants; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; @@ -130,7 +135,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return false; } deviceManager.enrollDevice(device); - if (deviceManager.isClaimable(deviceIdentifier)) { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); } else { @@ -221,6 +225,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv status = true; } + if (status) { + addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); + } return status; } @@ -1834,4 +1841,67 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return deviceManagementService.getDeviceManager(); } + /** + * Adds the enrolled devices to the default groups based on ownership + * + * @param deviceIdentifier 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) + 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); + } + 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 | GroupAlreadyExistException e) { + throw new DeviceManagementException("An error occurred when adding the device to the group.", e); + } + } + + /** + * Checks for the default group existence and create group based on device ownership + * + * @param service {@link GroupManagementProviderService} + * @param groupName of the group to create. + * @param groupDescription of the group to create. + * @return Group with details. + * @throws GroupManagementException + * @throws GroupAlreadyExistException + */ + private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName, + String groupDescription) throws GroupManagementException, GroupAlreadyExistException { + DeviceGroup defaultGroup = service.getGroup(groupName); + if (defaultGroup == null) { + defaultGroup = new DeviceGroup(groupName, groupDescription); + defaultGroup.setOwner(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); + defaultGroup.setDateOfCreation(new Date().getTime()); + defaultGroup.setDateOfLastUpdate(new Date().getTime()); + try { + service.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, + DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); + } catch (GroupAlreadyExistException e) { + if (log.isDebugEnabled()) { + log.debug("Default group: " + defaultGroup.getName() + " already exists.", e); + } + } + return service.getGroup(groupName); + } else { + return defaultGroup; + } + } } 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 4be6e298d8..7fcdb6708c 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 @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException; import java.util.List; @@ -76,6 +77,15 @@ public interface GroupManagementProviderService { */ DeviceGroup getGroup(int groupId) throws GroupManagementException; + /** + * Get the device group provided the device group id. + * + * @param groupName of the group. + * @return group with details. + * @throws GroupManagementException + */ + DeviceGroup getGroup(String groupName) throws GroupManagementException; + /** * Get all device groups in tenant. * @@ -211,15 +221,24 @@ public interface GroupManagementProviderService { */ int getDeviceCount(int groupId) throws GroupManagementException; + /** + * @param groupId of the group. + * @param deviceIdentifier of the device to add. + * @throws DeviceNotFoundException If device does not exist. + * @throws GroupManagementException If unable to add device to the group. + */ + void addDevice(int groupId, DeviceIdentifier deviceIdentifier) + throws DeviceNotFoundException, GroupManagementException; + /** * Add device to device group. * - * @param groupId of the group. + * @param groupId of the group. * @param deviceIdentifiers of devices. * @throws GroupManagementException */ - void addDevices(int groupId, List deviceIdentifiers) throws GroupManagementException, - DeviceNotFoundException; + void addDevices(int groupId, List deviceIdentifiers) + throws GroupManagementException, DeviceNotFoundException; /** * Remove device from device 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 575f19dcbe..a711a45da6 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 @@ -188,6 +188,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return deviceGroup; } + /** + * {@inheritDoc} + */ + @Override + public DeviceGroup getGroup(String groupName) throws GroupManagementException { + DeviceGroup deviceGroup; + try { + GroupManagementDAOFactory.openConnection(); + deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + } catch (GroupManagementDAOException e) { + throw new GroupManagementException("Error occurred while obtaining group with name: '" + groupName + "'", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + return deviceGroup; + } + @Override public List getGroups() throws GroupManagementException { List deviceGroups = new ArrayList<>(); @@ -587,6 +606,36 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } + /** + * {@inheritDoc} + */ + @Override + public void addDevice(int groupId, DeviceIdentifier deviceIdentifier) + throws DeviceNotFoundException, GroupManagementException { + Device device; + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + GroupManagementDAOFactory.beginTransaction(); + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier); + if (device == null) { + throw new DeviceNotFoundException("Device not found for id '" + deviceIdentifier.getId() + "'"); + } + if (!this.groupDAO.isDeviceMappedToGroup(groupId, device.getId(), tenantId)) { + this.groupDAO.addDevice(groupId, device.getId(), tenantId); + } + GroupManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementException e) { + throw new GroupManagementException("Error occurred while retrieving device.", e); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + throw new GroupManagementException("Error occurred while adding device to group.", e); + } catch (TransactionManagementException e) { + throw new GroupManagementException("Error occurred while initiating transaction.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + /** * {@inheritDoc} */ From 2164a30f8805313f2c6ba7bea858adb3e8c4d91c Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Mon, 21 Nov 2016 16:16:00 +0530 Subject: [PATCH 2/4] changing group owner to super user Changing the owner of the default system generated groups from the group creator to the super user of the system. --- .../DeviceManagementProviderServiceImpl.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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 37d9cb9fe3..6c62345aaa 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 @@ -37,6 +37,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; +import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; @@ -1868,7 +1869,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (DeviceNotFoundException e) { throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(), e); - } catch (GroupManagementException | GroupAlreadyExistException e) { + } catch (GroupManagementException | UserStoreException e) { throw new DeviceManagementException("An error occurred when adding the device to the group.", e); } } @@ -1876,19 +1877,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv /** * Checks for the default group existence and create group based on device ownership * - * @param service {@link GroupManagementProviderService} + * @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 - * @throws GroupAlreadyExistException */ private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName, - String groupDescription) throws GroupManagementException, GroupAlreadyExistException { + String groupDescription) throws GroupManagementException, UserStoreException { DeviceGroup defaultGroup = service.getGroup(groupName); if (defaultGroup == null) { defaultGroup = new DeviceGroup(groupName, groupDescription); - defaultGroup.setOwner(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); + defaultGroup.setOwner( + PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() + .getAdminUserName()); defaultGroup.setDateOfCreation(new Date().getTime()); defaultGroup.setDateOfLastUpdate(new Date().getTime()); try { @@ -1896,7 +1898,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); } catch (GroupAlreadyExistException e) { if (log.isDebugEnabled()) { - log.debug("Default group: " + defaultGroup.getName() + " already exists.", e); + log.debug("Default group: " + defaultGroup.getName() + " already exists. Skipping group creation.", + e); } } return service.getGroup(groupName); From 8ab73ce07d72ad9c3d6bf3ee3ca374c5f6396a91 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Mon, 21 Nov 2016 16:16:33 +0530 Subject: [PATCH 3/4] fixing issue with javadocs --- .../device/mgt/core/service/GroupManagementProviderService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7fcdb6708c..3aded5fc07 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 @@ -78,7 +78,7 @@ public interface GroupManagementProviderService { DeviceGroup getGroup(int groupId) throws GroupManagementException; /** - * Get the device group provided the device group id. + * Get the device group provided the device group name. * * @param groupName of the group. * @return group with details. From 2f431504fe817f3f72b0b535a16749de66f8a757 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Tue, 22 Nov 2016 13:25:27 +0530 Subject: [PATCH 4/4] 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 {