[xwiki-users] Dynamically create space and add rights and members
Hi Developers, i have been trying this the whole time already and decided to ask for help. I want to create a space, a group and add a member to the group. The space should only be viewed by this group. Here is my code till now. The Space is created, the group too and the user login in is added to the group. I can't figure how to assign the rights. The CODE: protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException{ // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "myspace", "Testgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context); BaseClass rightsClass = context.getWiki().getRightsClass(context); if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname); BaseObject rights= groupDoc.newXObject(rightsClass.getDocumentReference(),context); rights.setStringValue("groups", "Testgroup"); rights.setStringValue("levels","view"); rights.setStringValue("levels", "edit"); rights.setStringValue("users", ""); rights.setIntValue("allow",1); //groupDoc.getObject("XWiki.XWikiGlobalRights"); groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}"); // Save modifications context.getWiki().saveDocument(groupDoc, context); } log.error("Added user"+ xwikiname +"to Testgroup"); } } Thanks for any help.
Hi, to add access rights to a space, you have to add an "XWiki.XWikiRight" object to the page <spacename>."WebPreferences". I have not tested it, but it should look somewhat like: DocumentReference spacePrefsReference = new DocumentReference( context.getWiki().getName(), spaceName, "WebPreferences") XWikiDocument spacePrefs = context.getWiki().getDocument(spacePrefsReference, context) [...] then add the "rights" object like you did in your code, except that you add it to spacePrefs instead of groupDoc. (I guess you also need to set rights.setStringValue("levels","view,edit"); instead of doing two calls) Do not forget to save spacePrefs, too. hope this helps, clemens On 04/19/2014 08:14 AM, Daniel Ebanja wrote:
Hi Developers, i have been trying this the whole time already and decided to ask for help. I want to create a space, a group and add a member to the group. The space should only be viewed by this group. Here is my code till now. The Space is created, the group too and the user login in is added to the group. I can't figure how to assign the rights.
The CODE:
protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException{ // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "myspace", "Testgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context);
BaseClass rightsClass = context.getWiki().getRightsClass(context);
if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname);
BaseObject rights= groupDoc.newXObject(rightsClass.getDocumentReference(),context); rights.setStringValue("groups", "Testgroup"); rights.setStringValue("levels","view"); rights.setStringValue("levels", "edit"); rights.setStringValue("users", ""); rights.setIntValue("allow",1); //groupDoc.getObject("XWiki.XWikiGlobalRights");
groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}");
// Save modifications context.getWiki().saveDocument(groupDoc, context); } log.error("Added user"+ xwikiname +"to Testgroup"); }
}
Thanks for any help. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
Hi does someone please see what i am getting wrong here? The rights are not just beeing set. protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException { // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "Myspace", "Testgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context); BaseClass rightsClass = context.getWiki().getRightsClass(context); // trying to see how to add rights if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname); // groupDoc.getObject("XWiki.XWikiGlobalRights"); // If the document is new, set its content groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}"); // Save modifications context.getWiki().saveDocument(groupDoc, context); DocumentReference spacePrefsRef = new DocumentReference(context .getWiki().getDatabase(), "Myspace", "WebPreferences"); XWikiDocument spacePrefs = context.getWiki().getDocument( spacePrefsRef, context); BaseObject rights = spacePrefs.newXObject( rightsClass.getDocumentReference(), context); rights.setStringValue("groups", "XWiki.XWikiAllGroup"); rights.setStringValue("levels", "view,edit"); rights.setStringValue("users", ""); rights.setIntValue("deny", 1); context.getWiki().saveDocument(spacePrefs, context); } log.error("Added user" + xwikiname + "to Testgroup"); } } thanks in advance. 2014-04-19 16:09 GMT+02:00 Clemens Klein-Robbenhaar < [email protected]>:
Hi,
to add access rights to a space, you have to add an "XWiki.XWikiRight" object to the page <spacename>."WebPreferences".
I have not tested it, but it should look somewhat like:
DocumentReference spacePrefsReference = new DocumentReference( context.getWiki().getName(), spaceName, "WebPreferences") XWikiDocument spacePrefs = context.getWiki().getDocument(spacePrefsReference, context) [...] then add the "rights" object like you did in your code, except that you add it to spacePrefs instead of groupDoc. (I guess you also need to set rights.setStringValue("levels","view,edit"); instead of doing two calls) Do not forget to save spacePrefs, too.
hope this helps, clemens
On 04/19/2014 08:14 AM, Daniel Ebanja wrote:
Hi Developers, i have been trying this the whole time already and decided to ask for help. I want to create a space, a group and add a member to the group. The space should only be viewed by this group. Here is my code till now. The Space is created, the group too and the user login in is added to the group. I can't figure how to assign the rights.
The CODE:
protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException{ // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "myspace", "Testgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context);
BaseClass rightsClass = context.getWiki().getRightsClass(context);
if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname);
BaseObject rights= groupDoc.newXObject(rightsClass.getDocumentReference(),context); rights.setStringValue("groups", "Testgroup"); rights.setStringValue("levels","view"); rights.setStringValue("levels", "edit"); rights.setStringValue("users", ""); rights.setIntValue("allow",1); //groupDoc.getObject("XWiki.XWikiGlobalRights");
groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}");
// Save modifications context.getWiki().saveDocument(groupDoc, context); } log.error("Added user"+ xwikiname +"to Testgroup"); }
}
Thanks for any help. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Ebanja Daniel Informatikstudent der Hochschule Darmstadt Deutschland
Hi Developers, i like came up with this. It functions just well, but i have a problem: I have to safe all 3 Documents as you can see in the code below. -What if the Users created some other pages or add attachments to that space and don't set the rights so that only members of the group should see the stuff in the space? I guess i will have some space with some hidden docs and some public. -So how could i set the rights globally for the space? -I am trying to do this upon authentication. i.e user transmits me the name of the group and space to create upon Authentication.. Problem is at the time of creation the user is not yet logged in. So i see space created by unknown User. -Could anyone please help with some ideas. Thanks Clemens, and thank you all in advance. The code: protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException { // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group. Rights are set for the group when the group is saved in Xwiki space DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "XWiki", "XWikiTestgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context); BaseClass rightsClass = context.getWiki().getRightsClass(context); // trying to see how to add rights if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname); // groupDoc.getObject("XWiki.XWikiGlobalRights"); // If the document is new, set its content groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}"); BaseObject grouprights = groupDoc.newXObject( rightsClass.getDocumentReference(), context); grouprights.setStringValue("groups", "XWiki.XWikiTestgroup"); grouprights.setStringValue("levels", "view"); grouprights.setStringValue("levels", "edit"); grouprights.setStringValue("users", ""); grouprights.setIntValue("deny", 1); // Save modifications context.getWiki().saveDocument(groupDoc, context); DocumentReference spacePrefsRef = new DocumentReference(context .getWiki().getDatabase(), "XWiki.Myspace", "WebPreferences"); XWikiDocument spacePrefs = context.getWiki().getDocument( spacePrefsRef, context); BaseObject rights = spacePrefs.newXObject( rightsClass.getDocumentReference(), context); rights.setStringValue("groups", "XWiki.XWikiTestgroup"); rights.setStringValue("levels", "view,edit"); rights.setStringValue("users", ""); rights.setIntValue("deny", 1); context.getWiki().saveDocument(spacePrefs, context); DocumentReference spacePrefsRefWebh = new DocumentReference(context .getWiki().getDatabase(), "XWiki.Myspace", "WebHome"); XWikiDocument spacePrefsWh = context.getWiki().getDocument( spacePrefsRefWebh, context); BaseObject rightsWh = spacePrefsWh.newXObject( rightsClass.getDocumentReference(), context); rightsWh.setStringValue("groups", "XWiki.XWikiTestgroup"); rightsWh.setStringValue("levels", "view,edit"); rightsWh.setStringValue("users", ""); rightsWh.setIntValue("deny", 1); context.getWiki().saveDocument(spacePrefsWh, context); } log.error("Added user" + xwikiname + "to Testgroup"); } } 2014-04-21 5:06 GMT+02:00 Daniel Ebanja <[email protected]>:
Hi does someone please see what i am getting wrong here? The rights are not just beeing set.
protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException { // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "Myspace", "Testgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context);
BaseClass rightsClass = context.getWiki().getRightsClass(context); // trying to see how to add rights
if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname);
// groupDoc.getObject("XWiki.XWikiGlobalRights");
// If the document is new, set its content
groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}");
// Save modifications context.getWiki().saveDocument(groupDoc, context);
DocumentReference spacePrefsRef = new DocumentReference(context .getWiki().getDatabase(), "Myspace", "WebPreferences"); XWikiDocument spacePrefs = context.getWiki().getDocument( spacePrefsRef, context); BaseObject rights = spacePrefs.newXObject( rightsClass.getDocumentReference(), context); rights.setStringValue("groups", "XWiki.XWikiAllGroup");
rights.setStringValue("levels", "view,edit"); rights.setStringValue("users", ""); rights.setIntValue("deny", 1); context.getWiki().saveDocument(spacePrefs, context);
} log.error("Added user" + xwikiname + "to Testgroup"); }
}
thanks in advance.
2014-04-19 16:09 GMT+02:00 Clemens Klein-Robbenhaar < [email protected]>:
Hi,
to add access rights to a space, you have to add an "XWiki.XWikiRight" object to the page <spacename>."WebPreferences".
I have not tested it, but it should look somewhat like:
DocumentReference spacePrefsReference = new DocumentReference( context.getWiki().getName(), spaceName, "WebPreferences") XWikiDocument spacePrefs = context.getWiki().getDocument(spacePrefsReference, context) [...] then add the "rights" object like you did in your code, except that you add it to spacePrefs instead of groupDoc. (I guess you also need to set rights.setStringValue("levels","view,edit"); instead of doing two calls) Do not forget to save spacePrefs, too.
hope this helps, clemens
On 04/19/2014 08:14 AM, Daniel Ebanja wrote:
Hi Developers, i have been trying this the whole time already and decided to ask for help. I want to create a space, a group and add a member to the group. The space should only be viewed by this group. Here is my code till now. The Space is created, the group too and the user login in is added to the group. I can't figure how to assign the rights.
The CODE:
protected void addUserToGroup(String xwikiname, XWikiContext context) throws XWikiException{ // // Create a group and add the user to the newly created group. BaseClass groupClass = context.getWiki().getGroupClass(context); // Get document representing group DocumentReference groupDocumentReference = new DocumentReference( context.getDatabase(), "myspace", "Testgroup"); XWikiDocument groupDoc = context.getWiki().getDocument( groupDocumentReference, context);
BaseClass rightsClass = context.getWiki().getRightsClass(context);
if (groupDoc.isNew()) { log.error("Creating Test group"); synchronized (groupDoc) { // Add a member object to document BaseObject memberObj = groupDoc.newXObject( groupClass.getDocumentReference(), context); memberObj.setStringValue("member", xwikiname);
BaseObject rights= groupDoc.newXObject(rightsClass.getDocumentReference(),context); rights.setStringValue("groups", "Testgroup"); rights.setStringValue("levels","view"); rights.setStringValue("levels", "edit"); rights.setStringValue("users", ""); rights.setIntValue("allow",1); //groupDoc.getObject("XWiki.XWikiGlobalRights");
groupDoc.setSyntax(Syntax.XWIKI_2_1); groupDoc.setContent("{{include document='XWiki.XWikiGroupSheet' /}}");
// Save modifications context.getWiki().saveDocument(groupDoc, context); } log.error("Added user"+ xwikiname +"to Testgroup"); }
}
Thanks for any help. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Ebanja Daniel Informatikstudent der Hochschule Darmstadt Deutschland
-- Ebanja Daniel Informatikstudent der Hochschule Darmstadt Deutschland
participants (2)
-
Clemens Klein-Robbenhaar -
Daniel Ebanja