Fix group retrieving issue in sub tenants

grouping-feature
Rajitha Kumara 1 year ago
parent 2b8e4b7c63
commit 72a6831350

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

Loading…
Cancel
Save