Search in sources :

Example 6 with AuthorizationManager

use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.

the class SolrQueryExecutorTest method filterResponse.

@Test
public void filterResponse() throws Exception {
    ParameterizedType resolverType = new DefaultParameterizedType(null, DocumentReferenceResolver.class, SolrDocument.class);
    DocumentReferenceResolver<SolrDocument> resolver = this.componentManager.getInstance(resolverType);
    AuthorizationManager authorizationManager = this.componentManager.getInstance(AuthorizationManager.class);
    DocumentReference currentUserReference = new DocumentReference("xwiki", "XWiki", "currentuser");
    this.oldCore.getXWikiContext().setUserReference(currentUserReference);
    DocumentReference currentAuthorReference = new DocumentReference("xwiki", "XWiki", "currentauthor");
    XWikiDocument currentDocument = new XWikiDocument(currentAuthorReference);
    currentDocument.setContentAuthorReference(currentAuthorReference);
    this.oldCore.getXWikiContext().setDoc(currentDocument);
    DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
    when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, aliceReference)).thenReturn(true);
    SolrDocument alice = new SolrDocument();
    when(resolver.resolve(alice)).thenReturn(aliceReference);
    DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
    when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, bobReference)).thenReturn(true);
    when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, bobReference)).thenReturn(true);
    SolrDocument bob = new SolrDocument();
    when(resolver.resolve(bob)).thenReturn(bobReference);
    DocumentReference carolReference = new DocumentReference("wiki", "Users", "Carol");
    when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, carolReference)).thenReturn(true);
    SolrDocument carol = new SolrDocument();
    when(resolver.resolve(carol)).thenReturn(carolReference);
    SolrDocumentList sourceResults = new SolrDocumentList();
    sourceResults.addAll(Arrays.asList(alice, bob, carol));
    sourceResults.setNumFound(3);
    QueryResponse response = mock(QueryResponse.class);
    when(this.solr.query(any(SolrParams.class))).thenReturn(response);
    DefaultQuery query = new DefaultQuery("", null);
    // No right check
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    SolrDocumentList results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(alice, bob, carol), results);
    // Check current user right
    query.checkCurrentUser(true);
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(bob, carol), results);
    // Check both current user and author rights
    query.checkCurrentAuthor(true);
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(bob), results);
    // Check current author right
    query.checkCurrentUser(false);
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(alice, bob), results);
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SolrDocument(org.apache.solr.common.SolrDocument) DefaultQuery(org.xwiki.query.internal.DefaultQuery) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrParams(org.apache.solr.common.params.SolrParams) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with AuthorizationManager

use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
    ModelContext modelContext = componentManager.registerMockComponent(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
    // Document Access Bridge mock
    DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    DocumentReference currentDocumentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
    DocumentModelBridge document = Mockito.mock(DocumentModelBridge.class);
    when(dab.getDocumentURL(new DocumentReference("currentWiki", "space", "page"), "temp", null, null)).thenReturn("temppath");
    when(dab.getCurrentDocumentReference()).thenReturn(currentDocumentReference);
    when(dab.exists(documentReference)).thenReturn(true);
    when(dab.getDocumentInstance(documentReference)).thenReturn(document);
    when(dab.getCurrentUserReference()).thenReturn(null);
    DocumentDisplayer displayer = componentManager.registerMockComponent(DocumentDisplayer.class);
    Parser parser = componentManager.getInstance(Parser.class, Syntax.XWIKI_2_0.toIdString());
    final XDOM xdom = parser.parse(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(WIKI_CONTENT_FILE)));
    when(displayer.display(eq(document), any(DocumentDisplayerParameters.class))).thenReturn(xdom);
    AuthorizationManager authorizationManager = componentManager.registerMockComponent(AuthorizationManager.class);
    when(authorizationManager.hasAccess(Right.VIEW, null, documentReference)).thenReturn(true);
    componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
    componentManager.registerMockComponent(SpaceReferenceResolver.TYPE_STRING, "current");
}
Also used : ModelContext(org.xwiki.model.ModelContext) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InputStreamReader(java.io.InputStreamReader) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Parser(org.xwiki.rendering.parser.Parser)

Example 8 with AuthorizationManager

use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.

the class DocumentTitleDisplayerTest method whenSettingTheContextDocumentTheContextWikiIsAlsoSet.

