diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index fd85662597..c63810fb1f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -570,20 +570,28 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String parentPath; List 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 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 getGroups(List groupIds, int tenantId) throws GroupManagementException { try { + GroupManagementDAOFactory.openConnection(); Listgroups = 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(); } } @@ -670,35 +679,60 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid 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(); - List 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(); + UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); + if (isAdminUser(username, userStoreManager)) { + return getGroup(groupId, requireGroupProps); + } + + List 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 (GroupManagementDAOException e) { - String msg = "Error occurred while obtaining group '" + groupId + "'"; + } catch (UserStoreException e) { + String msg = "Error occurred while getting user store manager service"; 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"; + } catch (GroupManagementDAOException e) { + String msg = "Error occurred while obtaining group '" + groupId + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } }