Checking rights for the current user
There is an API for check checking rights of the current user for a given page. There is no such thing of checking rights for a complete space (which could be open to various interpretations, like "the user has access to at least one page", or "the user has an "ok" checkbox in the "rights" view for the root page of the space").
Usually one checks for the access rights of the "home page" of that space instead (the one with name WebHome
), so the link to that space does not lead to a "forbidden" page for that user.
Also there is no query filter or the like, instead one has to check each page separately.
The access check is in the checkAccess(String action, XWikiDocument doc, XWikiContext context)
method of the XWiki
object; a check for view rights on the home page of a space would look like:
String spaceName = "...."
XWikiDocument spaceHomePage = xwiki.getDocument(spaceName +".WebHome", xcontext)
if (xwiki.checkAccess("view", spaceHomePage, xcontext) ) {
// space home viewable
} else {
// space home not viewable
}
The "current user" is stored in the xcontext
object, no need to pass this user object in explicitely anywhere. (Just in case someone wonders.)
Checking rights for other users
(This has not been asked, but as I just looked this up ...)
If you want to know the access rights for someone else but the user in the current context, there is the
hasAccessLevel(String level, String user, String docname)
on the rights service of the xwiki object, which wants a full reference to the user profile page in user
, so code usually looks like:
XWikiUser user = xwiki.getUser(userLoginHere, xcontext).getUser();
if (xwiki.getRightService().hasAccessLevel("view", user.getUser(), spaceRef +".WebHome", xcontext) ) {
// has view rights ...
}
Alternatively, if you are in a java component yourself, you can let the component manager @Inject
a org.xwiki.security.authorization.AuthorizationManager
and use an hasAccess
method in that class, which accepts typed objects instead of plain strings. (It wants a DocumentReference
, not an User
object, so feed an user.getUserReference()
to it.)