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());
}
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());
}
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);
}
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());
}
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;
}
}
Aggregations