UserWebAPI userWebAPI = WebAPILocator.getUserWebAPI();
WebContext ctx = WebContextFactory.get();
HttpServletRequest request = ctx.getHttpServletRequest();
UserAPI userAPI = APILocator.getUserAPI();
HostAPI hostAPI = APILocator.getHostAPI();
FolderAPI folderAPI = APILocator.getFolderAPI();
//Retrieving the current user
User user = userWebAPI.getLoggedInUser(request);
User systemUser = userAPI.getSystemUser();
boolean respectFrontendRoles = !userWebAPI.isLoggedToBackend(request);
Set<Object> permAssets = new HashSet<Object>();
HashMap<String, List<Permission>> permByInode = new HashMap<String, List<Permission>>();
RoleAPI roleAPI = APILocator.getRoleAPI();
PermissionAPI permAPI = APILocator.getPermissionAPI();
Host systemHost = hostAPI.findSystemHost(systemUser, false);
Role role = roleAPI.loadRoleById(roleId);
List<Permission> perms = permAPI.getPermissionsByRole(role, true, true);
for(Permission p : perms) {
List<Permission> permList = permByInode.get(p.getInode());
if(permList == null) {
permList = new ArrayList<Permission>();
permByInode.put(p.getInode(), permList);
}
permList.add(p);
Identifier ident = APILocator.getIdentifierAPI().findFromInode(p.getInode());
if(ident.getAssetType().equals("folder")) {
Folder f = APILocator.getFolderAPI().find(p.getInode(), systemUser, respectFrontendRoles);
permAssets.add(f);
} else {
Host h = hostAPI.find(p.getInode(), systemUser, respectFrontendRoles);
if(h != null) {
permAssets.add(h);
}
}
}
List<Map<String, Object>> hostMaps = new ArrayList<Map<String,Object>>();
List<Map<String, Object>> folderMaps = new ArrayList<Map<String,Object>>();
boolean systemHostInList = false;
for(Object i : permAssets) {
if(i instanceof Host && ((Host)i).isSystemHost())
systemHostInList = true;
Map<String, Object> assetMap = i instanceof Host?((Host)i).getMap():((Inode)i).getMap();
String assetId = i instanceof Host?((Host)i).getIdentifier():((Inode)i).getInode();
List<Map<String, Object>> permissionsList = new ArrayList<Map<String,Object>>();
for(Permission p: permByInode.get(assetId)) {
permissionsList.add(p.getMap());
}
assetMap.put("permissions", permissionsList);
if(i instanceof Host) {
assetMap.put("type", "host");
hostMaps.add(assetMap);
} else {
Folder f = (Folder) i;
Identifier id = APILocator.getIdentifierAPI().find(f);
String hostId = f.getHostId();
Host h = hostAPI.find(hostId, systemUser, false);
assetMap.put("fullPath", h.getHostname() + ":" + id.getParentPath() + f.getName());
folderMaps.add(assetMap);
}
boolean permissionToEditPermissions = permAPI.doesUserHavePermission((Permissionable)i, PermissionAPI.PERMISSION_EDIT_PERMISSIONS, user, respectFrontendRoles);
assetMap.put("permissionToEditPermissions", permissionToEditPermissions);