Search in sources :

Example 1 with InsufficientPrivilegesException

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

the class DefaultWikiMacroInitializer method registerMacro.

/**
 * Register a wiki macro in the component manager, if the macro author has the required rights.
 *
 * @param wikiMacroDocumentReference the document holding the macro definition
 * @param wikiMacroDocumentAuthor the author of the macro document
 * @param xcontext the current request context
 */
private void registerMacro(DocumentReference wikiMacroDocumentReference, String wikiMacroDocumentAuthor, XWikiContext xcontext) {
    this.logger.debug("Registering macro in document [{}]...", wikiMacroDocumentReference);
    DocumentReference originalAuthor = xcontext.getUserReference();
    try {
        WikiMacro macro = this.wikiMacroFactory.createWikiMacro(wikiMacroDocumentReference);
        this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, macro);
        this.logger.debug("Macro [{}] from document [{}] is now registered.", macro.getDescriptor().getId().getId(), 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
        this.logger.debug(ex.getMessage(), ex);
    } catch (WikiMacroException ex) {
        // Just log the exception and skip to the next.
        this.logger.error(ex.getMessage(), ex);
    } finally {
        xcontext.setUserReference(originalAuthor);
    }
}
Also used : InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with InsufficientPrivilegesException

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

the class DefaultWikiMacroManager method registerWikiMacro.

@Override
public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException {
    WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor();
    // Verify that the user has the right to register this wiki macro the chosen visibility
    if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) {
        DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
        componentDescriptor.setRoleType(Macro.class);
        componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId());
        // Save current context informations
        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(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference()));
            this.modelContext.setCurrentEntityReference(documentReference);
            // Register the macro against the right Component Manager, depending on the defined macro visibility.
            findComponentManager(macroDescriptor.getVisibility()).registerComponent(componentDescriptor, wikiMacro);
            this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro));
        } catch (Exception e) {
            throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()), e);
        } finally {
            // Restore previous context informations
            this.bridge.setCurrentUser(currentUser);
            this.modelContext.setCurrentEntityReference(currentEntityReference);
        }
    } else {
        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()));
    }
}
Also used : InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) Macro(org.xwiki.rendering.macro.Macro) 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

InsufficientPrivilegesException (org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException)2 WikiMacro (org.xwiki.rendering.macro.wikibridge.WikiMacro)2 WikiMacroException (org.xwiki.rendering.macro.wikibridge.WikiMacroException)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1 Macro (org.xwiki.rendering.macro.Macro)1 WikiMacroDescriptor (org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor)1