Patch getGroup in fail safe manner

pull/186/head
Rajitha Kumara 1 year ago
parent 2dd3e86f69
commit 3b4f159bf6

@ -533,7 +533,14 @@ public interface GroupManagementService {
defaultValue = "1") defaultValue = "1")
@DefaultValue("1") @DefaultValue("1")
@QueryParam("depth") @QueryParam("depth")
int depth); int depth,
@ApiParam(
name = "allowed",
value = "Whether to return allowed group",
defaultValue = "false")
@QueryParam("allowed")
@DefaultValue("false")
boolean allowed);
@Path("/name/{groupName}") @Path("/name/{groupName}")
@GET @GET

@ -176,10 +176,11 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
@Override @Override
public Response getGroup(int groupId, boolean requireGroupProps, int depth) { public Response getGroup(int groupId, boolean requireGroupProps, int depth, boolean allowed) {
try { try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps, depth); DeviceGroup deviceGroup = allowed ? service.getUserOwnGroup(groupId, requireGroupProps, depth):
service.getGroup(groupId, requireGroupProps, depth);
if (deviceGroup != null) { if (deviceGroup != null) {
return Response.status(Response.Status.OK).entity(deviceGroup).build(); return Response.status(Response.Status.OK).entity(deviceGroup).build();
} else { } else {

@ -176,13 +176,13 @@ public class GroupManagementServiceImplTest {
Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false, 1); Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false, 1);
Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false, 1); Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false, 1);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false, 1); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false, 1);
Response response = groupManagementService.getGroup(1, false, 1); Response response = groupManagementService.getGroup(1, false, 1, false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getGroup request failed for a request with valid parameters"); "getGroup request failed for a request with valid parameters");
response = groupManagementService.getGroup(2, false, 1); response = groupManagementService.getGroup(2, false, 1, false);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(),
"getGroup request returned a group for a non-existing group"); "getGroup request returned a group for a non-existing group");
response = groupManagementService.getGroup(3, false, 1); response = groupManagementService.getGroup(3, false, 1, false);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"getGroup request returned a group for a in-valid request"); "getGroup request returned a group for a in-valid request");
} }

@ -351,4 +351,6 @@ public interface GroupManagementProviderService {
* @throws GroupManagementException * @throws GroupManagementException
*/ */
DeviceTypesOfGroups getDeviceTypesOfGroups(List<String> identifiers) throws GroupManagementException; DeviceTypesOfGroups getDeviceTypesOfGroups(List<String> identifiers) throws GroupManagementException;
DeviceGroup getUserOwnGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException;
} }

@ -636,8 +636,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
List<DeviceGroup> tree = new ArrayList<>(); List<DeviceGroup> tree = new ArrayList<>();
for (DeviceGroup deviceGroup : groups) { for (DeviceGroup deviceGroup : groups) {
DeviceGroup treeNode = tree.stream(). DeviceGroup treeNode = tree.stream().
filter(node -> deviceGroup.getParentPath(). filter(node -> Arrays.stream(deviceGroup.getParentPath().split("/")).
contains(Integer.toString(node.getGroupId()))). collect(Collectors.toList()).contains(Integer.toString(node.getGroupId()))).
findFirst().orElse(null); findFirst().orElse(null);
if (treeNode != null) { if (treeNode != null) {
if (Objects.equals(treeNode.getParentPath(), deviceGroup.getParentPath())) { if (Objects.equals(treeNode.getParentPath(), deviceGroup.getParentPath())) {
@ -657,6 +657,47 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return tree; return tree;
} }
private DeviceGroup findGroupFromTree(List<DeviceGroup> tree, int groupId) {
for (DeviceGroup node: tree) {
if (node.getGroupId() == groupId) return node;
if (node.getChildrenGroups() != null)
return findGroupFromTree(node.getChildrenGroups(), groupId);
}
return null;
}
@Override
public DeviceGroup getUserOwnGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String username = ctx.getUsername();
int tenantId = ctx.getTenantId();
List<Integer> userOwnGroupIds = this.getGroupIds(username);
if (userOwnGroupIds == null) {
String msg = "Retrieved null when getting group ids for user " + username;
log.error(msg);
throw new GroupManagementException(msg);
}
try {
GroupManagementDAOFactory.openConnection();
DeviceGroup deviceGroup = findGroupFromTree(
getGroups(userOwnGroupIds, tenantId), groupId);
if (deviceGroup != null && requireGroupProps)
populateGroupProperties(deviceGroup, tenantId);
return deviceGroup;
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while obtaining group '" + groupId + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source to retrieve all groups "
+ "with hierarchy";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Override @Override
public List<DeviceGroup> getGroups(String username, boolean requireGroupProps) throws GroupManagementException { public List<DeviceGroup> getGroups(String username, boolean requireGroupProps) throws GroupManagementException {
if (username == null || username.isEmpty()) { if (username == null || username.isEmpty()) {

Loading…
Cancel
Save