[xwiki-users] Dynamically set rights over document
Hi, I'm trying to set rights over a document automatically. In my wiki i have the "Publication" concept. Each Publication has one or more authors. Each author is an "Researcher" that is directly related to an XWikiUser. What I want to do is to set editing rights only for the authors of that publication. If I add (or remove) an author he should be able (not able) to edit the document. What i thought first was to check and add user rights every time the document was loaded. However this didn't worked because, initially, users don't have rights to add new rights (lol). That doesn't seemed a good practice anyway. What is the best way to do this? Maybe after every change in editing mode, system may apply the rights? How can I do that? Btw, I'm using this snippet to add the user rights: http://extensions.xwiki.org/xwiki/bin/view/Extension/Setting+Rights Thanks in advance, Luís Braga -- View this message in context: http://xwiki.475771.n2.nabble.com/Dynamically-set-rights-over-document-tp606... Sent from the XWiki- Users mailing list archive at Nabble.com.
On 02/24/2011 04:58 PM, Lukapt wrote:
Hi,
I'm trying to set rights over a document automatically.
In my wiki i have the "Publication" concept. Each Publication has one or more authors. Each author is an "Researcher" that is directly related to an XWikiUser. What I want to do is to set editing rights only for the authors of that publication. If I add (or remove) an author he should be able (not able) to edit the document.
What i thought first was to check and add user rights every time the document was loaded. However this didn't worked because, initially, users don't have rights to add new rights (lol). That doesn't seemed a good practice anyway.
What is the best way to do this? Maybe after every change in editing mode, system may apply the rights? How can I do that?
With a Groovy listener. See this tutorial for a starting point: http://platform.xwiki.org/xwiki/bin/DevGuide/GroovyNotificationTutorial Or you can go low level and write a Java component that does the same thing, this should be a bit more bulletproof (you have to register the groovy listener when the wiki restarts). An alternative is not to update rights when changing the document, but to plug in a different XWikiRightsService implementation that checks the authors instead of the rights objects when determining if the current user has edit right on a document. For other rights, or if there are no authors, fall back to the default XWikiRightServiceImpl base class.
Btw, I'm using this snippet to add the user rights: http://extensions.xwiki.org/xwiki/bin/view/Extension/Setting+Rights
Thanks in advance,
Luís Braga
-- Sergiu Dumitriu http://purl.org/net/sergiu/
Hi, Thank you for the reply! I'm trying to implement the groovy listener but I'm doing something wrong.. This is what I'm trying to do: {{groovy}} import org.xwiki.observation.* import org.xwiki.observation.event.* import com.xpn.xwiki.web.* import com.xpn.xwiki.* class RightsEventListener implements EventListener { def xwiki def context def doc RightsEventListener(xwiki, context, doc) { this.xwiki = xwiki this.context = context this.doc = doc } String getName() { // The unique name of this event listener return "dynamicRights" } List<Event> getEvents() { // The list of events this listener listens to return Arrays.asList(new DocumentUpdateEvent()) } // Called by the Observation Manager when an event matches the list of events returned // by getEvents() void onEvent(Event event, Object source, Object data) { def rightsObject = doc.getObject("XWiki.XWikiRights", true) rightsObject.set("groups", "") rightsObject.set("levels", "edit") rightsObject.set("users", "Luis Braga") rightsObject.set("allow", 1) doc.save() } } // Register against the Observation Manager def observation = Utils.getComponent(ObservationManager.class) observation.removeListener("dynamicRights") def listener = new RightsEventListener(xwiki, xcontext, doc) observation.addListener(listener) {{/groovy}} When I update the file I get an error: Detailed information: Error number 3201 in 3: Exception while saving document xwiki:Publication.FonteICEIS2010 Wrapped Exception: Failed to commit or rollback transaction. Root cause [] com.xpn.xwiki.XWikiException: Error number 3201 in 3: Exception while saving document xwiki:Publication.FonteICEIS2010 Wrapped Exception: Failed to commit or rollback transaction. Root cause [] at com.xpn.xwiki.store.XWikiHibernateStore.saveXWikiDoc(XWikiHibernateStore.java:653) at com.xpn.xwiki.store.XWikiCacheStore.saveXWikiDoc(XWikiCacheStore.java:181) at com.xpn.xwiki.store.XWikiCacheStore.saveXWikiDoc(XWikiCacheStore.java:175) at com.xpn.xwiki.XWiki.saveDocument(XWiki.java:1451) at com.xpn.xwiki.web.SaveAction.save(SaveAction.java:162) at com.xpn.xwiki.web.SaveAction.action(SaveAction.java:184) at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:215) at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:117) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) (...) For my surprise the XWikiRights object is created but don't work. If I create the exactly same object manually it works. Luís Braga -- View this message in context: http://xwiki.475771.n2.nabble.com/Dynamically-set-rights-over-document-tp606... Sent from the XWiki- Users mailing list archive at Nabble.com.
Still unable to create a correct Groovy macro :( It would be great if someone could help me on this. Thanks! Luís Braga -- View this message in context: http://xwiki.475771.n2.nabble.com/Dynamically-set-rights-over-document-tp606... Sent from the XWiki- Users mailing list archive at Nabble.com.
participants (2)
-
Lukapt -
Sergiu Dumitriu