From c29bf24a756ff75c0975bb3da78e0a18e2efe37d Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 17 Nov 2016 17:54:25 +0530 Subject: [PATCH] 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} */