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
revert-70aa11f8
Madawa Soysa 8 years ago
parent 8ab73ce07d
commit 2f431504fe

@ -50,9 +50,8 @@ public class DeviceGroup implements Serializable {
public DeviceGroup() {} public DeviceGroup() {}
public DeviceGroup(String name, String description) { public DeviceGroup(String name) {
this.name = name; this.name = name;
this.description = description;
} }
public int getGroupId() { public int getGroupId() {

@ -79,16 +79,4 @@ 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.";
}
} }

@ -19,8 +19,8 @@ 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.CarbonConstants;
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;
@ -1846,30 +1846,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
* Adds the enrolled devices to the default groups based on ownership * Adds the enrolled devices to the default groups based on ownership
* *
* @param deviceIdentifier of the device. * @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. * @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 { throws DeviceManagementException {
GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl();
DeviceGroup defaultGroup = null;
try { try {
if (ownerShip == EnrolmentInfo.OwnerShip.BYOD) { DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString());
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) { if (defaultGroup != null) {
groupManagementProviderService.addDevice(defaultGroup.getGroupId(), deviceIdentifier); groupManagementProviderService.addDevice(defaultGroup.getGroupId(), deviceIdentifier);
} }
} catch (DeviceNotFoundException e) { } catch (DeviceNotFoundException e) {
throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(), throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(),
e); e);
} catch (GroupManagementException | UserStoreException e) { } catch (GroupManagementException e) {
throw new DeviceManagementException("An error occurred when adding the device to the group.", 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 service {@link GroupManagementProviderService} instance.
* @param groupName of the group to create. * @param groupName of the group to create.
* @param groupDescription of the group to create.
* @return Group with details. * @return Group with details.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName, private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName)
String groupDescription) throws GroupManagementException, UserStoreException { throws GroupManagementException {
DeviceGroup defaultGroup = service.getGroup(groupName); DeviceGroup defaultGroup = service.getGroup(groupName);
if (defaultGroup == null) { if (defaultGroup == null) {
defaultGroup = new DeviceGroup(groupName, groupDescription); defaultGroup = new DeviceGroup(groupName);
defaultGroup.setOwner( // Setting system level user (wso2.system.user) as the owner
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
.getAdminUserName());
defaultGroup.setDateOfCreation(new Date().getTime()); defaultGroup.setDateOfCreation(new Date().getTime());
defaultGroup.setDateOfLastUpdate(new Date().getTime()); defaultGroup.setDateOfLastUpdate(new Date().getTime());
try { try {

Loading…
Cancel
Save