Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

AccessControlManager throwns PathNotFoundException

Avatar

Level 3

Hi,

I want to generate a SiteMap related with the user's privileges. Then, I have thought to check the privileges of each page (they will change in order to the user logged) and performance an action depending the privilege. I don't know why my code is throwing AccessControlManager throws a PathNotFoundException. Any idea?

To clarify: the first time that I call the AccessControlManager, it works as expected. The second time (inside buildSiteMap function), it throws the exception.

 

Here is my JSP:

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<sling:defineObjects/>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@page import="java.lang.Math, 
                java.lang.Integer, 
                java.util.Iterator,
                com.day.text.Text, 
                com.day.cq.wcm.api.PageFilter, 
                com.day.cq.wcm.api.Page,
                com.day.cq.wcm.api.WCMMode,
                com.day.cq.security.User,
                javax.jcr.security.AccessControlManager,
                org.apache.jackrabbit.api.security.JackrabbitAccessControlManager,
                javax.jcr.security.Privilege,
                org.apache.sling.api.SlingHttpServletRequest,
                org.apache.sling.api.resource.ResourceResolver" %>

<cq:includeClientLib categories="myclientlibs.all" />

<% if (WCMMode.fromRequest(request) == WCMMode.EDIT) {%>
    <c:if test="${empty properties.rootPath}">
    Please, Insert a root path.
    </c:if>
<%}%>

<c:if test="${not empty properties.rootPath}">

<%
    String siteMapLevelsString = (String) properties.get("maxLevels");  
    if(null == siteMapLevelsString){
        siteMapLevelsString = "3";
    }
    int siteMaplevels = Integer.parseInt(siteMapLevelsString)-1;
    String rootPathSiteMap = properties.get("rootPath").toString();
    Page rootPage = slingRequest.getResourceResolver().adaptTo(PageManager.class).getPage(rootPathSiteMap);

    final String userID = resourceResolver.getUserID();
    Session session = resourceResolver.adaptTo(Session.class);
    AccessControlManager aCLManager = session.getAccessControlManager();
    //JackrabbitAccessControlManager aCLManager = (JackrabbitAccessControlManager) session.getAccessControlManager();

    Privilege [] privileges = aCLManager.getPrivileges(rootPathSiteMap);  //         IT DOESN'T THROW EXCEPTION.

    String siteMap = "";
    siteMap = siteMap + "<ul id='slickmapNav'><li id='home'><a href='"+rootPage.getPath()+".html'>"+rootPage.getTitle()+"</a></li>";
    siteMap = siteMap + buildSiteMap(slingRequest, rootPage, siteMaplevels, 0, aCLManager) +"</ul>";
%>
    <div class="module siteMapGraphic">
        <%=siteMap%>
    </div>
<%!
        public String buildSiteMap(SlingHttpServletRequest slingRequest, Page rootPage, int siteMapLevels, int currentLevel, AccessControlManager aCLManager){
        String siteMap = "";
        if (rootPage != null) {

            Iterator<Page> children = rootPage.listChildren(new PageFilter(slingRequest));
            boolean isNewUl = children.hasNext();

            while (children.hasNext()) { 
                Page child = children.next(); 
                String childRootPath = child.getPath();
                Privilege [] childPrivileges = aCLManager.getPrivileges(childRootPath);   // THIS IS THE LINE THAT THROWS THE EXCEPTION
                siteMap = siteMap+"<li><a href='"+childRootPath+".html'>"+child.getTitle()+"</a>";

                if(siteMapLevels>currentLevel){

                    // TO-DO some logic depending on the privileges of the page according the user logged
                    siteMap = siteMap + buildSiteMap(slingRequest, child, siteMapLevels, currentLevel+1, aCLManager);
                 }
                siteMap = siteMap+"</li>";
            } 
            if(isNewUl && currentLevel!=0){
                siteMap = "<ul>"+ siteMap + "</ul>"; //"</li></ul>";
            }
        }
        return siteMap;
    }
%>
</c:if>

1 Accepted Solution

Avatar

Correct answer by
Level 10

The docs say this:

PathNotFoundException - if no node at absPath exists or the session does not have sufficient access to retrieve a node at that location.

Does the session have permission to access the child node? 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

The docs say this:

PathNotFoundException - if no node at absPath exists or the session does not have sufficient access to retrieve a node at that location.

Does the session have permission to access the child node? 

Avatar

Level 3

I have checked the session's permission and I reckon this is the issue. Before retrieving the privileges of the child pages (inside buildSiteMap function), I have added these lines:

        public String buildSiteMap(SlingHttpServletRequest slingRequest, Page rootPage, int siteMapLevels, int currentLevel, Session session){
        String siteMap = "";
        if (rootPage != null) {

            Iterator<Page> children = rootPage.listChildren(new PageFilter(slingRequest));
            boolean isNewUl = children.hasNext();

            while (children.hasNext()) { 
                Page child = children.next(); 
                String childRootPath = child.getPath();
                String read = session.ACTION_READ;
                boolean permission = session.hasPermission(childRootPath,read);

Now, checking the logs, I am getting "Unhandled exception type RepositoryException" in the last line. 

Is it related?