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>
issue-10462/secure-pending-operation-6.2
Rajitha Kumara 1 year ago committed by Pahansith Gunathilake
parent 2dd3e86f69
commit 7c9d3a2108

@ -533,7 +533,14 @@ public interface GroupManagementService {
defaultValue = "1")
@DefaultValue("1")
@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}")
@GET

@ -176,10 +176,11 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}
@Override
public Response getGroup(int groupId, boolean requireGroupProps, int depth) {
public Response getGroup(int groupId, boolean requireGroupProps, int depth, boolean allowed) {
try {
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) {
return Response.status(Response.Status.OK).entity(deviceGroup).build();
} else {

@ -176,13 +176,13 @@ public class GroupManagementServiceImplTest {
Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false, 1);
Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, 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(),
"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(),
"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(),
"getGroup request returned a group for a in-valid request");
}

@ -1353,7 +1353,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
+ "FROM DM_DEVICE d, "
+ "(SELECT dgm.DEVICE_ID "
+ "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 gd.DEVICE_TYPE_ID = t.ID) d1 "
+ "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.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
devices = new ArrayList<>();
while (rs.next()) {

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

@ -570,20 +570,28 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String parentPath;
List<DeviceGroup> childrenGroups;
if (StringUtils.isBlank(username)) {
GroupManagementDAOFactory.openConnection();
rootGroups = groupDAO.getGroups(request, tenantId);
for (DeviceGroup rootGroup : rootGroups) {
parentPath = DeviceManagerUtil.createParentPath(rootGroup);
childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId);
createGroupWithChildren(
rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0);
if (requireGroupProps) {
populateGroupProperties(rootGroup, tenantId);
try {
GroupManagementDAOFactory.openConnection();
rootGroups = groupDAO.getGroups(request, tenantId);
for (DeviceGroup rootGroup : rootGroups) {
parentPath = DeviceManagerUtil.createParentPath(rootGroup);
childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId);
createGroupWithChildren(
rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0);
if (requireGroupProps) {
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 {
List<Integer> allDeviceGroupIdsOfUser = getGroupIds(username);
GroupManagementDAOFactory.openConnection();
rootGroups = this.getGroups(allDeviceGroupIdsOfUser, tenantId);
if (requireGroupProps) {
for (DeviceGroup rootGroup : rootGroups) {
@ -591,19 +599,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
}
}
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups with hierarchy";
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();
}
PaginationResult groupResult = new PaginationResult();
groupResult.setData(rootGroups);
if (StringUtils.isBlank(username)) {
@ -616,6 +617,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
private List<DeviceGroup> getGroups(List<Integer> groupIds, int tenantId) throws GroupManagementException {
try {
GroupManagementDAOFactory.openConnection();
List<DeviceGroup >groups = groupDAO.getGroups(groupIds, tenantId);
if (groups == null) {
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;
groups.sort(Comparator.comparing(DeviceGroup::getGroupId));
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) {
String msg = "Error occurred while getting groups for group ids " + groupIds.toString();
log.error(msg, ex);
throw new GroupManagementException(msg, ex);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@ -636,8 +645,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
List<DeviceGroup> tree = new ArrayList<>();
for (DeviceGroup deviceGroup : groups) {
DeviceGroup treeNode = tree.stream().
filter(node -> deviceGroup.getParentPath().
contains(Integer.toString(node.getGroupId()))).
filter(node -> Arrays.stream(deviceGroup.getParentPath().split("/")).
collect(Collectors.toList()).contains(Integer.toString(node.getGroupId()))).
findFirst().orElse(null);
if (treeNode != null) {
if (Objects.equals(treeNode.getParentPath(), deviceGroup.getParentPath())) {
@ -657,6 +666,76 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
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
public List<DeviceGroup> getGroups(String username, boolean requireGroupProps) throws GroupManagementException {
if (username == null || username.isEmpty()) {

Loading…
Cancel
Save