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)
revert-70aa11f8
Madawa Soysa 8 years ago
parent 023b554e0f
commit c29bf24a75

@ -48,6 +48,13 @@ public class DeviceGroup implements Serializable {
private Long dateOfLastUpdate; private Long dateOfLastUpdate;
private String owner; private String owner;
public DeviceGroup() {}
public DeviceGroup(String name, String description) {
this.name = name;
this.description = description;
}
public int getGroupId() { public int getGroupId() {
return id; return id;
} }

@ -79,4 +79,16 @@ public class DeviceGroupConstants {
public static final String[] DEFAULT_VIEW_EVENTS_PERMISSIONS = public static final String[] DEFAULT_VIEW_EVENTS_PERMISSIONS =
{"/permission/device-mgt/user/groups/device_events"}; {"/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.";
}
} }

@ -20,11 +20,16 @@ package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; 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.*;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; 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.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; 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.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.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
@ -130,7 +135,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return false; return false;
} }
deviceManager.enrollDevice(device); deviceManager.enrollDevice(device);
if (deviceManager.isClaimable(deviceIdentifier)) { if (deviceManager.isClaimable(deviceIdentifier)) {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE);
} else { } else {
@ -221,6 +225,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
status = true; status = true;
} }
if (status) {
addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership());
}
return status; return status;
} }
@ -1834,4 +1841,67 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return deviceManagementService.getDeviceManager(); 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;
}
}
} }

@ -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.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; 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.common.group.mgt.RoleDoesNotExistException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException;
import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException; import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException;
import java.util.List; import java.util.List;
@ -76,6 +77,15 @@ public interface GroupManagementProviderService {
*/ */
DeviceGroup getGroup(int groupId) throws GroupManagementException; 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. * Get all device groups in tenant.
* *
@ -211,6 +221,15 @@ public interface GroupManagementProviderService {
*/ */
int getDeviceCount(int groupId) throws GroupManagementException; 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. * Add device to device group.
* *
@ -218,8 +237,8 @@ public interface GroupManagementProviderService {
* @param deviceIdentifiers of devices. * @param deviceIdentifiers of devices.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers) throws GroupManagementException, void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers)
DeviceNotFoundException; throws GroupManagementException, DeviceNotFoundException;
/** /**
* Remove device from device group. * Remove device from device group.

@ -188,6 +188,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return deviceGroup; 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 @Override
public List<DeviceGroup> getGroups() throws GroupManagementException { public List<DeviceGroup> getGroups() throws GroupManagementException {
List<DeviceGroup> deviceGroups = new ArrayList<>(); List<DeviceGroup> 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} * {@inheritDoc}
*/ */

Loading…
Cancel
Save