Search in sources :

Example 1 with XWikiRightService

use of com.xpn.xwiki.user.api.XWikiRightService in project xwiki-platform by xwiki.

the class ImportTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.pack = new Package();
    this.xwiki = new XWiki();
    getContext().setWiki(this.xwiki);
    this.xwiki.setConfig(new XWikiConfig());
    Mock mockLocalizationContext = registerMockComponent(LocalizationContext.class);
    mockLocalizationContext.stubs().method("getCurrentLocale").will(returnValue(Locale.ROOT));
    // mock a store that would also handle translations
    this.mockXWikiStore = mock(XWikiHibernateStore.class, new Class[] { XWiki.class, XWikiContext.class }, new Object[] { this.xwiki, getContext() });
    this.mockXWikiStore.stubs().method("loadXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
            String documentKey = shallowDoc.getFullName();
            if (!shallowDoc.getLanguage().equals("")) {
                documentKey += "." + shallowDoc.getLanguage();
            }
            if (docs.containsKey(documentKey)) {
                return docs.get(documentKey);
            } else {
                return shallowDoc;
            }
        }
    });
    this.mockXWikiStore.stubs().method("saveXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
            document.setNew(false);
            document.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
            // if this is a translated document, append a language prefix
            String documentKey = document.getFullName();
            if (!document.getLanguage().equals("")) {
                documentKey += "." + document.getLanguage();
            }
            docs.put(documentKey, document);
            return null;
        }
    });
    this.mockXWikiStore.stubs().method("deleteXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.deleteXWikiDoc") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
            // delete the document from the map
            String documentKey = document.getFullName();
            if (!document.getLanguage().equals("")) {
                documentKey += "." + document.getLanguage();
            }
            docs.remove(documentKey);
            return null;
        }
    });
    this.mockXWikiStore.stubs().method("getTranslationList").will(new CustomStub("Implements XWikiStoreInterface.getTranslationList") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
            // search for this document in the map and return it's translations
            List translationList = new ArrayList();
            for (Iterator pairsIt = docs.entrySet().iterator(); pairsIt.hasNext(); ) {
                Map.Entry currentEntry = (Map.Entry) pairsIt.next();
                if (((String) currentEntry.getKey()).startsWith(document.getFullName()) && !((XWikiDocument) currentEntry.getValue()).getLanguage().equals("")) {
                    // yeeey, it's a translation
                    translationList.add(((XWikiDocument) currentEntry.getValue()).getLanguage());
                }
            }
            return translationList;
        }
    });
    this.mockXWikiStore.stubs().method("injectCustomMapping").will(returnValue(false));
    this.mockRecycleBinStore = mock(XWikiHibernateRecycleBinStore.class, new Class[] { XWikiContext.class }, new Object[] { getContext() });
    this.mockRecycleBinStore.stubs().method("saveToRecycleBin").will(VoidStub.INSTANCE);
    this.mockXWikiVersioningStore = mock(XWikiHibernateVersioningStore.class, new Class[] { XWiki.class, XWikiContext.class }, new Object[] { this.xwiki, getContext() });
    this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
    this.mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null));
    this.xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
    this.xwiki.setRecycleBinStore((XWikiHibernateRecycleBinStore) this.mockRecycleBinStore.proxy());
    this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore.proxy());
    // mock the right service
    this.mockRightService = mock(XWikiRightService.class);
    this.mockRightService.stubs().method("checkAccess").will(returnValue(true));
    this.mockRightService.stubs().method("hasWikiAdminRights").will(returnValue(true));
    this.mockRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
    this.xwiki.setRightService((XWikiRightService) this.mockRightService.proxy());
}
Also used : Invocation(org.jmock.core.Invocation) XWikiHibernateVersioningStore(com.xpn.xwiki.store.XWikiHibernateVersioningStore) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) Mock(org.jmock.Mock) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) CustomStub(org.jmock.core.stub.CustomStub) Iterator(java.util.Iterator) XWikiHibernateRecycleBinStore(com.xpn.xwiki.store.XWikiHibernateRecycleBinStore) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) XWikiConfig(com.xpn.xwiki.XWikiConfig)

Example 2 with XWikiRightService