@Test
public void whenSettingTheContextDocumentTheContextWikiIsAlsoSet() throws Exception {
    EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
    when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("Page", EntityType.DOCUMENT));
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space"), "Page");
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(document.getTitle()).thenReturn("title");
    XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("title")));
    Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
    when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
    ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
    WikiReference currentWikiReference = new WikiReference("currentWiki");
    when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
    AuthorizationManager authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
    when(authorizationManager.hasAccess(eq(Right.SCRIPT), any(), any())).thenReturn(true);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentDisplayerParameters params = new DocumentDisplayerParameters();
    params.setTitleDisplayed(true);
    params.setExecutionContextIsolated(true);
    this.mocker.getComponentUnderTest().display(document, params);
    // Check that the context is set.
    verify(dab).pushDocumentInContext(any(), eq(documentReference));
    verify(modelContext).setCurrentEntityReference(documentReference.getWikiReference());
    // Check that the context is restored.
    verify(dab).popDocumentFromContext(any());
    verify(modelContext).setCurrentEntityReference(currentWikiReference);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) WordBlock(org.xwiki.rendering.block.WordBlock) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Parser(org.xwiki.rendering.parser.Parser) ModelContext(org.xwiki.model.ModelContext) EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReference(org.xwiki.model.reference.EntityReference) StringReader(java.io.StringReader) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with AuthorizationManager

use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method setUp.

@Before
public void setUp() throws Exception {
    eventStream = mocker.getInstance(EventStream.class);
    queryGenerator = mocker.getInstance(QueryGenerator.class);
    documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    documentReferenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
    notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
    authorizationManager = mocker.getInstance(AuthorizationManager.class);
    startDate = new Date(10);
    when(documentReferenceResolver.resolve("xwiki:XWiki.UserA")).thenReturn(userReference);
    query = mock(Query.class);
    when(queryGenerator.generateQuery(any(DocumentReference.class), any(NotificationFormat.class), nullable(Date.class), nullable(Date.class), nullable(List.class))).thenReturn(query);
    NotificationPreference pref1 = mock(NotificationPreference.class);
    when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
    when(pref1.isNotificationEnabled()).thenReturn(true);
    when(notificationPreferenceManager.getAllPreferences(userReference)).thenReturn(Arrays.asList(pref1));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) Query(org.xwiki.query.Query) EventStream(org.xwiki.eventstream.EventStream) NotificationFormat(org.xwiki.notifications.NotificationFormat) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ComponentList(org.xwiki.test.annotation.ComponentList) List(java.util.List) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 10 with AuthorizationManager

use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.

the class WikiTemplateManagerScriptTest method setUp.

@Before
public void setUp() throws Exception {
    wikiTemplateManager = mocker.getInstance(WikiTemplateManager.class);
    wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
    authorizationManager = mocker.getInstance(AuthorizationManager.class);
    entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceSerializer.class, String.class));
    xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    xcontext = mock(XWikiContext.class);
    when(xcontextProvider.get()).thenReturn(xcontext);
    execution = mocker.getInstance(Execution.class);
    executionContext = new ExecutionContext();
    when(execution.getContext()).thenReturn(executionContext);
    currentUserRef = new DocumentReference("mainWiki", "XWiki", "User");
    when(xcontext.getUserReference()).thenReturn(currentUserRef);
    currentDoc = mock(XWikiDocument.class);
    when(xcontext.getDoc()).thenReturn(currentDoc);
    when(xcontext.getMainXWiki()).thenReturn("mainWiki");
    when(entityReferenceSerializer.serialize(currentUserRef)).thenReturn("mainWiki:XWiki.User");
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWikiContext(com.xpn.xwiki.XWikiContext) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiTemplateManager(org.xwiki.wiki.template.WikiTemplateManager) Before(org.junit.Before)

Aggregations

AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)13 DocumentReference (org.xwiki.model.reference.DocumentReference)10 Before (org.junit.Before)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 Execution (org.xwiki.context.Execution)5 ExecutionContext (org.xwiki.context.ExecutionContext)5 Test (org.junit.Test)4 XWiki (com.xpn.xwiki.XWiki)3 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)3 WikiReference (org.xwiki.model.reference.WikiReference)3 Date (java.util.Date)2 List (java.util.List)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 ModelContext (org.xwiki.model.ModelContext)2 QueryManager (org.xwiki.query.QueryManager)2 XDOM (org.xwiki.rendering.block.XDOM)2 Parser (org.xwiki.rendering.parser.Parser)2