Search in sources :

Example 1 with ViewDTO

use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.

the class ViewFacade method decode.

protected NativeEntity<ViewDTO> decode(EntityV1 entityV1, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities, User user) {
    final ViewEntity viewEntity = objectMapper.convertValue(entityV1.data(), ViewEntity.class);
    final Map<String, ViewStateDTO> viewStateMap = new LinkedHashMap<>(viewEntity.state().size());
    for (Map.Entry<String, ViewStateEntity> entry : viewEntity.state().entrySet()) {
        final ViewStateEntity entity = entry.getValue();
        viewStateMap.put(entry.getKey(), entity.toNativeEntity(parameters, nativeEntities));
    }
    final ViewDTO.Builder viewBuilder = viewEntity.toNativeEntity(parameters, nativeEntities);
    viewBuilder.state(viewStateMap);
    final Search search = viewEntity.search().toNativeEntity(parameters, nativeEntities);
    final Search persistedSearch = searchDbService.save(search);
    final ViewDTO persistedView = viewService.saveWithOwner(viewBuilder.searchId(persistedSearch.id()).build(), user);
    return NativeEntity.create(entityV1.id(), persistedView.id(), getModelType(), persistedView.title(), persistedView);
}
Also used : ViewStateEntity(org.graylog2.contentpacks.model.entities.ViewStateEntity) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) ViewStateDTO(org.graylog.plugins.views.search.views.ViewStateDTO) ViewEntity(org.graylog2.contentpacks.model.entities.ViewEntity) Search(org.graylog.plugins.views.search.Search) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ViewDTO

use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.

the class ViewSharingToGrantsMigration method getTarget.

private GRN getTarget(String viewId) {
    final ViewDTO view = viewService.get(viewId).orElseThrow(() -> new IllegalArgumentException("View <" + viewId + "> doesn't exist"));
    final GRNType grnType = ViewDTO.Type.DASHBOARD.equals(view.type()) ? GRNTypes.DASHBOARD : GRNTypes.SEARCH;
    return grnType.toGRN(viewId);
}
Also used : ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) GRNType(org.graylog.grn.GRNType)

Example 3 with ViewDTO

use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.

the class SearchDomainTest method includesSearchesPermittedViaViewsInList.

@Test
public void includesSearchesPermittedViaViewsInList() {
    final Search permittedSearch = mockSearchWithOwner("someone else");
    mockSearchWithOwner("someone else");
    final SearchUser searchUser = mock(SearchUser.class);
    final ViewDTO viewDTO = mock(ViewDTO.class);
    when(viewService.forSearch(permittedSearch.id())).thenReturn(ImmutableList.of(viewDTO));
    when(searchUser.canReadView(viewDTO)).thenReturn(true);
    List<Search> result = sut.getAllForUser(searchUser, searchUser::canReadView);
    assertThat(result).containsExactly(permittedSearch);
}
Also used : ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) Test(org.junit.Test)

Example 4 with ViewDTO

use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.

the class SearchDomainTest method loadsSearchIfSearchIsPermittedViaViews.

@Test
public void loadsSearchIfSearchIsPermittedViaViews() {
    final Search search = mockSearchWithOwner("someone else");
    final SearchUser searchUser = mock(SearchUser.class);
    final ViewDTO viewDTO = mock(ViewDTO.class);
    when(viewService.forSearch(anyString())).thenReturn(ImmutableList.of(viewDTO));
    when(searchUser.canReadView(viewDTO)).thenReturn(true);
    final Optional<Search> result = sut.getForUser(search.id(), searchUser);
    assertThat(result).isEqualTo(Optional.of(search));
}
Also used : ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) Test(org.junit.Test)

Example 5 with ViewDTO

use of org.graylog.plugins.views.search.views.ViewDTO in project graylog2-server by Graylog2.

the class ViewsResource method views.

@GET
@ApiOperation("Get a list of all views")
public PaginatedResponse<ViewDTO> views(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "id,title,created_at") @DefaultValue(ViewDTO.FIELD_TITLE) @QueryParam("sort") String sortField, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query, @Context SearchUser searchUser) {
    if (!ViewDTO.SORT_FIELDS.contains(sortField.toLowerCase(ENGLISH))) {
        sortField = ViewDTO.FIELD_TITLE;
    }
    try {
        final SearchQuery searchQuery = searchQueryParser.parse(query);
        final PaginatedList<ViewDTO> result = dbService.searchPaginated(searchQuery, searchUser::canReadView, order, sortField, page, perPage);
        return PaginatedResponse.create("views", result, query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) BadRequestException(javax.ws.rs.BadRequestException) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ViewDTO (org.graylog.plugins.views.search.views.ViewDTO)15 Test (org.junit.Test)5 ApiOperation (io.swagger.annotations.ApiOperation)4 NotFoundException (javax.ws.rs.NotFoundException)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 ForbiddenException (javax.ws.rs.ForbiddenException)3 Path (javax.ws.rs.Path)3 Search (org.graylog.plugins.views.search.Search)3 StreamFilter (org.graylog.plugins.views.search.filter.StreamFilter)3 SearchUser (org.graylog.plugins.views.search.permissions.SearchUser)3 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 Entity (org.graylog2.contentpacks.model.entities.Entity)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)3 PivotEntity (org.graylog2.contentpacks.model.entities.PivotEntity)3 ViewEntity (org.graylog2.contentpacks.model.entities.ViewEntity)3 ViewStateEntity (org.graylog2.contentpacks.model.entities.ViewStateEntity)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BadRequestException (javax.ws.rs.BadRequestException)2