Search in sources :

Example 66 with WikiReference

use of org.xwiki.model.reference.WikiReference in project xwiki-platform by xwiki.

the class WikiCreationJobScriptServices method createWiki.

/**
 * Asynchronously create a wiki.
 *
 * @param request creation wiki request containing all information about the wiki to create
 * @return the creationjob that creates the wiki
 */
public Job createWiki(WikiCreationRequest request) {
    try {
        // Verify that the user has the CREATE_WIKI right
        XWikiContext xcontext = xcontextProvider.get();
        WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId());
        authorizationManager.checkAccess(Right.CREATE_WIKI, xcontext.getUserReference(), mainWikiReference);
        // Verify that if an extension id is provided, this extension is authorized.
        if (request.getExtensionId() != null) {
            if (!isAuthorizedExtension(request.getExtensionId())) {
                throw new WikiCreationException(String.format("The extension [%s] is not authorized.", request.getExtensionId()));
            }
        }
        return wikiCreator.createWiki(request);
    } catch (WikiCreationException e) {
        setLastError(e);
        logger.warn("Failed to create a new wiki.", e);
    } catch (AccessDeniedException e) {
        setLastError(e);
    }
    return null;
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiCreationException(org.xwiki.platform.wiki.creationjob.WikiCreationException) XWikiContext(com.xpn.xwiki.XWikiContext) WikiReference(org.xwiki.model.reference.WikiReference)

Example 67 with WikiReference

use of org.xwiki.model.reference.WikiReference in project xwiki-platform by xwiki.

the class WikiCreationJobScriptServicesTest method createWikiWhenNoCreateWikiRight.

@Test
public void createWikiWhenNoCreateWikiRight() throws Exception {
    DocumentReference currentUser = new DocumentReference("xwiki", "XWiki", "User");
    when(xcontext.getUserReference()).thenReturn(currentUser);
    AccessDeniedException exception = new AccessDeniedException(Right.CREATE_WIKI, currentUser, new WikiReference("mainWikiId"));
    doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUser), eq(new WikiReference("mainWikiId")));
    WikiCreationRequest wikiCreationRequest = new WikiCreationRequest();
    wikiCreationRequest.setExtensionId("authorized-extension", "1.0");
    assertNull(mocker.getComponentUnderTest().createWiki(wikiCreationRequest));
    Exception lastError = mocker.getComponentUnderTest().getLastError();
    assertNotNull(lastError);
    assertEquals(exception, lastError);
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiCreationRequest(org.xwiki.platform.wiki.creationjob.WikiCreationRequest) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) Test(org.junit.Test)

Example 68 with WikiReference

use of org.xwiki.model.reference.WikiReference in project xwiki-platform by xwiki.

the class WatchListMessageDataExtractorTest method relativeTemplateDefaultTemplateMainWiki.

@Test
public void relativeTemplateDefaultTemplateMainWiki() throws Exception {
    // Setup the user profile data to extract.
    setUpUserObject();
    // Setup the template
    String templateStringReference = "Some.Template";
    parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
    DocumentReference userWikiTemplateReference = new DocumentReference(testSubscriberReference.getWikiReference().getName(), "Some", "Template");
    when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, testSubscriberReference.getWikiReference())).thenReturn(userWikiTemplateReference);
    when(mockWiki.exists(userWikiTemplateReference, mockContext)).thenReturn(false);
    String currentWikiId = "someWiki";
    when(mockContext.getWikiId()).thenReturn(currentWikiId);
    WikiReference currentWikiReference = new WikiReference(currentWikiId);
    DocumentReference currentWikiTemplateReference = new DocumentReference(currentWikiId, "Some", "Template");
    when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, currentWikiReference)).thenReturn(currentWikiTemplateReference);
    when(mockWiki.exists(currentWikiTemplateReference, mockContext)).thenReturn(false);
    DocumentReference defaultCurrentWikiTemplateReference = new DocumentReference(currentWikiId, "XWiki", "WatchListMessage");
    when(mockExplicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, currentWikiReference)).thenReturn(defaultCurrentWikiTemplateReference);
    when(mockWiki.exists(defaultCurrentWikiTemplateReference, mockContext)).thenReturn(false);
    String mainWikiId = "xwiki";
    when(mockContext.getMainXWiki()).thenReturn(mainWikiId);
    WikiReference mainWikiReference = new WikiReference(mainWikiId);
    DocumentReference defaultMainWikiTemplateReference = new DocumentReference(mainWikiId, "XWiki", "WatchListMessage");
    when(mockExplicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, mainWikiReference)).thenReturn(defaultMainWikiTemplateReference);
    testTemplateReference = defaultMainWikiTemplateReference;
    // Run the extraction.
    WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
    // Check the extracted data.
    assertResult(result);
}
Also used : WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 69 with WikiReference

