Fixes for grouping issues (#186)

Co-authored-by: rajitha <rajitha@entgra.io>
Reviewed-on: community/device-mgt-core#186
Co-authored-by: Rajitha Kumara <rajitha@entgra.io>
Co-committed-by: Rajitha Kumara <rajitha@entgra.io>
certificatecolomn
Rajitha Kumara 1 year ago committed by Pahansith Gunathilake
parent 2dd3e86f69
commit 7c9d3a2108

@ -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");
} }

@ -1353,7 +1353,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
+ "FROM DM_DEVICE d, " + "FROM DM_DEVICE d, "
+ "(SELECT dgm.DEVICE_ID " + "(SELECT dgm.DEVICE_ID "
+ "FROM DM_DEVICE_GROUP_MAP dgm " + "FROM DM_DEVICE_GROUP_MAP dgm "
+ "WHERE dgm.GROUP_ID = (SELECT ID FROM DM_GROUP WHERE GROUP_NAME = ? )) dgm1 " + "WHERE dgm.GROUP_ID = (SELECT ID FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?)) dgm1 "
+ "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t "
+ "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 "
+ "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
@ -1362,6 +1362,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(1, groupName); stmt.setString(1, groupName);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {

@ -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;
} }

@ -570,6 +570,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String parentPath; String parentPath;
List<DeviceGroup> childrenGroups; List<DeviceGroup> childrenGroups;
if (StringUtils.isBlank(username)) { if (StringUtils.isBlank(username)) {
try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
rootGroups = groupDAO.getGroups(request, tenantId); rootGroups = groupDAO.getGroups(request, tenantId);
for (DeviceGroup rootGroup : rootGroups) { for (DeviceGroup rootGroup : rootGroups) {
@ -581,9 +582,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
populateGroupProperties(rootGroup, tenantId); populateGroupProperties(rootGroup, tenantId);
} }
} }
} 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();
}
} else { } else {
List<Integer> allDeviceGroupIdsOfUser = getGroupIds(username); List<Integer> allDeviceGroupIdsOfUser = getGroupIds(username);
GroupManagementDAOFactory.openConnection();
rootGroups = this.getGroups(allDeviceGroupIdsOfUser, tenantId); rootGroups = this.getGroups(allDeviceGroupIdsOfUser, tenantId);
if (requireGroupProps) { if (requireGroupProps) {
for (DeviceGroup rootGroup : rootGroups) { for (DeviceGroup rootGroup : rootGroups) {
@ -591,19 +599,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
} }
} }
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups with hierarchy"; String msg = "Error occurred while retrieving all groups with hierarchy";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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();
} }
PaginationResult groupResult = new PaginationResult(); PaginationResult groupResult = new PaginationResult();
groupResult.setData(rootGroups); groupResult.setData(rootGroups);
if (StringUtils.isBlank(username)) { if (StringUtils.isBlank(username)) {
@ -616,6 +617,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
private List<DeviceGroup> getGroups(List<Integer> groupIds, int tenantId) throws GroupManagementException { private List<DeviceGroup> getGroups(List<Integer> groupIds, int tenantId) throws GroupManagementException {
try { try {
GroupManagementDAOFactory.openConnection();
List<DeviceGroup >groups = groupDAO.getGroups(groupIds, tenantId); List<DeviceGroup >groups = groupDAO.getGroups(groupIds, tenantId);
if (groups == null) { if (groups == null) {
String msg = "Retrieved null when getting groups for group ids " + groupIds.toString(); String msg = "Retrieved null when getting groups for group ids " + groupIds.toString();
@ -625,10 +627,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (groups.isEmpty()) return groups; if (groups.isEmpty()) return groups;
groups.sort(Comparator.comparing(DeviceGroup::getGroupId)); groups.sort(Comparator.comparing(DeviceGroup::getGroupId));
return getTree(groups); return getTree(groups);
} 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);
} catch (GroupManagementDAOException ex) { } catch (GroupManagementDAOException ex) {
String msg = "Error occurred while getting groups for group ids " + groupIds.toString(); String msg = "Error occurred while getting groups for group ids " + groupIds.toString();
log.error(msg, ex); log.error(msg, ex);
throw new GroupManagementException(msg, ex); throw new GroupManagementException(msg, ex);
} finally {
GroupManagementDAOFactory.closeConnection();
} }
} }
@ -636,8 +645,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 +666,76 @@ 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) {
DeviceGroup tempNode = findGroupFromTree(node.getChildrenGroups(), groupId);
if (tempNode != null) {
return tempNode;
}
}
}
return null;
}
private boolean isAdminUser(String username, UserStoreManager userStoreManager)
throws GroupManagementException {
try {
if (!userStoreManager.isExistingUser(username)) {
String msg = "User doesn't exists with given username " + username;
throw new GroupManagementException(msg);
}
String []currentRoles = userStoreManager.getRoleListOfUser(username);
for (String role : currentRoles) {
if (role.equals("admin")) return true;
}
return false;
} catch (UserStoreException e) {
String msg = "Error occurred while requesting user details";
log.error(msg, e);
throw new GroupManagementException(msg, e);
}
}
@Override
public DeviceGroup getUserOwnGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String username = ctx.getUsername();
int tenantId = ctx.getTenantId();
try {
UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance().
getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
if (isAdminUser(username, userStoreManager)) {
return getGroup(groupId, requireGroupProps);
}
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);
}
DeviceGroup deviceGroup = findGroupFromTree(
getGroups(userOwnGroupIds, tenantId), groupId);
if (deviceGroup != null && requireGroupProps)
populateGroupProperties(deviceGroup, tenantId);
return deviceGroup;
} catch (UserStoreException e) {
String msg = "Error occurred while getting user store manager service";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while obtaining group '" + groupId + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
}
}
@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