Search in sources :

Example 26 with WikiReference

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

the class WikiManagerScriptServiceTest method saveDescriptorWhenDescriptorDidNotExistAndIAmGlobalAdmin.

@Test
public void saveDescriptorWhenDescriptorDidNotExistAndIAmGlobalAdmin() throws Exception {
    when(this.authorizationManager.hasAccess(Right.ADMIN, currentUserRef, new WikiReference("mainWiki"))).thenReturn(true);
    WikiDescriptor descriptor = new WikiDescriptor("wikiId", "wikiAlias");
    boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
    assertTrue(result);
    // Verify the rights have been checked
    verify(authorizationManager).hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("mainWiki")));
    // The descriptor has been saved
    verify(wikiDescriptorManager).saveDescriptor(descriptor);
}
Also used : WikiReference(org.xwiki.model.reference.WikiReference) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) Test(org.junit.Test)

Example 27 with WikiReference

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

the class XWikiContextInitializationFilter method initializeXWikiContext.

/**
 * Initializes the XWiki context.
 *
 * @param request the request being processed
 * @param response the response
 * @throws ServletException if the initialization fails
 */
protected void initializeXWikiContext(ServletRequest request, ServletResponse response) throws ServletException {
    try {
        // Not all request types specify an action (e.g. GWT-RPC) so we default to the empty string.
        String action = "";
        XWikiServletContext xwikiEngine = new XWikiServletContext(this.filterConfig.getServletContext());
        XWikiServletRequest xwikiRequest = new XWikiServletRequest((HttpServletRequest) request);
        XWikiServletResponse xwikiResponse = new XWikiServletResponse((HttpServletResponse) response);
        // Create the XWiki context.
        XWikiContext context = Utils.prepareContext(action, xwikiRequest, xwikiResponse, xwikiEngine);
        // parameter is specified.
        if (this.mode >= 0) {
            context.setMode(this.mode);
        }
        // Initialize the Container component which is the new way of transporting the Context in the new component
        // architecture. Further initialization might require the Container component.
        initializeContainerComponent(context);
        // Initialize the XWiki database. XWiki#getXWiki(XWikiContext) calls XWikiContext.setWiki(XWiki).
        XWiki xwiki = XWiki.getXWiki(context);
        // Initialize the URL factory.
        context.setURLFactory(xwiki.getURLFactoryService().createURLFactory(context.getMode(), context));
        // Prepare the localized resources, according to the selected language.
        xwiki.prepareResources(context);
        // Initialize the current user.
        XWikiUser user = context.getWiki().checkAuth(context);
        if (user != null) {
            DocumentReferenceResolver<String> documentReferenceResolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
            SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(context.getWikiId()));
            DocumentReference userReference = documentReferenceResolver.resolve(user.getUser(), defaultUserSpace);
            context.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
        }
    } catch (XWikiException e) {
        throw new ServletException("Failed to initialize the XWiki context.", e);
    }
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) SpaceReference(org.xwiki.model.reference.SpaceReference) XWikiServletContext(com.xpn.xwiki.web.XWikiServletContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ServletException(javax.servlet.ServletException) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 28 with WikiReference

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

the class ContextComponentManagerTest method testRegisterComponentInWikiComponentManager.

@Test
public void testRegisterComponentInWikiComponentManager() throws Exception {
    final States state = getMockery().states("test");
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            when(state.isNot("otherwiki"));
            will(returnValue("wiki1"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            when(state.isNot("otherwiki"));
            will(returnValue(new SpaceReference("space1", new WikiReference("wiki1"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            when(state.isNot("otherwiki"));
            will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            when(state.isNot("otherwiki"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    // Register in the current wiki.
    ComponentManager wikiCM = getComponentManager().getInstance(ComponentManager.class, "wiki");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    wikiCM.registerComponent(cd);
    // Verify we can lookup the component from the context CM.
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
    // Now verify that we cannot look it up anymore if there's another wiki in the context
    state.become("otherwiki");
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki2"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
        }
    });
    try {
        contextCM.getInstance(Role.class);
        Assert.fail("Should have raised an exception");
    } catch (ComponentLookupException expected) {
    // No need to assert the message, we just want to ensure an exception is raised.
    }
}
Also used : States(org.jmock.States) Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 29 with WikiReference

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

the class ContextComponentManagerTest method testRegisterComponentInDocumentComponentManager.

@Test
public void testRegisterComponentInDocumentComponentManager() throws Exception {
    final States state = getMockery().states("test");
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            when(state.isNot("otherdocument"));
            will(returnValue("wiki1"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            when(state.isNot("otherdocument"));
            will(returnValue(new SpaceReference("space1", new WikiReference("wiki"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            when(state.isNot("otherdocument"));
            will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            when(state.isNot("otherdocument"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    ComponentManager documentCM = getComponentManager().getInstance(ComponentManager.class, "document");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    // Register component for the current user
    documentCM.registerComponent(cd);
    // Verify we can lookup the component from the Context CM
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getInstance(Role.class));
    // Now verify that we cannot look it up anymore if there's another user in the context
    state.become("otherdocument");
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki2"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
        }
    });
    try {
        contextCM.getInstance(Role.class);
        Assert.fail("Should have raised an exception");
    } catch (ComponentLookupException expected) {
    // No need to assert the message, we just want to ensure an exception is raised.
    }
}
Also used : States(org.jmock.States) Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 30 with WikiReference

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

the class ContextComponentManagerTest method testDeleteWiki.

@Test
public void testDeleteWiki() throws ComponentLookupException, Exception {
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki", "space", "document")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    // Register in the current wiki.
    ComponentManager wikiCM = getComponentManager().getInstance(ComponentManager.class, "wiki");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    wikiCM.registerComponent(cd);
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
    ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
    observationManager.notify(new WikiDeletedEvent("wiki"), null, null);
    Assert.assertNull(contextCM.getComponentDescriptor(Role.class, "default"));
}
Also used : Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ObservationManager(org.xwiki.observation.ObservationManager) WikiReference(org.xwiki.model.reference.WikiReference) WikiDeletedEvent(org.xwiki.bridge.event.WikiDeletedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) 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