Search in sources :

Example 6 with PaginatedList

use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.

the class AuthzRolesResource method getListForUser.

@GET
@ApiOperation(value = "Get a paginated list roles for a user")
@Path("/user/{username}")
@RequiresPermissions(RestPermissions.ROLES_READ)
public PaginatedResponse<AuthzRoleDTO> getListForUser(@ApiParam(name = "username") @PathParam("username") @NotEmpty String username, @ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "name,description") @DefaultValue(AuthzRoleDTO.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    SearchQuery searchQuery;
    try {
        searchQuery = searchQueryParser.parse(query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final User user = Optional.ofNullable(userService.load(username)).orElseThrow(() -> new NotFoundException("Couldn't find user: " + username));
    final PaginatedList<AuthzRoleDTO> result = authzRolesService.findPaginatedByIds(searchQuery, page, perPage, sort, order, user.getRoleIds());
    return PaginatedResponse.create("roles", result, query);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) User(org.graylog2.plugin.database.users.User) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with PaginatedList

use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.

the class AuthzRolesResource method getUsersForRole.

@GET
@ApiOperation(value = "Get a paginated list of users for a role")
@Path("/{roleId}/assignees")
@Produces(MediaType.APPLICATION_JSON)
@RequiresPermissions(RestPermissions.USERS_LIST)
public PaginatedResponse<UserOverviewDTO> getUsersForRole(@ApiParam(name = "roleId") @PathParam("roleId") @NotEmpty String roleId, @ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "username,full_name,email") @DefaultValue(AuthzRoleDTO.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    SearchQuery searchQuery;
    try {
        searchQuery = userSearchQueryParser.parse(query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final PaginatedList<UserOverviewDTO> result = paginatedUserService.findPaginatedByRole(searchQuery, page, perPage, sort, order, ImmutableSet.of(roleId));
    final Set<String> roleIds = result.stream().flatMap(u -> u.roles().stream()).collect(Collectors.toSet());
    final Map<String, String> rolesMap = authzRolesService.findPaginatedByIds(new SearchQuery(""), 0, 0, AuthzRoleDTO.FIELD_NAME, "asc", roleIds).stream().collect(Collectors.toMap(AuthzRoleDTO::id, AuthzRoleDTO::name));
    final List<UserOverviewDTO> users = result.stream().map(u -> {
        final Set<String> roleNames = u.roles().stream().map(rolesMap::get).collect(Collectors.toSet());
        return u.toBuilder().roles(roleNames).build();
    }).collect(Collectors.toList());
    final PaginatedList<UserOverviewDTO> enrichedResult = new PaginatedList<>(users, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
    return PaginatedResponse.create("users", enrichedResult, query);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) PathParam(javax.ws.rs.PathParam) NotBlank(javax.validation.constraints.NotBlank) Produces(javax.ws.rs.Produces) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) HashMap(java.util.HashMap) NotAllowedException(javax.ws.rs.NotAllowedException) NotEmpty(javax.validation.constraints.NotEmpty) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SearchQueryField(org.graylog2.search.SearchQueryField) USERS_ROLESEDIT(org.graylog2.shared.security.RestPermissions.USERS_ROLESEDIT) Map(java.util.Map) AuditEvent(org.graylog2.audit.jersey.AuditEvent) DefaultValue(javax.ws.rs.DefaultValue) BadRequestException(javax.ws.rs.BadRequestException) Api(io.swagger.annotations.Api) SearchQuery(org.graylog2.search.SearchQuery) DELETE(javax.ws.rs.DELETE) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) RestResource(org.graylog2.shared.rest.resources.RestResource) Collectors(java.util.stream.Collectors) PaginatedUserService(org.graylog2.users.PaginatedUserService) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) UserService(org.graylog2.shared.users.UserService) AuditEventTypes(org.graylog2.audit.AuditEventTypes) ValidationException(org.graylog2.plugin.database.ValidationException) RestPermissions(org.graylog2.shared.security.RestPermissions) Optional(java.util.Optional) PUT(javax.ws.rs.PUT) PaginatedResponse(org.graylog2.rest.models.PaginatedResponse) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) User(org.graylog2.plugin.database.users.User) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) BadRequestException(javax.ws.rs.BadRequestException) PaginatedList(org.graylog2.database.PaginatedList) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 8 with PaginatedList

use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.

the class AuthzRolesResource method userRoleContext.

