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