Continuing the discussion from Permissions: Add but not edit:
I’m currently looking for a mechanism that would allow to set dynamic rights to newly created pages.
More specificially, whenever a page is created whose name follows a pattern (_Homepage):
The Groupname should be extracted from the pagenameAn XWikiRights-Object should be created for the page which allows the extracted Group access to the document
As @tmortagne pointed out in the linked post, this can be done via an Event Listener.
I’ve followed http://extensions.xwiki.org/xwiki/bin/view/Extension/WikiComponent%20Module to create a Listener as a Component.
I also added the following groovy-code to the onEvent method:
{{groovy}}
import org.xwiki.context.*
import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.bridge.event.*
import org.xwiki.model.reference.*
import org.xwiki.model.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*
try {
System.out.println("\n[BEGIN]\nThe document ${xcontext.method.input.get(1)} has been created.")
def crtContext = Utils.getComponent(Execution.class).getContext().getProperty("xwikicontext")
def docSource = xcontext.method.input.get(1)
def oldDoc = docSource.getOriginalDocument()
def existingObj = oldDoc.getObject("XWiki.XWikiRights")
if (existingObj == null) {
System.out.println("XWikiRights object does not exist, creating.")
def rightsObj = oldDoc.getObject("XWiki.XWikiRights", true, crtContext)
// try to read props from obj
def allow = rightsObj.get("allow")
def users = rightsObj.get("users")
def levels = rightsObj.get("levels")
System.out.println("Setting value of 'allow'.")
rightsObj.setIntValue("allow", 1)
System.out.println("Setting value of 'levels'.")
rightsObj.setStringValue("levels", "view,edit")
System.out.println("Setting value of 'users'.")
rightsObj.setLargeStringValue("users", "XWiki.DigitalData")
System.out.print("\nallow:\t")
System.out.println(rightsObj.get("allow").value)
System.out.print("levels:\t")
System.out.println(rightsObj.get("levels").value)
System.out.print("users:\t")
System.out.println(rightsObj.get("users").value)
oldDoc.setObject("XWiki.XWikiRights", 0, rightsObj)
System.out.println("\nXWikiRights object added.\n[END]")
} else {
System.out.println("XWikiRights object exists.")
System.out.println(existingObj)
}
} catch(e) {
System.out.println("ERROR!")
System.out.println(e.getMessage())
}
{{/groovy}}
EDIT
I added the code I’ve written so far.
I can check for an existing XWikiRights object and also create a new one. Setting the property works fine too.
What does not work is actually saving the object back to the page.
- Do I have to call the setObject on the document?
- Am I using the method correctly?
- Is there something else wrong with my code that prevents creating the XWikiRights object?
Things I’ve been reading:
- https://stackoverflow.com/questions/34066573/set-user-and-group-rights-to-document-in-xwiki
- https://forum.xwiki.org/t/need-help-with-setting-the-value-of-an-object-property-thru-a-documentupdatingevent/2659/3?u=inhji
- https://forum.xwiki.org/t/help-customising-diagram-diagramsheet/410/4?u=inhji
Thanks in advance!