Programmatically creating XWikiGlobalRights from async jobs

Hello,

I am trying to to create a job triggered by certain document created events that would create a number of groups and set access right on those groups for the document and its children.

I was basing the logic on some publicly available script snippets, but I am doing it in Java.

I am doing something like the following:

XWikiDocument webPreferences = xcontext.getWiki().getDocument(webPreferencesReference, xcontext);
webPreferences.setAuthorReference(xcontext.getUserReference());
webPreferences.setCreatorReference(xcontext.getAuthorReference());

BaseClass rightsClass = xcontext.getWiki().getGlobalRightsClass(xcontext);
BaseObject rightsObject = webPreferences.newXObject(rightsClass.getReference(), xcontext);
String adminRights = "admin,script";
rightsObject.setStringValue("levels", adminRights);
rightsObject.setStringValue("groups", newGroupDocRef.toString().split(":")[1]);
rightsObject.setStringValue("users", "");
rightsObject.setIntValue("allow", 1);
xcontext.getWiki().saveDocument(webPreferences, "Setting rights", xcontext);

In the above code newGroupDocRef is a reference to the group document I crated and saved.

When I check the created WebPreferences document with the GUI using the objects editor I have the following warning:

If I then edit the object using the GUI editor and save, the deprecation warnings are gone. If I export the page as a XAR before and after the edit I see no significant change in the WebPreferences xml.

When I go to the root page (for which I am trying to set the access rights) and go to Administer page > Rights: Page & Children I can see the group in fact has the correct access right despite the deprecation warnings.

What can I do to fix the deprecation warning?
Also, once I click Administer page my WebPreferences page is replaced by a Page Administration page, why is that? Should I rather create a Page Administration page right away in my job instead of a simple WebPreferences?

Thank you for your help.

Kind regads,
Akos

This is because you are inserting in the object a property which does not have the same type than what is indicated in the class: setStringValue explicitly set a StringProperty but objects and groups type is actually LargeStringProperty. The snippet is using set() method which convert the passed String value into the right type automatically, this method exist in Java too (you just need to also pass the XWikiContext).

It’s not “replaced” :slight_smile: Page Administration is just the title of the special UI used to manipulate what is stored in the WebPreferences document. If you look at the address bar in of your browser you will see that you are on https://mydomain.com/xwiki/bin/admin/MyPage/WebPreferences.

Other subject: newGroupDocRef.toString() is not really an API and you are taking a risk by doing that. The clean way to serialize this reference is using an EntityReferenceSerializer and in this case it would be best to use the “compactwiki” one with the webPreferences reference as second parameter if you want something closer to the UI behavior. See https://extensions.xwiki.org/xwiki/bin/view/Extension/Model+Module#HSerializingaReference.

Thank you for your quick response! :+1:

I now managed to set the group reference in the rights object correctly, and also used compactwiki to serialize the group reference.

Also thank you for explanation on the the WebPreferences and Page Administration. :slight_smile: