Search in sources :

Example 41 with WikiReference

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

the class AbstractUsersAndGroupsClassPropertyValuesProvider method getValueFromQueryResult.

@Override
protected PropertyValue getValueFromQueryResult(Object result, T propertyDefinition) {
    PropertyValue value = super.getValueFromQueryResult(result, propertyDefinition);
    if (value != null && value.getValue() instanceof DocumentReference) {
        DocumentReference documentReference = (DocumentReference) value.getValue();
        WikiReference wikiReference = propertyDefinition.getOwnerDocument().getDocumentReference().getWikiReference();
        // Serialize the user/group reference relative to the wiki were the property is defined.
        value.setValue(this.compactSerializer.serialize(documentReference, wikiReference));
        value.getMetaData().put(META_DATA_LABEL, getLabel(documentReference, value.getMetaData().get(META_DATA_LABEL)));
        value.getMetaData().put(META_DATA_ICON, getIcon(documentReference));
        value.getMetaData().put("url", getURL(documentReference));
    }
    return value;
}
Also used : PropertyValue(org.xwiki.rest.model.jaxb.PropertyValue) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 42 with WikiReference

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

the class AbstractDatabaseSearchSource method search.

@Override
public List<SearchResult> search(String partialQueryString, String wikiName, String wikis, boolean hasProgrammingRights, String orderField, String order, boolean distinct, int number, int start, Boolean withPrettyNames, String className, UriInfo uriInfo) throws Exception {
    XWikiContext xwikiContext = this.xcontextProvider.get();
    XWiki xwikiApi = new XWiki(xwikiContext.getWiki(), xwikiContext);
    if (partialQueryString == null || StringUtils.startsWithIgnoreCase(partialQueryString, "select")) {
        return Collections.emptyList();
    }
    String queryString = resolveQuery(distinct, partialQueryString);
    Query query = this.queryManager.createQuery(queryString, this.queryLanguage);
    query.setLimit(number).setOffset(start);
    query.setWiki(wikiName);
    List<Object> queryResult = query.execute();
    WikiReference wikiReference = new WikiReference(wikiName);
    /* Build the result. */
    List<SearchResult> result = new ArrayList<>();
    for (Object object : queryResult) {
        Object[] fields = (Object[]) object;
        String fullName = (String) fields[0];
        String language = (String) fields[3];
        DocumentReference documentReference = this.resolver.resolve(fullName, wikiReference);
        /* Check if the user has the right to see the found document */
        if (this.authorization.hasAccess(Right.VIEW, documentReference)) {
            Document doc = xwikiApi.getDocument(documentReference);
            String title = doc.getDisplayTitle();
            SearchResult searchResult = this.objectFactory.createSearchResult();
            searchResult.setType("page");
            searchResult.setId(doc.getPrefixedFullName());
            searchResult.setPageFullName(doc.getFullName());
            searchResult.setTitle(title);
            searchResult.setWiki(wikiName);
            searchResult.setSpace(doc.getSpace());
            searchResult.setPageName(doc.getName());
            searchResult.setVersion(doc.getVersion());
            searchResult.setAuthor(doc.getAuthor());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(doc.getDate());
            searchResult.setModified(calendar);
            if (withPrettyNames) {
                searchResult.setAuthorName(xwikiApi.getUserName(doc.getAuthor(), false));
            }
            /*
                 * Avoid to return object information if the user is not authenticated. This will prevent crawlers to
                 * retrieve information such as email addresses and passwords from user's profiles.
                 */
            if (StringUtils.isNotEmpty(className) && xwikiContext.getUserReference() != null) {
                XWikiDocument xdocument = xwikiContext.getWiki().getDocument(doc.getDocumentReference(), xwikiContext);
                BaseObject baseObject = xdocument.getObject(className);
                if (baseObject != null) {
                    searchResult.setObject(this.modelFactory.toRestObject(uriInfo.getBaseUri(), doc, baseObject, false, false));
                }
            }
            String pageUri;
            if (StringUtils.isBlank(language)) {
                pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName, Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()), documentReference.getName()).toString();
            } else {
                searchResult.setLanguage(language);
                pageUri = Utils.createURI(uriInfo.getBaseUri(), PageTranslationResource.class, wikiName, Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()), documentReference.getName(), language).toString();
            }
            Link pageLink = new Link();
            pageLink.setHref(pageUri);
            pageLink.setRel(Relations.PAGE);
            searchResult.getLinks().add(pageLink);
            result.add(searchResult);
        }
    }
    return result;
}
Also used : Query(org.xwiki.query.Query) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.api.XWiki) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link)

