Fix group retrieving issue in sub tenants

pull/186/head
Rajitha Kumara 1 year ago
parent 2b8e4b7c63
commit 72a6831350

@ -570,20 +570,28 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String parentPath; String parentPath;
List<DeviceGroup> childrenGroups; List<DeviceGroup> childrenGroups;
if (StringUtils.isBlank(username)) { if (StringUtils.isBlank(username)) {
GroupManagementDAOFactory.openConnection(); try {
rootGroups = groupDAO.getGroups(request, tenantId); GroupManagementDAOFactory.openConnection();
for (DeviceGroup rootGroup : rootGroups) { rootGroups = groupDAO.getGroups(request, tenantId);
parentPath = DeviceManagerUtil.createParentPath(rootGroup); for (DeviceGroup rootGroup : rootGroups) {
childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); parentPath = DeviceManagerUtil.createParentPath(rootGroup);
createGroupWithChildren( childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId);
rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0); createGroupWithChildren(
if (requireGroupProps) { rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0);
populateGroupProperties(rootGroup, tenantId); 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 { } 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();
} }
} }
@ -670,35 +679,60 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return null; 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 @Override
public DeviceGroup getUserOwnGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException { public DeviceGroup getUserOwnGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String username = ctx.getUsername(); String username = ctx.getUsername();
int tenantId = ctx.getTenantId(); 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 { try {
GroupManagementDAOFactory.openConnection(); 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( DeviceGroup deviceGroup = findGroupFromTree(
getGroups(userOwnGroupIds, tenantId), groupId); getGroups(userOwnGroupIds, tenantId), groupId);
if (deviceGroup != null && requireGroupProps) if (deviceGroup != null && requireGroupProps)
populateGroupProperties(deviceGroup, tenantId); populateGroupProperties(deviceGroup, tenantId);
return deviceGroup; return deviceGroup;
} catch (GroupManagementDAOException e) { } catch (UserStoreException e) {
String msg = "Error occurred while obtaining group '" + groupId + "'"; String msg = "Error occurred while getting user store manager service";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(msg, e); throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (GroupManagementDAOException e) {
String msg = "Error occurred while opening a connection to the data source to retrieve all groups " String msg = "Error occurred while obtaining group '" + groupId + "'";
+ "with hierarchy";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(msg, e); throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
} }
} }

Loading…
Cancel
Save