use of org.xwiki.model.reference.WikiReference in project xwiki-platform by xwiki.

the class WatchListMessageDataExtractorTest method relativeTemplateContextWiki.

@Test
public void relativeTemplateContextWiki() throws Exception {
    // Setup the user profile data to extract.
    setUpUserObject();
    // Setup the template
    String templateStringReference = "Some.Template";
    parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
    DocumentReference userWikiTemplateReference = new DocumentReference("wiki", "Some", "Template");
    when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, testSubscriberReference.getWikiReference())).thenReturn(userWikiTemplateReference);
    when(mockWiki.exists(userWikiTemplateReference, mockContext)).thenReturn(false);
    String currentWikiId = "xwiki";
    when(mockContext.getWikiId()).thenReturn(currentWikiId);
    WikiReference currentWikiReference = new WikiReference(currentWikiId);
    DocumentReference currentWikiTemplateReference = new DocumentReference(currentWikiId, "Some", "Template");
    when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, currentWikiReference)).thenReturn(currentWikiTemplateReference);
    when(mockWiki.exists(currentWikiTemplateReference, mockContext)).thenReturn(true);
    testTemplateReference = currentWikiTemplateReference;
    // Run the extraction.
    WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
    // Check the extracted data.
    assertResult(result);
}
Also used : WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 70 with WikiReference

use of org.xwiki.model.reference.WikiReference in project xwiki-platform by xwiki.

the class TemporaryChartImageWriterTest method getStorageLocation.

@Test
public void getStorageLocation() throws Exception {
    WikiReference currentWikiReference = new WikiReference("wiki");
    ModelContext modelContext = this.componentManager.getInstance(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
    Environment environment = this.componentManager.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File("/tmpdir"));
    File location = this.componentManager.getComponentUnderTest().getStorageLocation(new ImageId(new ChartMacroParameters()));
    Assert.assertTrue("Got: " + location.toString(), location.toString().matches("/tmpdir/temp/chart/wiki/space/page/.*\\.png"));
}
Also used : ModelContext(org.xwiki.model.ModelContext) ChartMacroParameters(org.xwiki.rendering.macro.chart.ChartMacroParameters) Environment(org.xwiki.environment.Environment) WikiReference(org.xwiki.model.reference.WikiReference) File(java.io.File) Test(org.junit.Test)

Aggregations

WikiReference (org.xwiki.model.reference.WikiReference)220 DocumentReference (org.xwiki.model.reference.DocumentReference)127 Test (org.junit.Test)106 SpaceReference (org.xwiki.model.reference.SpaceReference)58 XWikiContext (com.xpn.xwiki.XWikiContext)33 XWikiException (com.xpn.xwiki.XWikiException)24 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)23 ArrayList (java.util.ArrayList)19 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)18 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)11 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)10 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)10 XWiki (com.xpn.xwiki.XWiki)9 BaseObject (com.xpn.xwiki.objects.BaseObject)9 ComponentManager (org.xwiki.component.manager.ComponentManager)9 Expectations (org.jmock.Expectations)8 Before (org.junit.Before)8 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)8