Ok. It took a while but now I got you
The following code works for me when listening to an DocumentUpdatingEvent:
{{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.api.*
try {
def docSource = xcontext.method.input.get(1)
def context = xcontext.method.input.get(2)
def userObject = docSource.getObject("XWiki.XWikiUsers")
// Check if updated page is a profile page
if (userObject == null) {
// Nothing
} else {
def rightsObject = docSource.getObject("XWiki.XWikiRights", true, context)
rightsObject.setLargeStringValue("groups", "XWiki.PersonalAdminGroup")
rightsObject.setStringValue("levels", "view,edit")
rightsObject.setLargeStringValue("users", "XWiki.TestUser,XWiki.TestUser2")
rightsObject.setIntValue("allow", 1)
}
} catch(e) {
System.out.println("ERROR!")
System.out.println(e.getMessage())
}
{{/groovy}}