private Map<String, Set<Map<String, String>>> userRoleContext(PaginatedList<AuthzRoleDTO> roles) {
    final PaginatedList<UserOverviewDTO> users = paginatedUserService.findPaginatedByRole(new SearchQuery(""), 1, 0, UserOverviewDTO.FIELD_USERNAME, "asc", roles.stream().map(AuthzRoleDTO::id).collect(Collectors.toSet()));
    final Map<String, Set<Map<String, String>>> userRoleMap = new HashMap<>(roles.size());
    roles.forEach(authzRoleDTO -> {
        final Set<Map<String, String>> userMap = users.stream().filter(u -> u.roles().contains(authzRoleDTO.id())).map(u -> ImmutableMap.of(UserOverviewDTO.FIELD_ID, Objects.requireNonNull(u.id()), UserOverviewDTO.FIELD_USERNAME, u.username())).collect(Collectors.toSet());
        userRoleMap.put(authzRoleDTO.id(), userMap);
    });
    return userRoleMap;
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) PathParam(javax.ws.rs.PathParam) NotBlank(javax.validation.constraints.NotBlank) Produces(javax.ws.rs.Produces) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) HashMap(java.util.HashMap) NotAllowedException(javax.ws.rs.NotAllowedException) NotEmpty(javax.validation.constraints.NotEmpty) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SearchQueryField(org.graylog2.search.SearchQueryField) USERS_ROLESEDIT(org.graylog2.shared.security.RestPermissions.USERS_ROLESEDIT) Map(java.util.Map) AuditEvent(org.graylog2.audit.jersey.AuditEvent) DefaultValue(javax.ws.rs.DefaultValue) BadRequestException(javax.ws.rs.BadRequestException) Api(io.swagger.annotations.Api) SearchQuery(org.graylog2.search.SearchQuery) DELETE(javax.ws.rs.DELETE) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) RestResource(org.graylog2.shared.rest.resources.RestResource) Collectors(java.util.stream.Collectors) PaginatedUserService(org.graylog2.users.PaginatedUserService) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) UserService(org.graylog2.shared.users.UserService) AuditEventTypes(org.graylog2.audit.AuditEventTypes) ValidationException(org.graylog2.plugin.database.ValidationException) RestPermissions(org.graylog2.shared.security.RestPermissions) Optional(java.util.Optional) PUT(javax.ws.rs.PUT) PaginatedResponse(org.graylog2.rest.models.PaginatedResponse) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) User(org.graylog2.plugin.database.users.User) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 9 with PaginatedList

use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.

the class LookupTableResource method adapters.

@GET
@Path("adapters")
@ApiOperation(value = "List available data adapters")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public DataAdapterPage adapters(@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 = "title,description,name,id") @DefaultValue(DataAdapterDto.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("desc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query) {
    if (!ADAPTER_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
        sort = DataAdapterDto.FIELD_TITLE;
    }
    DBSort.SortBuilder sortBuilder;
    if ("desc".equalsIgnoreCase(order)) {
        sortBuilder = DBSort.desc(sort);
    } else {
        sortBuilder = DBSort.asc(sort);
    }
    try {
        final SearchQuery searchQuery = adapterSearchQueryParser.parse(query);
        final DBQuery.Query dbQuery = searchQuery.toDBQuery();
        PaginatedList<DataAdapterDto> paginated = dbDataAdapterService.findPaginated(dbQuery, sortBuilder, page, perPage);
        return new DataAdapterPage(query, paginated.pagination(), paginated.stream().map(DataAdapterApi::fromDto).collect(Collectors.toList()));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) DBQuery(org.mongojack.DBQuery) BadRequestException(javax.ws.rs.BadRequestException) DataAdapterApi(org.graylog2.rest.models.system.lookup.DataAdapterApi) DBSort(org.mongojack.DBSort) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 10 with PaginatedList

use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.

the class LookupTableResource method caches.

@GET
@Path("caches")
@ApiOperation(value = "List available caches")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public CachesPage caches(@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 = "title,description,name,id") @DefaultValue(CacheDto.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("desc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query) {
    if (!CACHE_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
        sort = CacheDto.FIELD_TITLE;
    }
    DBSort.SortBuilder sortBuilder;
    if ("desc".equalsIgnoreCase(order)) {
        sortBuilder = DBSort.desc(sort);
    } else {
        sortBuilder = DBSort.asc(sort);
    }
    try {
        final SearchQuery searchQuery = cacheSearchQueryParser.parse(query);
        final DBQuery.Query dbQuery = searchQuery.toDBQuery();
        PaginatedList<CacheDto> paginated = dbCacheService.findPaginated(dbQuery, sortBuilder, page, perPage);
        return new CachesPage(query, paginated.pagination(), paginated.stream().map(CacheApi::fromDto).collect(Collectors.toList()));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) DBQuery(org.mongojack.DBQuery) BadRequestException(javax.ws.rs.BadRequestException) CacheApi(org.graylog2.rest.models.system.lookup.CacheApi) CacheDto(org.graylog2.lookup.dto.CacheDto) DBSort(org.mongojack.DBSort) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)23 GET (javax.ws.rs.GET)22 SearchQuery (org.graylog2.search.SearchQuery)21 BadRequestException (javax.ws.rs.BadRequestException)17 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)16 Path (javax.ws.rs.Path)15 PaginatedList (org.graylog2.database.PaginatedList)11 Produces (javax.ws.rs.Produces)10 Timed (com.codahale.metrics.annotation.Timed)9 Api (io.swagger.annotations.Api)7 ApiParam (io.swagger.annotations.ApiParam)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 PUT (javax.ws.rs.PUT)7 SearchQueryParser (org.graylog2.search.SearchQueryParser)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 DELETE (javax.ws.rs.DELETE)6 DefaultValue (javax.ws.rs.DefaultValue)6