Updated group management APIs and service

4.x.x
charitha 8 years ago
parent 726bbcd35d
commit a2db592d69

@ -1,58 +0,0 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.mgt.jaxrs.beans;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.List;
/**
* This class holds Device Group user name and assigned roles of user for particular group. Exposed to external access.
*/
@ApiModel(value = "DeviceGroupShare", description = "This class carries roles assigned to a user of a managed device group.")
public class DeviceGroupShare implements Serializable {
private static final long serialVersionUID = 1998141711L;
@ApiModelProperty(name = "username", value = "Username of the user.", required = true)
private String username;
@ApiModelProperty(name = "roles", value = "List of roles assigned to user from the group.")
private List<String> groupRoles;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<String> getGroupRoles() {
return groupRoles;
}
public void setGroupRoles(List<String> groupRoles) {
this.groupRoles = groupRoles;
}
}

@ -1,54 +0,0 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.mgt.jaxrs.beans;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser;
import java.util.ArrayList;
import java.util.List;
public class DeviceGroupUsersList extends BasePaginatedResult {
@ApiModelProperty(value = "List of device group users returned")
@JsonProperty("users")
private List<GroupUser> users = new ArrayList<>();
public List<GroupUser> getList() {
return users;
}
public void setList(List<GroupUser> users) {
this.users = users;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{\n");
sb.append(" count: ").append(getCount()).append(",\n");
sb.append(" users: [").append(users).append("\n");
sb.append("]}\n");
return sb.toString();
}
}

@ -36,10 +36,9 @@ import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupUsersList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
import javax.validation.Valid; import javax.validation.Valid;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@ -70,12 +69,14 @@ import java.util.List;
} }
), ),
tags = { tags = {
@Tag(name = "device_management", description = "") @Tag(name = "device_management", description = "Device group related REST-API. " +
"This can be used to manipulated device group related " +
"details.")
} }
) )
@Path("/groups") @Path("/groups")
@Api(value = "Device Group Management", description = "This API carries all device group management related operations " + @Api(value = "Device Group Management", description = "This API carries all device group management related " +
"such as get all the available groups, etc.") "operations such as get all the available groups, etc.")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public interface GroupManagementService { public interface GroupManagementService {
@ -482,10 +483,10 @@ public interface GroupManagementService {
required = true) required = true)
@PathParam("groupId") int groupId, @PathParam("groupId") int groupId,
@ApiParam( @ApiParam(
name = "deviceGroupShare", name = "userRoles",
value = "User name and the assigned roles for the share.", value = "User roles to share group with.",
required = true) required = true)
@Valid DeviceGroupShare deviceGroupShare); @Valid List<String> userRoles);
@Path("/id/{groupId}/roles") @Path("/id/{groupId}/roles")
@GET @GET
@ -505,7 +506,7 @@ public interface GroupManagementService {
) )
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the users.", @ApiResponse(code = 200, message = "OK. \n Successfully fetched the users.",
response = DeviceGroupUsersList.class, response = RoleList.class,
responseHeaders = { responseHeaders = {
@ResponseHeader( @ResponseHeader(
name = "Content-Type", name = "Content-Type",

@ -34,7 +34,6 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
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.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService;
@ -164,10 +163,10 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
@Override @Override
public Response manageGroupSharing(int groupId, DeviceGroupShare deviceGroupShare) { public Response manageGroupSharing(int groupId, List<String> userRoles) {
try { try {
DeviceMgtAPIUtils.getGroupManagementProviderService() DeviceMgtAPIUtils.getGroupManagementProviderService()
.manageGroupSharing(groupId, deviceGroupShare.getGroupRoles()); .manageGroupSharing(groupId, userRoles);
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
String msg = "Error occurred while managing group share."; String msg = "Error occurred while managing group share.";

@ -21,7 +21,18 @@ 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.CarbonConstants;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
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;
@ -37,8 +48,12 @@ 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.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; 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.ApplicationDAO;
import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -53,7 +68,13 @@ import org.wso2.carbon.email.sender.core.TypedValue;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService,
PluginInitializationListener { PluginInitializationListener {
@ -1855,7 +1876,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString()); DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString());
if (defaultGroup != null) { if (defaultGroup != null) {
groupManagementProviderService.addDevice(defaultGroup.getGroupId(), deviceIdentifier); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(deviceIdentifier);
groupManagementProviderService.addDevices(defaultGroup.getGroupId(), deviceIdentifiers);
} }
} 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(),
@ -1880,8 +1903,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
defaultGroup = new DeviceGroup(groupName); defaultGroup = new DeviceGroup(groupName);
// Setting system level user (wso2.system.user) as the owner // Setting system level user (wso2.system.user) as the owner
defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME); defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
defaultGroup.setDateOfCreation(new Date().getTime());
defaultGroup.setDateOfLastUpdate(new Date().getTime());
try { try {
service.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, service.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE,
DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS);

@ -27,7 +27,6 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; 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.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException;
import java.util.List; import java.util.List;
@ -175,15 +174,6 @@ 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.
* *

@ -459,36 +459,6 @@ 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