Setting Rights
Last modified by slauriere on 2021/03/18 11:28
![]() | Shows how to programmatically set rights |
Type | Snippet |
Category | |
Developed by | |
Rating | |
Website | a |
License | GNU Lesser General Public License 2.1 |
Table of contents
Description
{{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}}
#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}}
## 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}}