use of org.pentaho.platform.api.repository2.unified.IRepositoryAccessVoterManager in project pentaho-platform by pentaho.
the class JcrRepositoryFileUtilsTest method testCheckNodeForTree.
@Test
public void testCheckNodeForTree() throws Exception {
List<RepositoryFileTree> children = new ArrayList<>();
IPathConversionHelper pathConversionHelper = new DefaultPathConversionHelper();
ILockHelper lockHelperMock = mock(ILockHelper.class);
IRepositoryAccessVoterManager repositoryAccessVoterManagerMock = mock(IRepositoryAccessVoterManager.class);
MutableBoolean foundFiltered = new MutableBoolean();
RepositoryFile fileMock = mock(RepositoryFile.class);
when(fileMock.getId()).thenReturn(1);
PowerMockito.mockStatic(JcrRepositoryFileUtils.class);
PowerMockito.mockStatic(JcrRepositoryFileAclUtils.class);
PowerMockito.doCallRealMethod().when(JcrRepositoryFileUtils.class, "checkNodeForTree", nodeMock, children, sessionMock, pJcrConstMock, pathConversionHelper, "childNodeFilter", lockHelperMock, 0, false, repositoryAccessVoterManagerMock, RepositoryRequest.FILES_TYPE_FILTER.FOLDERS, foundFiltered, true, false, "/");
when(JcrRepositoryFileUtils.nodeToFile(sessionMock, pJcrConstMock, pathConversionHelper, lockHelperMock, nodeMock)).thenReturn(fileMock);
when(JcrRepositoryFileUtils.isSupportedNodeType(pJcrConstMock, nodeMock)).thenReturn(true);
when(JcrRepositoryFileAclUtils.getAcl(sessionMock, pJcrConstMock, 1)).thenThrow(new AccessDeniedException());
try {
JcrRepositoryFileUtils.checkNodeForTree(nodeMock, children, sessionMock, pJcrConstMock, pathConversionHelper, "childNodeFilter", lockHelperMock, 0, false, repositoryAccessVoterManagerMock, RepositoryRequest.FILES_TYPE_FILTER.FOLDERS, foundFiltered, true, false, "/");
} catch (Exception e) {
fail();
}
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryAccessVoterManager in project pentaho-platform by pentaho.
the class JcrRepositoryFileUtils method checkNodeForTree.
/**
* This method is called twice by <code>getTreeNode</code>. It's job is to determine whether the current child node
* should be added to the list of children for the node being processed. It is a separate method simply because it is
* too much code to appear twice in the above <code>getTreeNode</code> method. It also makes the recursive call back
* to getTreeByNode to process the next lower level of folder node (it must process the lower levels to know if the
* folder should be added). Finally, it returns the foundFiltered boolean to let the caller know if a file was found
* that satisfied the childNodeFilter.
*/
static void checkNodeForTree(final Node childNode, List<RepositoryFileTree> children, final Session session, final PentahoJcrConstants pentahoJcrConstants, final IPathConversionHelper pathConversionHelper, final String childNodeFilter, final ILockHelper lockHelper, final int depth, final boolean showHidden, final IRepositoryAccessVoterManager accessVoterManager, RepositoryRequest.FILES_TYPE_FILTER types, MutableBoolean foundFiltered, boolean isRootFiltered, final boolean includeSystemFolders, final String rootPath) throws RepositoryException {
RepositoryFile file = nodeToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, childNode);
if (isSupportedNodeType(pentahoJcrConstants, childNode)) {
RepositoryFileAcl fileAcl;
try {
fileAcl = JcrRepositoryFileAclUtils.getAcl(session, pentahoJcrConstants, file.getId());
} catch (AccessDeniedException e) {
return;
}
if (accessVoterManager.hasAccess(file, RepositoryFilePermission.READ, fileAcl, PentahoSessionHolder.getSession())) {
MutableBoolean foundFilteredAtomic = new MutableBoolean(!isPentahoFolder(pentahoJcrConstants, childNode));
RepositoryFileTree repositoryFileTree = getTreeByNode(session, pentahoJcrConstants, pathConversionHelper, lockHelper, childNode, depth - 1, childNodeFilter, showHidden, accessVoterManager, types, foundFilteredAtomic, includeSystemFolders, rootPath);
if (repositoryFileTree != null && (foundFilteredAtomic.booleanValue() || isRootFiltered)) {
foundFiltered.setValue(true);
children.add(repositoryFileTree);
}
}
}
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryAccessVoterManager in project pentaho-platform by pentaho.
the class JcrRepositoryFileUtils method getTreeByNode.
/**
* Returns a RepositoryFileTree for a given node. This method will be called recursively for each folder it processes.
* The childNodeFilter is a filter used directly by the JCR jar to filter node names. Since JCR does not know a folder
* from a file (that is our construct), it is not capable of filtering out filenames but not folder names. Therefore,
* this logic will create two sets of children nodes. The <code>filteredChildrenSet</code> keeps the child nodes that
* satisfied the childNodeFilter. The <code> childrenFolderSet</code> keeps a set of all child folder nodes regardless
* of the value of the filter. We use the <code>childrenFolderSet</code> to know what folders to traverse, but we use
* the <code>filteredChildrenSet </code> to determine what actual files to include in the returned tree.
* <p>
* Just because we process a folder node does not necessarily mean the folder will be reported in the tree. It must
* first find a file that satisfies the criteria of the <code>childNodeFilter</code> mask. A file meeting the criteria
* may be any number of folders down the repository structure, so the <code>foundFiltered</code> MutableBoolean tells
* the caller if a file was found, at any level, meeting that criteria.
*
* @param session
* The current session in progress
* @param pentahoJcrConstants
* @param pathConversionHelper
* @param lockHelper
* @param fileNode
* The node which will serve as the root of the tree
* @param depth
* how many levels do we go down.
* @param childNodeFilter
* The filter sent to JCR to retrieve defining which files are in scope
* @param showHidden
* Whether to return hidden files
* @param accessVoterManager
* See IRepositoryAccessVoterManager
* @param types
* <code>FILE_TYPE_FILTERS</code> Types of files to return including FILES, FOLDERS, FILES_FOLDERS
* @param foundFiltered
* This <code>MutableBoolean</code> will tell the caller if there was a file encountered, (at any level up to
* the depth), that was compliant with the childNodeFilter. This will determine if this node, (and its
* children), should be discarded because there are no relevant files.
* @return A RepositoryFileTree representing the entire tree at and below the given node that complies with file
* filtering and other parameters of the tree request.
* @throws RepositoryException
*/
private static RepositoryFileTree getTreeByNode(final Session session, final PentahoJcrConstants pentahoJcrConstants, final IPathConversionHelper pathConversionHelper, final ILockHelper lockHelper, final Node fileNode, final int depth, final String childNodeFilter, final boolean showHidden, IRepositoryAccessVoterManager accessVoterManager, RepositoryRequest.FILES_TYPE_FILTER types, MutableBoolean foundFiltered, final boolean includeSystemFolders, final String rootPath) throws RepositoryException {
RepositoryFile rootFile = nodeToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode, false, null);
if ((!showHidden && rootFile.isHidden()) || rootFile.isAclNode() || (!accessVoterManager.hasAccess(rootFile, RepositoryFilePermission.READ, JcrRepositoryFileAclUtils.getAcl(session, pentahoJcrConstants, rootFile.getId()), PentahoSessionHolder.getSession()))) {
return null;
}
List<RepositoryFileTree> children;
HashSet<Node> childrenFolderSet;
// to go)
if (depth != 0) {
children = new ArrayList<RepositoryFileTree>();
int numberOfPasses = childNodeFilter != null && !childNodeFilter.equals("*") ? 2 : 1;
// get Filtered Children set
HashSet<Node> filteredChildrenSet;
filteredChildrenSet = new HashSet<Node>();
NodeIterator childNodes = fileNode.getNodes(childNodeFilter);
while (childNodes.hasNext()) {
Node childNode = childNodes.nextNode();
boolean pentahoFolder = isPentahoFolder(pentahoJcrConstants, childNode);
if (!(!pentahoFolder && types == RepositoryRequest.FILES_TYPE_FILTER.FOLDERS || pentahoFolder && types == RepositoryRequest.FILES_TYPE_FILTER.FILES)) {
// do not to include (skip) system_folder children that are at root level if includeSystemFolders is false
if (!(!includeSystemFolders && (rootPath.equals(childNode.getParent().getPath()) && isSystemFolder(session, childNode)))) {
filteredChildrenSet.add(childNode);
}
}
}
// Now get the unfiltered folder set not already in Filtered Set
childrenFolderSet = new HashSet<Node>();
if (numberOfPasses == 2) {
if (isPentahoFolder(pentahoJcrConstants, fileNode)) {
childNodes = fileNode.getNodes();
while (childNodes.hasNext()) {
Node childNode = childNodes.nextNode();
boolean pentahoFolder = isPentahoFolder(pentahoJcrConstants, childNode);
if (pentahoFolder) {
childrenFolderSet.add(childNode);
}
}
}
}
// tree
for (Node childNode : childrenFolderSet) {
checkNodeForTree(childNode, children, session, pentahoJcrConstants, pathConversionHelper, childNodeFilter, lockHelper, depth, showHidden, accessVoterManager, types, foundFiltered, false, includeSystemFolders, rootPath);
}
// And finally, add Children in filtered
for (Node childNode : filteredChildrenSet) {
foundFiltered.setValue(true);
checkNodeForTree(childNode, children, session, pentahoJcrConstants, pathConversionHelper, childNodeFilter, lockHelper, depth, showHidden, accessVoterManager, types, foundFiltered, true, includeSystemFolders, rootPath);
}
children.removeIf(Objects::isNull);
Collections.sort(children);
} else {
children = null;
}
return new RepositoryFileTree(rootFile, children);
}
Aggregations