Index: xwiki-core/src/main/java/com/xpn/xwiki/internal/DefaultWikiMacroInitializer.java =================================================================== --- xwiki-core/src/main/java/com/xpn/xwiki/internal/DefaultWikiMacroInitializer.java (revision 30762) +++ xwiki-core/src/main/java/com/xpn/xwiki/internal/DefaultWikiMacroInitializer.java (working copy) @@ -34,6 +34,7 @@ import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.SpaceReference; import org.xwiki.model.reference.WikiReference; +import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; import org.xwiki.rendering.macro.wikibridge.WikiMacro; import org.xwiki.rendering.macro.wikibridge.WikiMacroException; import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory; @@ -198,6 +199,10 @@ xcontext.setUser(wikiMacroDocumentAuthor); this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, macro); LOG.debug("Registered macro " + wikiMacroDocumentReference); + } catch (InsufficientPrivilegesException ex) { + // Just log the exception and skip to the next. + // We only log at the debug level here as this is not really an error + getLogger().debug(ex.getMessage(), ex); } catch (WikiMacroException ex) { // Just log the exception and skip to the next. getLogger().error(ex.getMessage(), ex); Index: xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/test/java/org/xwiki/rendering/internal/macro/wikibridge/DefaultWikiMacroManagerTest.java =================================================================== --- xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/test/java/org/xwiki/rendering/internal/macro/wikibridge/DefaultWikiMacroManagerTest.java (revision 30762) +++ xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/test/java/org/xwiki/rendering/internal/macro/wikibridge/DefaultWikiMacroManagerTest.java (working copy) @@ -30,6 +30,7 @@ import org.xwiki.rendering.macro.MacroId; import org.xwiki.rendering.macro.MacroManager; import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; +import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor; import org.xwiki.rendering.macro.wikibridge.WikiMacroException; import org.xwiki.rendering.macro.wikibridge.WikiMacroManager; @@ -146,7 +147,7 @@ Assert.assertEquals(0, registeredMacro.compareTo(wikiMacro)); } - @org.junit.Test + @org.junit.Test(expected=InsufficientPrivilegesException.class) public void testRegisterWikiMacroWhenGlobalVisibilityAndNotAllowed() throws Exception { final DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL); @@ -156,18 +157,10 @@ allowing(mockDocumentAccessBridge).hasProgrammingRights(); will(returnValue(false)); }}); - try { - wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); - Assert.fail("Should have raised an exception here"); - } catch (WikiMacroException e) { - Assert.assertEquals("Unable to register macro [testwikimacro] in " - + "[name = [TestWikiMacro], type = [DOCUMENT], parent = [name = [Main], type = [SPACE], " - + "parent = [name = [xwiki], type = [WIKI], parent = [null]]]] for visibility [GLOBAL] due to " - + "insufficient privileges", e.getMessage()); - } + wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); } - @org.junit.Test + @org.junit.Test(expected=InsufficientPrivilegesException.class) public void testRegisterWikiMacroWhenWikiVisibilityAndNotAllowed() throws Exception { final DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI); @@ -178,18 +171,10 @@ will(returnValue(false)); }}); - try { - wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); - Assert.fail("Should have raised an exception here"); - } catch (WikiMacroException e) { - Assert.assertEquals("Unable to register macro [testwikimacro] in [name = [TestWikiMacro], " - + "type = [DOCUMENT], parent = [name = [Main], type = [SPACE], parent = [name = [xwiki], " - + "type = [WIKI], parent = [null]]]] for visibility [WIKI] due to insufficient privileges", - e.getMessage()); - } + wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); } - @org.junit.Test + @org.junit.Test(expected=InsufficientPrivilegesException.class) public void testRegisterWikiMacroWhenUserVisibilityAndNotAllowed() throws Exception { final DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER); @@ -200,15 +185,7 @@ will(returnValue(false)); }}); - try { - wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); - Assert.fail("Should have raised an exception here"); - } catch (WikiMacroException e) { - Assert.assertEquals("Unable to register macro [testwikimacro] in [name = [TestWikiMacro], " - + "type = [DOCUMENT], parent = [name = [Main], type = [SPACE], parent = [name = [xwiki], " - + "type = [WIKI], parent = [null]]]] for visibility [USER] due to insufficient privileges", - e.getMessage()); - } + wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); } private DefaultWikiMacro generateWikiMacro(WikiMacroVisibility visibility) throws Exception Index: xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/macro/wikibridge/InsufficientPrivilegesException.java =================================================================== --- xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/macro/wikibridge/InsufficientPrivilegesException.java (revision 0) +++ xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/macro/wikibridge/InsufficientPrivilegesException.java (revision 0) @@ -0,0 +1,47 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.rendering.macro.wikibridge; + +/** + * Indicate that a Wiki Macro operation is refused because of insufficient user privileges. This is for example thrown + * when a trying to register a macro of global visibility and with a macro document not saved with programming right. + * + * @version $Id$ + * @since 2.5M1 + */ +public class InsufficientPrivilegesException extends Exception +{ + /** + * Class version. + */ + private static final long serialVersionUID = 7945701891355704070L; + + /** + * Builds a new {@link InsufficientPrivilegesException} with the given error message. + * + * @param message error messge. + */ + public InsufficientPrivilegesException(String message) + { + super(message); + } + + +} Index: xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/macro/wikibridge/WikiMacroManager.java =================================================================== --- xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/macro/wikibridge/WikiMacroManager.java (revision 30762) +++ xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/macro/wikibridge/WikiMacroManager.java (working copy) @@ -42,18 +42,18 @@ * * @param documentReference the name of the document which contains the wiki macro * @param wikiMacro the {@link org.xwiki.rendering.macro.wikibridge.WikiMacro} instance - * @exception WikiMacroException if a problem happened when registering the macro (document doesn't exist, - * not enough privilege, etc) + * @exception InsufficientPrivilegesException if asked visibility is not allowed + * @exception WikiMacroException if a problem happened when registering the macro (document doesn't exist, etc.) * @since 2.2.M1 */ - void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws WikiMacroException; + void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) + throws InsufficientPrivilegesException, WikiMacroException; /** * Unregisters the wiki macro defined on the given document (if there is one). * * @param documentReference the name of the document which contains the wiki macro - * @exception WikiMacroException if a problem happened when registering the macro (document doesn't exist, - * not enough privilege, etc) + * @exception WikiMacroException if a problem happened when registering the macro (document doesn't exist, etc) * @since 2.2.M1 */ void unregisterWikiMacro(DocumentReference documentReference) throws WikiMacroException; Index: xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/internal/macro/wikibridge/DefaultWikiMacroManager.java =================================================================== --- xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/internal/macro/wikibridge/DefaultWikiMacroManager.java (revision 30762) +++ xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/internal/macro/wikibridge/DefaultWikiMacroManager.java (working copy) @@ -32,6 +32,7 @@ import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.EntityReference; import org.xwiki.rendering.macro.Macro; +import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; import org.xwiki.rendering.macro.wikibridge.WikiMacro; import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor; import org.xwiki.rendering.macro.wikibridge.WikiMacroException; @@ -146,7 +147,8 @@ * @see WikiMacroManager#registerWikiMacro(org.xwiki.model.reference.DocumentReference , WikiMacro) * @since 2.2M1 */ - public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws WikiMacroException + public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) + throws InsufficientPrivilegesException, WikiMacroException { WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); @@ -166,9 +168,9 @@ wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()), e); } } else { - throw new WikiMacroException(String.format("Unable to register macro [%s] in [%s] for visibility [%s] " - + "due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, - macroDescriptor.getVisibility())); + throw new InsufficientPrivilegesException(String.format( + "Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro + .getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); } } Index: xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/internal/macro/wikibridge/WikiMacroEventListener.java =================================================================== --- xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/internal/macro/wikibridge/WikiMacroEventListener.java (revision 30762) +++ xwiki-rendering/xwiki-rendering-macros/xwiki-rendering-macro-wikibridge/src/main/java/org/xwiki/rendering/internal/macro/wikibridge/WikiMacroEventListener.java (working copy) @@ -33,6 +33,7 @@ import org.xwiki.observation.event.DocumentSaveEvent; import org.xwiki.observation.event.DocumentUpdateEvent; import org.xwiki.observation.event.Event; +import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; import org.xwiki.rendering.macro.wikibridge.WikiMacro; import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory; import org.xwiki.rendering.macro.wikibridge.WikiMacroException; @@ -140,6 +141,9 @@ getLogger().debug( String.format("Unable to register macro [%s] in document [%s]", wikiMacro.getDescriptor().getId() .getId(), documentReference), e); + } catch (InsufficientPrivilegesException e) { + getLogger().debug(String.format("Insufficient privileges for registering macro [%s] in document [%s]", + wikiMacro.getDescriptor().getId().getId(), documentReference), e); } }