Skip to content

Commit

Permalink
XWIKI-4671: Add ability to register Wiki Macros only for a given Wiki…
Browse files Browse the repository at this point in the history
… or for a given User

What remains to be done:
* Change the way feedback is provided to the user by removing the Event Listener and instead having a Macro management page to register/unregister wiki macros.
* Fix programming rights requirements for accessing macro parameters and other stuff
* More unit tests
* Find ways to have functional tests for XEM


git-svn-id: https://svn.xwiki.org/svnroot/xwiki/platform/core/trunk@25762 f329d543-caf0-0310-9063-dda96c69346f
  • Loading branch information
vmassol committed Dec 11, 2009
1 parent b8c9503 commit 30aab34
Show file tree
Hide file tree
Showing 29 changed files with 957 additions and 448 deletions.
Expand Up @@ -314,9 +314,17 @@ void setDocumentContent(String documentName, String content, String editComment,
/**
* @param documentName The name of the document to be edited.
* @return True if current user has 'edit' access on the target document.
* @deprecated use {@link #isDocumentEditable(DocumentName)} instead
*/
boolean isDocumentEditable(String documentName);

/**
* @param documentName the name of the document to be edited.
* @return True if current user has 'edit' access on the target document.
* @since 2.2M1
*/
boolean isDocumentEditable(DocumentName documentName);

/**
* @return true if the current document's author has programming rights.
*/
Expand Down
Expand Up @@ -47,9 +47,15 @@ public interface DocumentModelBridge
* Retrieve the full name of the document, in the <code>Space.Name</code> format, for example <tt>Main.WebHome</tt>.
*
* @return A <code>String</code> representation of the document's full name.
* @deprecated use #getDocumentName instead
*/
String getFullName();

/**
* @return the full document's name, including Wiki, Space and Page
*/
DocumentName getDocumentName();

/**
* Retrieve the name of the virtual wiki this document belongs to.
*
Expand Down
15 changes: 9 additions & 6 deletions xwiki-core/pom.xml
Expand Up @@ -550,11 +550,6 @@
</dependency>

<!-- Other XWiki modules -->
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-core-component-default</artifactId>
<version>${pom.version}</version>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-core-configuration-default</artifactId>
Expand Down Expand Up @@ -615,7 +610,8 @@
<artifactId>xwiki-core-chart-renderer</artifactId>
<version>${pom.version}</version>
</dependency>
<!-- XWiki rendering macros -->
<!-- XWiki rendering macros, listed here so that they are included in the distribution
TODO: Move these dependencies elsewhere. -->
<!-- Please try to keep them in alphabetical order -->
<dependency>
<groupId>org.xwiki.platform</groupId>
Expand Down Expand Up @@ -793,6 +789,13 @@
-->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-core-component-multi</artifactId>
<version>${pom.version}</version>
<!-- Only required at runtime (ie in the distribution) -->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-core-query-xwql</artifactId>
Expand Down
Expand Up @@ -495,12 +495,24 @@ public boolean isDocumentViewable(String documentName)
* {@inheritDoc}
*
* @see DocumentAccessBridge#isDocumentEditable(String)
* @deprecated use {@link #isDocumentEditable(DocumentName)} instead
*/
public boolean isDocumentEditable(String documentName)
{
return hasRight(documentName, "edit");
}

/**
* {@inheritDoc}
*
* @see DocumentAccessBridge#isDocumentEditable(org.xwiki.bridge.DocumentName)
* @since 2.2M1
*/
public boolean isDocumentEditable(DocumentName documentName)
{
return hasRight(this.documentNameSerializer.serialize(documentName), "edit");
}

/**
* {@inheritDoc}
*
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.xwiki.bridge.DocumentName;
import org.xwiki.bridge.DocumentNameSerializer;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
Expand All @@ -40,6 +41,8 @@
import org.xwiki.rendering.macro.wikibridge.WikiMacroException;
import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor;
import org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor;
import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
Expand Down Expand Up @@ -70,7 +73,7 @@ public class DefaultWikiMacroFactory extends AbstractLogEnabled implements WikiM
* Used to serialize document names into strings.
*/
@Requirement
private DocumentNameSerializer docNameSerializer;
private DocumentNameSerializer documentNameSerializer;

/**
* Utility method for accessing XWikiContext.
Expand All @@ -85,11 +88,11 @@ private XWikiContext getContext()
/**
* {@inheritDoc}
*/
public WikiMacro createWikiMacro(String documentName) throws WikiMacroException
public WikiMacro createWikiMacro(DocumentName documentName) throws WikiMacroException
{
XWikiDocument doc = null;
XWikiDocument doc;
try {
doc = getContext().getWiki().getDocument(documentName, getContext());
doc = getContext().getWiki().getDocument(this.documentNameSerializer.serialize(documentName), getContext());
} catch (XWikiException ex) {
throw new WikiMacroException(String.format(
"Could not build macro from : [%s], unable to load document", documentName), ex);
Expand All @@ -100,26 +103,27 @@ public WikiMacro createWikiMacro(String documentName) throws WikiMacroException
/**
* Creates a {@link WikiMacro} from an {@link XWikiDocument} which contains a macro definition.
*
* @param doc the {@link XWikiDocument} to look for a macro definition.
* @return a {@link WikiMacro} found inside the document.
* @throws org.xwiki.rendering.macro.wikibridge.WikiMacroException invalid macro definition / no macro definition found.
* @param doc the {@link XWikiDocument} to look for a macro definition
* @return the {@link WikiMacro} found inside the document
* @throws WikiMacroException when an invalid macro definition or no macro definition was found
*/
private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException
{
String fullDocumentName = docNameSerializer.serialize(doc.getDocumentName());
DocumentName documentName = doc.getDocumentName();

// Check whether this document contains a macro definition.
BaseObject macroDefinition = doc.getObject(WIKI_MACRO_CLASS);
if (null == macroDefinition) {
throw new WikiMacroException(String.format("No macro definition found in document : [%s]",
fullDocumentName));
throw new WikiMacroException(String.format("No macro definition found in document : [%s]", documentName));
}

// Extract macro definition.
String macroId = macroDefinition.getStringValue(MACRO_ID_PROPERTY);
String macroName = macroDefinition.getStringValue(MACRO_NAME_PROPERTY);
String macroDescription = macroDefinition.getStringValue(MACRO_DESCRIPTION_PROPERTY);
String macroDefaultCategory = macroDefinition.getStringValue(MACRO_DEFAULT_CATEGORY_PROPERTY);
WikiMacroVisibility macroVisibility = WikiMacroVisibility.fromString(macroDefinition.getStringValue(
MACRO_VISIBILITY_PROPERTY));
boolean macroSupportsInlineMode = (macroDefinition.getIntValue(MACRO_INLINE_PROPERTY) == 0) ? false : true;
String macroContentType = macroDefinition.getStringValue(MACRO_CONTENT_TYPE_PROPERTY);
String macroContentDescription = macroDefinition.getStringValue(MACRO_CONTENT_DESCRIPTION_PROPERTY);
Expand All @@ -128,29 +132,27 @@ private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException
// Verify macro id.
if (StringUtils.isEmpty(macroId)) {
throw new WikiMacroException(String.format(
"Incomplete macro definition in [%s], macro id is empty", fullDocumentName));
"Incomplete macro definition in [%s], macro id is empty", documentName));
}

// Verify macro name.
if (StringUtils.isEmpty(macroName)) {
macroName = macroId;
getLogger().warn(
String.format("Incomplete macro definition in [%s], macro name is empty", fullDocumentName));
String.format("Incomplete macro definition in [%s], macro name is empty", documentName));
}

// Verify macro description.
if (StringUtils.isEmpty(macroDescription)) {
getLogger().warn(
String.format("Incomplete macro definition in [%s], macro description is empty", fullDocumentName));
String.format("Incomplete macro definition in [%s], macro description is empty", documentName));
}

