Search in sources :

Example 1 with XWikiValidationInterface

use of com.xpn.xwiki.validation.XWikiValidationInterface in project xwiki-platform by xwiki.

the class XWikiDocument method executeValidationScript.

private boolean executeValidationScript(XWikiContext context, String validationScript) {
    try {
        ContextualAuthorizationManager authorization = Utils.getComponent(ContextualAuthorizationManager.class);
        DocumentReference validationScriptReference = getCurrentDocumentReferenceResolver().resolve(validationScript, getDocumentReference());
        // Make sure target document is allowed to execute Groovy
        // TODO: this check should probably be right in XWiki#parseGroovyFromPage
        authorization.checkAccess(Right.PROGRAM, validationScriptReference);
        XWikiValidationInterface validObject = (XWikiValidationInterface) context.getWiki().parseGroovyFromPage(validationScript, context);
        return validObject.validateDocument(this, context);
    } catch (Throwable e) {
        XWikiValidationStatus.addExceptionToContext(getFullName(), "", e, context);
        return false;
    }
}
Also used : ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) XWikiValidationInterface(com.xpn.xwiki.validation.XWikiValidationInterface) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Example 2 with XWikiValidationInterface

use of com.xpn.xwiki.validation.XWikiValidationInterface in project xwiki-platform by xwiki.

the class BaseClass method executeValidationScript.

private boolean executeValidationScript(BaseObject obj, String validationScript, XWikiContext context) {
    try {
        ContextualAuthorizationManager authorization = Utils.getComponent(ContextualAuthorizationManager.class);
        DocumentReference validationScriptReference = getCurrentDocumentReferenceResolver().resolve(validationScript, getDocumentReference());
        // Make sure target document is allowed to execute Groovy
        // TODO: this check should probably be right in XWiki#parseGroovyFromPage
        authorization.checkAccess(Right.PROGRAM, validationScriptReference);
        XWikiValidationInterface validObject = (XWikiValidationInterface) context.getWiki().parseGroovyFromPage(validationScript, context);
        return validObject.validateObject(obj, context);
    } catch (Throwable e) {
        XWikiValidationStatus.addExceptionToContext(getName(), "", e, context);
        return false;
    }
}
Also used : ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) XWikiValidationInterface(com.xpn.xwiki.validation.XWikiValidationInterface) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with XWikiValidationInterface

use of com.xpn.xwiki.validation.XWikiValidationInterface in project xwiki-platform by xwiki.

the class XWikiDocumentMockitoTest method testValidate.

@Test
public void testValidate() throws XWikiException, AccessDeniedException {
    this.document.setValidationScript("validationScript");
    this.baseClass.setValidationScript("validationScript");
    doReturn(new XWikiValidationInterface() {

        @Override
        public boolean validateObject(BaseObject object, XWikiContext context) {
            return true;
        }

        @Override
        public boolean validateDocument(XWikiDocument doc, XWikiContext context) {
            return true;
        }
    }).when(this.oldcore.getSpyXWiki()).parseGroovyFromPage("validationScript", this.oldcore.getXWikiContext());
    // With PR
    assertTrue(this.document.validate(this.oldcore.getXWikiContext()));
    assertTrue(this.baseClass.validateObject(this.baseObject, this.oldcore.getXWikiContext()));
    // Without PR
    doThrow(AccessDeniedException.class).when(this.oldcore.getMockContextualAuthorizationManager()).checkAccess(Right.PROGRAM, new DocumentReference("wiki", "space", "validationScript"));
    assertFalse(this.document.validate(this.oldcore.getXWikiContext()));
    assertFalse(this.baseClass.validateObject(this.baseObject, this.oldcore.getXWikiContext()));
}
Also used : XWikiValidationInterface(com.xpn.xwiki.validation.XWikiValidationInterface) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

XWikiValidationInterface (com.xpn.xwiki.validation.XWikiValidationInterface)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ContextualAuthorizationManager (org.xwiki.security.authorization.ContextualAuthorizationManager)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Test (org.junit.Test)1 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1