@SuppressWarnings("unchecked")
private void addPermissionToRoleList(Permissionable perm, Permission p, Map<String, Map<String, Object>> roles, boolean inheritable) throws DotDataException, DotSecurityException {
Map<String, Permissionable> inodeCache = new HashMap<String, Permissionable>();
RoleAPI roleAPI = APILocator.getRoleAPI();
HostAPI hostAPI = APILocator.getHostAPI();
User systemUser = APILocator.getUserAPI().getSystemUser();
String roleId = p.getRoleId();
Map<String, Object> roleMap = roles.get(roleId);
if(roleMap == null) {
Role role = roleAPI.loadRoleById(roleId);
if(role == null)
return;
roleMap = role.toMap();
roles.put(role.getId(), roleMap);
if(!inheritable) {
if(p.getInode().equals(perm.getPermissionId())) {
roleMap.put("inherited", false);
} else {
roleMap.put("inherited", true);
String assetInode = p.getInode();
//try from the cache
Permissionable permParent = inodeCache.get(p.getInode());
if(permParent == null) {
// because identifiers are not Inodes, we need to do a double lookup
permParent = InodeFactory.getInode(assetInode, Inode.class);
if(permParent !=null || InodeUtils.isSet(permParent.getPermissionId())){
inodeCache.put(permParent.getPermissionId(), permParent);
}
else{
permParent = APILocator.getIdentifierAPI().find(assetInode);
if(permParent != null && InodeUtils.isSet(permParent.getPermissionId())){
inodeCache.put(permParent.getPermissionId(), permParent);
}
}
}
if(permParent instanceof Folder) {
roleMap.put("inheritedFromType", "folder");
roleMap.put("inheritedFromPath", APILocator.getIdentifierAPI().find((Folder)permParent).getPath());
roleMap.put("inheritedFromId", ((Folder)permParent).getInode());
} else if (permParent instanceof Structure) {
roleMap.put("inheritedFromType", "structure");
roleMap.put("inheritedFromPath", ((Structure)permParent).getName());
roleMap.put("inheritedFromId", ((Structure)permParent).getInode());
} else if (permParent instanceof Category) {
roleMap.put("inheritedFromType", "category");
roleMap.put("inheritedFromPath", ((Category)permParent).getCategoryName());
roleMap.put("inheritedFromId", ((Category)permParent).getInode());
} else {
Host host = hostAPI.find(assetInode, systemUser, false);
if(host != null) {
roleMap.put("inheritedFromType", "host");
roleMap.put("inheritedFromPath", host.getHostname());
roleMap.put("inheritedFromId", host.getIdentifier());
}