use of com.xpn.xwiki.user.api.XWikiRightService in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(new CustomAction("WikiDescriptorManager#getCurrentWikiId") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return getContext().getWikiId();
                }
            });
        }
    });
    final ContextualAuthorizationManager mockCam = getContextualAuthorizationManager();
    final XWiki mockXWiki = getMockery().mock(XWiki.class);
    final XWikiGroupService mockXWikiGroupService = getMockery().mock(XWikiGroupService.class);
    getContext().setWiki(mockXWiki);
    this.xwiki20Parser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
    this.wikiMacroDocumentReference = new DocumentReference(getContext().getWikiId(), "space", "macroPage");
    this.wikiMacroManager = getComponentManager().getInstance(WikiMacroManager.class);
    this.wikiMacroDocument = new XWikiDocument(wikiMacroDocumentReference);
    final XWikiRightService rightService = new XWikiRightServiceImpl();
    this.user = new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "user"));
    this.user.setNew(false);
    BaseObject userObject = new BaseObject();
    userObject.setXClassReference(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiusers"));
    this.user.addXObject(userObject);
    this.wikiMacroDocument.setCreatorReference(this.user.getAuthorReference());
    this.wikiMacroDocument.setAuthorReference(this.user.getAuthorReference());
    this.wikiMacroDocument.setContentAuthorReference(this.user.getAuthorReference());
    // Setup an XWikiPreferences document granting programming rights to user
    final XWikiDocument prefs = new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreferences"));
    final BaseObject mockGlobalRightObj = getMockery().mock(BaseObject.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockCam).hasAccess(Right.PROGRAM);
            will(returnValue(true));
            allowing(mockXWiki).getDocument(with(equal(wikiMacroDocumentReference)), with(any(XWikiContext.class)));
            will(returnValue(wikiMacroDocument));
            allowing(mockXWiki).isReadOnly();
            will(returnValue(false));
            allowing(mockXWiki).getLanguagePreference(with(any(XWikiContext.class)));
            will(returnValue(null));
            allowing(mockXWiki).getRightService();
            will(returnValue(rightService));
            allowing(mockXWiki).getGroupService(with(any(XWikiContext.class)));
            will(returnValue(mockXWikiGroupService));
            allowing(mockXWikiGroupService).getAllGroupsReferencesForMember(with(any(DocumentReference.class)), with(any(int.class)), with(any(int.class)), with(any(XWikiContext.class)));
            will(returnValue(Collections.EMPTY_LIST));
            allowing(mockXWiki).getDocument(with(equal(XWIKIPREFERENCES_REFERENCE)), with(any(XWikiContext.class)));
            will(returnValue(prefs));
            allowing(mockGlobalRightObj).getStringValue("levels");
            will(returnValue("programming"));
            allowing(mockGlobalRightObj).getStringValue("users");
            will(returnValue(user.getFullName()));
            allowing(mockGlobalRightObj).getIntValue("allow");
            will(returnValue(1));
            allowing(mockGlobalRightObj).setNumber(with(any(int.class)));
            allowing(mockGlobalRightObj).setDocumentReference(with(any(DocumentReference.class)));
            allowing(mockGlobalRightObj).setOwnerDocument(with(any(XWikiDocument.class)));
        }
    });
    prefs.addObject("XWiki.XWikiGlobalRights", mockGlobalRightObj);
    getContext().setUserReference(this.user.getDocumentReference());
}
Also used : Expectations(org.jmock.Expectations) ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) WikiMacroManager(org.xwiki.rendering.macro.wikibridge.WikiMacroManager) Parser(org.xwiki.rendering.parser.Parser) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) XWikiRightServiceImpl(com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService) Before(org.junit.Before)

Example 3 with XWikiRightService

use of com.xpn.xwiki.user.api.XWikiRightService in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method canRestoreDeletedDocument.

