Search code examples
velocityxwiki

xwiki - Get title of Page if rights are Read=False


I'm trying to create an automatic Index where all the pages from the spaces will be displayed. Something like:

  • Space 1 (public)
    • Page 1 (public)
    • Page 2 (private)
      • Page 2.1 (public)
  • Space 2 (public)
    • Page A (private)
    • Page B (public)

Where:

  • public: (Rights:Read=True) user can click to access the content.
  • private: (Rights:Read=False) user can see the title but can't click.

If the user has no rights to read the page I want that at least he can get to see the title of the page as I can continue listing the pages that are able to read in the child.

What I have:

#foreach($space in $xwiki.spaces) 
    #set($WebHome = $space + ".WebHome")
    #getChildrenOf($WebHome)
#end

Now the recursive getChildrenOf macro is:

#macro(getChildrenOf, $docName)
    #foreach($name in $xwiki.searchDocuments('where doc.parent = ? or doc.parent = ? order by doc.name', [$docName, "xwiki:$docName"]))
         #if($xwiki.hasAccessLevel('view', "xwiki:$name"))
             [[$xwiki.getDocument($name).getPlainTitle()>>$name]]
         #else
             $xwiki.getDocument($name).getPlainTitle()
         #end
         #getChildrenOf($name)
    #end
#end

Results:

  • In the elsei'm getting a plain text showing the function $xwiki.getDocument($name).getPlainTitle() instead of the title of the Page.

  • If I just write $name in the else what I get is: Space_1.Page_2 instead of the title of Page 2.

Why is that happening? How can I solve it?


Solution

  • $xwiki.getDocument($name) return null when the current user does not have view right on $name and when a method return null Velocity behavior is to display the code.

    You can use $xwiki.getDocumentAsAuthor($name) instead to access the document with the right of the script's author. See http://nexus.xwiki.org/nexus/service/local/repositories/releases/archive/org/xwiki/platform/xwiki-platform-oldcore/6.2/xwiki-platform-oldcore-6.2-javadoc.jar/!/com/xpn/xwiki/api/XWiki.html#getDocumentAsAuthor%28org.xwiki.model.reference.DocumentReference%29