Search in sources :

Example 6 with WikiMacroDescriptor

use of org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiMacroManager method unregisterWikiMacro.

@Override
public void unregisterWikiMacro(DocumentReference documentReference) throws WikiMacroException {
    WikiMacroData macroData = this.wikiMacroMap.get(documentReference);
    if (macroData != null) {
        WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) macroData.getWikiMacro().getDescriptor();
        // Verify that the user has the right to unregister this wiki macro for the chosen visibility
        if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) {
            String currentUser = this.bridge.getCurrentUser();
            EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference();
            try {
                // Put the proper context information to let components manager use the proper keys to find
                // components to unregister
                this.bridge.setCurrentUser(this.serializer.serialize(macroData.getWikiMacro().getAuthorReference()));
                this.modelContext.setCurrentEntityReference(documentReference);
                findComponentManager(macroDescriptor.getVisibility()).unregisterComponent(Macro.class, macroData.getHint());
                this.wikiMacroMap.remove(documentReference);
            } catch (Exception e) {
                throw new WikiMacroException(String.format("Failed to unregister macro [%s] in [%s] for " + "visibility [%s]", macroData.getHint(), documentReference, macroDescriptor.getVisibility()), e);
            } finally {
                this.bridge.setCurrentUser(currentUser);
                this.modelContext.setCurrentEntityReference(currentEntityReference);
            }
        } else {
            throw new WikiMacroException(String.format("Unable to unregister macro [%s] in [%s] for visibility " + "[%s] due to insufficient privileges", macroData.getWikiMacro().getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()));
        }
    } else {
        throw new WikiMacroException(String.format("Macro in [%s] isn't registered", documentReference));
    }
}
Also used : WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) EntityReference(org.xwiki.model.reference.EntityReference) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException)

Aggregations

WikiMacroDescriptor (org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor)6 MacroId (org.xwiki.rendering.macro.MacroId)4 DefaultContentDescriptor (org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor)4 WikiMacroException (org.xwiki.rendering.macro.wikibridge.WikiMacroException)3 StringReader (java.io.StringReader)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 EntityReference (org.xwiki.model.reference.EntityReference)2 XDOM (org.xwiki.rendering.block.XDOM)2 DefaultWikiMacro (org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro)2 InsufficientPrivilegesException (org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ArrayList (java.util.ArrayList)1 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)1 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)1 WordBlock (org.xwiki.rendering.block.WordBlock)1 Macro (org.xwiki.rendering.macro.Macro)1 ContentDescriptor (org.xwiki.rendering.macro.descriptor.ContentDescriptor)1 MacroDescriptor (org.xwiki.rendering.macro.descriptor.MacroDescriptor)1 WikiMacro (org.xwiki.rendering.macro.wikibridge.WikiMacro)1