Example 43 with WikiReference

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

the class DefaultContextualAuthorizationManagerTest method before.

@Before
public void before() throws Exception {
    this.authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
    this.currentWikiReference = new WikiReference("wiki");
    this.oldcore.getXWikiContext().setWikiId(this.currentWikiReference.getName());
}
Also used : AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) Before(org.junit.Before)

Example 44 with WikiReference

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

the class CurrentUserAndGroupDocumentReferenceResolverTest method testResolver.

@Test
public void testResolver() throws ComponentLookupException {
    assertEquals(new DocumentReference("currentwiki", "XWiki", "Bosse"), this.mocker.getComponentUnderTest().resolve("Bosse"));
    assertEquals(new DocumentReference("currentwiki", "bossesSpace", "Bosse"), this.mocker.getComponentUnderTest().resolve("bossesSpace.Bosse"));
    assertEquals(new DocumentReference("bossesWiki", "XWiki", "Bosse"), this.mocker.getComponentUnderTest().resolve("Bosse", new WikiReference("bossesWiki")));
    assertEquals(new DocumentReference("bossesWiki", "bossesSpace", "Bosse"), this.mocker.getComponentUnderTest().resolve("bossesSpace.Bosse", new WikiReference("bossesWiki")));
    assertEquals(new DocumentReference("bossesWiki", "bossesSpace", "Bosse"), this.mocker.getComponentUnderTest().resolve("bossesWiki:bossesSpace.Bosse"));
    // If null is passed we expect no reference (i.e. the guest user).
    assertNull(this.mocker.getComponentUnderTest().resolve(null));
}
Also used : WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 45 with WikiReference

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

the class DefaultSecurityEntryReader method read.

/**
 * Load the rules from wiki documents.
 *
 * @param entity Any entity reference that is either a WIKI or a SPACE, or an entity containing a DOCUMENT entity.
 * @return the access rules that could be loaded into the cache.
 * @throws org.xwiki.security.authorization.AuthorizationException if an issue arise while reading these rules
 *         from the wiki.
 */
@Override
public SecurityRuleEntry read(SecurityReference entity) throws AuthorizationException {
    if (entity == null) {
        return null;
    }
    if (entity.getOriginalReference() == null) {
        // More generally, any reference without a valid original reference should not be considered.
        return new InternalSecurityRuleEntry(entity, Collections.<SecurityRule>emptyList());
    }
    DocumentReference documentReference;
    DocumentReference classReference;
    WikiReference wikiReference;
    switch(entity.getType()) {
        case WIKI:
            wikiReference = new WikiReference(entity);
            SpaceReference wikiSpace = new SpaceReference(XWikiConstants.XWIKI_SPACE, wikiReference);
            documentReference = new DocumentReference(XWikiConstants.WIKI_DOC, wikiSpace);
            classReference = new DocumentReference(XWikiConstants.GLOBAL_CLASSNAME, wikiSpace);
            break;
        case SPACE:
            wikiReference = new WikiReference(entity.extractReference(EntityType.WIKI));
            documentReference = new DocumentReference(XWikiConstants.SPACE_DOC, new SpaceReference(entity));
            classReference = new DocumentReference(XWikiConstants.GLOBAL_CLASSNAME, new SpaceReference(XWikiConstants.XWIKI_SPACE, wikiReference));
            break;
        case DOCUMENT:
            wikiReference = new WikiReference(entity.extractReference(EntityType.WIKI));
            documentReference = new DocumentReference(entity);
            classReference = new DocumentReference(XWikiConstants.LOCAL_CLASSNAME, new SpaceReference(XWikiConstants.XWIKI_SPACE, wikiReference));
            break;
        default:
            throw new EntityTypeNotSupportedException(entity.getType(), this);
    }
    return new InternalSecurityRuleEntry(entity, getSecurityRules(documentReference, classReference, wikiReference));
}
Also used : EntityTypeNotSupportedException(org.xwiki.security.authorization.EntityTypeNotSupportedException) SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

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