// Verify default macro category.
if (StringUtils.isEmpty(macroDefaultCategory)) {
macroDefaultCategory = null;
getLogger()
.warn(
String.format("Incomplete macro definition in [%s], default macro category is empty",
fullDocumentName));
getLogger().warn(String.format("Incomplete macro definition in [%s], default macro category is empty",
documentName));
}

// Verify macro content type.
Expand All @@ -161,14 +163,14 @@ private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException
// Verify macro content description.
if (!macroContentType.equals(MACRO_CONTENT_EMPTY) && StringUtils.isEmpty(macroContentDescription)) {
String errorMsg = "Incomplete macro definition in [%s], macro content description is empty";
getLogger().warn(String.format(errorMsg, fullDocumentName));
getLogger().warn(String.format(errorMsg, documentName));
macroContentDescription = "Macro content";
}

// Verify macro code.
if (StringUtils.isEmpty(macroCode)) {
throw new WikiMacroException(String.format(
"Incomplete macro definition in [%s], macro code is empty", fullDocumentName));
"Incomplete macro definition in [%s], macro code is empty", documentName));
}

// Extract macro parameters.
Expand All @@ -190,13 +192,13 @@ private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException
// Verify parameter name.
if (StringUtils.isEmpty(parameterName)) {
throw new WikiMacroException(String.format(
"Incomplete macro definition in [%s], macro parameter name is empty", fullDocumentName));
"Incomplete macro definition in [%s], macro parameter name is empty", documentName));
}

// Verify parameter description.
if (StringUtils.isEmpty(parameterDescription)) {
String errorMessage = "Incomplete macro definition in [%s], macro parameter description is empty";
getLogger().warn(String.format(errorMessage, fullDocumentName));
getLogger().warn(String.format(errorMessage, documentName));
}

// Create the parameter descriptor.
Expand All @@ -214,21 +216,22 @@ private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException

// Create macro descriptor.
MacroDescriptor macroDescriptor = new WikiMacroDescriptor(macroName, macroDescription, macroDefaultCategory,
contentDescriptor, parameterDescriptors);
macroVisibility, contentDescriptor, parameterDescriptors);

// Create & return the macro.
return new DefaultWikiMacro(fullDocumentName, macroId, macroSupportsInlineMode, macroDescriptor, macroCode,
return new DefaultWikiMacro(documentName, macroId, macroSupportsInlineMode, macroDescriptor, macroCode,
doc.getSyntaxId(), componentManager);
}

/**
* {@inheritDoc}
*/
public boolean containsWikiMacro(String documentName)
public boolean containsWikiMacro(DocumentName documentName)
{
boolean result = true;
boolean result;
try {
XWikiDocument doc = getContext().getWiki().getDocument(documentName, getContext());
XWikiDocument doc = getContext().getWiki().getDocument(
this.documentNameSerializer.serialize(documentName), getContext());
BaseObject macroDefinition = doc.getObject(WIKI_MACRO_CLASS);
result = (null != macroDefinition);
} catch (XWikiException ex) {
Expand Down

0 comments on commit 30aab34

Please sign in to comment.