cogShows how to programmatically set rights
TypeSnippet
Category
Developed by

Vincent Massol, Pascal Bastien

Rating
0 Votes
Websitea
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Make sure to explicitely set the 'allow' field below as otherwise it won't work. This seems to be some bug since by default the allow field is set to 1.

If you set rights programmatically with the priviledged API via a BaseObject, make sure that you update the "groups" and "users" properties using "setLargeStringValue" (and not "setStringValue"), otherwise, it won't work as expected.

{{velocity}}
#set ($mydoc = $xwiki.getDocument("Space.Page"))

## Create a rights object and attach it to the Space.Page page
## Note that this method (getObject) will reuse an existing rights object, if one exists;
##  if you want to always create a new object, use $mydoc.newObject("XWiki.XWikiRights")
## Note that XWiki.XWikiRights is used for page rights; if you want to change global
##  (wiki or space) rights, use XWiki.XWikiGlobalRights
#set ($rightsObject = $mydoc.getObject("XWiki.XWikiRights", true))

## Set the values of the fields
#set ($result = $rightsObject.set("groups", "XWiki.XWikiAdminGroup"))
#set ($result = $rightsObject.set("levels", "edit"))
#set ($result = $rightsObject.set("users", ""))
#set ($result = $rightsObject.set("allow", 1))

## Save the modifications
$mydoc.save()

{{/velocity}}

The Space.Page page can now only be edited by members of the XWikiAdminGroup group.

If Space.Page have multiple rights object like (different group with different rights), it is possible to erase all rights before to add them again.
To set only some rights without erasing old oneyou must loop over rights object and use "$doc.use" on it (API guide) before to set them.

{{velocity}}

##  Space.Page already have multiple rights object (different group with different rights)
##  Erase all rights before to add them again
##  
#set ($mydoc = $xwiki.getDocument("Space.Page"))

##  Delete all right objects on MyDoc
#set($discard =$mydoc.removeObjects("XWiki.XWikiRights"))

##  Add some rights object and    
## Set the values of the fields
#set ($RightsObject = $myDoc.newObject("XWiki.XWikiRights"))
#set ($result = $RightsObject.set("groups", "XWiki.XWikiAdminGroup"))
#set ($result = $RightsObject.set("levels", "view,comment,edit,delete"))
#set ($result = $RightsObject.set("users", ""))
#set ($result = $RightsObject.set("allow", 1))

#set ($RightsObject = $mydoc.newObject("XWiki.XWikiRights"))
#set ($result = $RightsObject.set("groups", "XWiki.XWikiAllGroup"))
#set ($result = $RightsObject.set("levels", "view,comment"))
#set ($result = $RightsObject.set("users", ""))
#set ($result = $RightsObject.set("allow", 1))
## to deny: $RightsObject.set("allow", 0)

##  Don't forget to save the modifications
$mydoc.save()

{{/velocity}}
Tags: development
     

Get Connected