@Test
public void canRestoreDeletedDocument() throws Exception {
    long deletedDocumentId = 42;
    String deletedDocumentFullName = "Space.DeletedDocument";
    DocumentReference userReferenceToCheck = new DocumentReference("wiki", "Space", "User");
    DocumentReference currentUserReference = new DocumentReference("wiki", "Space", "CurrentUser");
    when(xcontext.getUserReference()).thenReturn(currentUserReference);
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getFullName()).thenReturn(deletedDocumentFullName);
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getDeletedDocument(deletedDocumentId, xcontext, true)).thenReturn(deletedDocument);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    XWikiRightService rightService = mock(XWikiRightService.class);
    when(xwiki.getRightService()).thenReturn(rightService);
    when(rightService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
    assertTrue(mocker.getComponentUnderTest().canRestoreDeletedDocument(deletedDocumentId, userReferenceToCheck));
    // Verify that the rights were checked with the specified user as context user.
    verify(xcontext).setUserReference(userReferenceToCheck);
    // Verify that the context user was restored. Note: We don`t know the order here, but maybe we don`t care that
    // much.
    verify(xcontext).setUserReference(currentUserReference);
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with XWikiRightService

use of com.xpn.xwiki.user.api.XWikiRightService in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method restoreDeletedDocumentNoRights.

@Test
public void restoreDeletedDocumentNoRights() throws Exception {
    long deletedDocumentId = 42;
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    String deletedDocumentFullName = "space.page";
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getDocumentReference()).thenReturn(documentReference);
    when(deletedDocument.getId()).thenReturn(deletedDocumentId);
    when(deletedDocument.getFullName()).thenReturn(deletedDocumentFullName);
    when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument);
    when(xwiki.exists(documentReference, xcontext)).thenReturn(false);
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getDeletedDocument(deletedDocumentId, xcontext, true)).thenReturn(deletedDocument);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    // No rights.
    XWikiRightService rightService = mock(XWikiRightService.class);
    when(xwiki.getRightService()).thenReturn(rightService);
    when(rightService.hasAccessLevel(any(), any(), any(), any())).thenReturn(false);
    assertFalse(mocker.getComponentUnderTest().restoreDeletedDocument(deletedDocumentId, true));
    verify(mocker.getMockedLogger()).error("You are not allowed to restore document [{}] with ID [{}]", documentReference, deletedDocumentId);
    verify(xwiki, never()).restoreFromRecycleBin(any(), any(), any());
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with XWikiRightService

use of com.xpn.xwiki.user.api.XWikiRightService in project xwiki-platform by xwiki.

the class XWiki method getRightService.

public XWikiRightService getRightService() {
    synchronized (this.RIGHT_SERVICE_LOCK) {
        if (this.rightService == null) {
            LOGGER.info("Initializing RightService...");
            String rightsClass = getConfiguration().getProperty("xwiki.authentication.rightsclass");
            if (rightsClass != null && !rightsClass.equals(DEFAULT_RIGHT_SERVICE_CLASS)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.warn("Using custom Right Service [{}].", rightsClass);
                }
            } else {
                rightsClass = DEFAULT_RIGHT_SERVICE_CLASS;
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Using default Right Service [{}].", rightsClass);
                }
            }
            try {
                this.rightService = (XWikiRightService) Class.forName(rightsClass).newInstance();
                LOGGER.debug("Initialized RightService using Reflection.");
            } catch (Exception e) {
                Exception lastException = e;
                if (!rightsClass.equals(DEFAULT_RIGHT_SERVICE_CLASS)) {
                    LOGGER.warn(String.format("Failed to initialize custom RightService [%s]" + " by Reflection, using default implementation [%s].", rightsClass, DEFAULT_RIGHT_SERVICE_CLASS), e);
                    rightsClass = DEFAULT_RIGHT_SERVICE_CLASS;
                    try {
                        this.rightService = (XWikiRightService) Class.forName(rightsClass).newInstance();
                        LOGGER.debug("Initialized default RightService using Reflection.");
                    } catch (Exception e1) {
                        lastException = e1;
                    }
                }
                if (this.rightService == null) {
                    LOGGER.warn(String.format("Failed to initialize RightService [%s]" + " by Reflection, using OLD implementation [%s] with 'new'.", rightsClass, XWikiRightServiceImpl.class.getCanonicalName()), lastException);
                    this.rightService = new XWikiRightServiceImpl();
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Initialized old RightService implementation " + this.rightService.getClass().getName() + " using 'new'.");
                    }
                }
            }
        }
        return this.rightService;
    }
}
Also used : XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) XWikiRightServiceImpl(com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException)

Aggregations

XWikiRightService (com.xpn.xwiki.user.api.XWikiRightService)5 DocumentReference (org.xwiki.model.reference.DocumentReference)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDeletedDocument (com.xpn.xwiki.doc.XWikiDeletedDocument)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 XWikiRecycleBinStoreInterface (com.xpn.xwiki.store.XWikiRecycleBinStoreInterface)2 XWikiRightServiceImpl (com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 XWikiConfig (com.xpn.xwiki.XWikiConfig)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 XWikiHibernateRecycleBinStore (com.xpn.xwiki.store.XWikiHibernateRecycleBinStore)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiHibernateVersioningStore (com.xpn.xwiki.store.XWikiHibernateVersioningStore)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiGroupService (com.xpn.xwiki.user.api.XWikiGroupService)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 FileNotFoundException (java.io.FileNotFoundException)1