use of org.graylog2.users.UserOverviewDTO 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.users.UserOverviewDTO 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.users.UserOverviewDTO in project graylog2-server by Graylog2.
the class AuthServiceBackendsResource method getUsers.
@GET
@Path("{backendId}/users")
@ApiOperation("Get paginated users for an authentication service backend")
@RequiresPermissions({ RestPermissions.AUTH_SERVICE_GLOBAL_CONFIG_READ, RestPermissions.USERS_READ })
public PaginatedResponse<UserOverviewDTO> getUsers(@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(UserOverviewDTO.FIELD_FULL_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order, @ApiParam(name = "backendId", required = true) @PathParam("backendId") @NotBlank String backendId) {
final AuthServiceBackendDTO activeConfig = loadConfig(backendId);
final PaginatedList<UserOverviewDTO> userList = userService.findPaginatedByAuthServiceBackend(parseSearchQuery(query), page, perPage, sort, order, activeConfig.id());
return PaginatedResponse.create("users", userList, query, Collections.singletonMap("roles", createRoleContext(userList.delegate())));
}
use of org.graylog2.users.UserOverviewDTO in project graylog2-server by Graylog2.
the class UsersResource method getPage.
@GET
@Timed
@Path("/paginated")
@ApiOperation(value = "Get paginated list of users")
@RequiresPermissions(RestPermissions.USERS_LIST)
@Produces(MediaType.APPLICATION_JSON)
public PaginatedResponse<UserOverviewDTO> getPage(@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 = "title,description") @DefaultValue(UserOverviewDTO.FIELD_FULL_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
SearchQuery searchQuery;
final AllUserSessions sessions = AllUserSessions.create(sessionService);
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final PaginatedList<UserOverviewDTO> result = paginatedUserService.findPaginated(searchQuery, page, perPage, sort, order);
final Set<String> allRoleIds = result.stream().flatMap(userDTO -> {
if (userDTO.roles() != null) {
return userDTO.roles().stream();
}
return Stream.empty();
}).collect(Collectors.toSet());
Map<String, String> roleNameMap;
try {
roleNameMap = getRoleNameMap(allRoleIds);
} catch (org.graylog2.database.NotFoundException e) {
throw new NotFoundException("Couldn't find roles: " + e.getMessage());
}
final UserOverviewDTO adminUser = getAdminUserDTO(sessions);
List<UserOverviewDTO> users = result.stream().map(userDTO -> {
UserOverviewDTO.Builder builder = userDTO.toBuilder().fillSession(sessions.forUser(userDTO));
if (userDTO.roles() != null) {
builder.roles(userDTO.roles().stream().map(roleNameMap::get).collect(Collectors.toSet()));
}
return builder.build();
}).collect(Collectors.toList());
final PaginatedList<UserOverviewDTO> userOverviewDTOS = new PaginatedList<>(users, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
return PaginatedResponse.create("users", userOverviewDTOS, query, Collections.singletonMap("admin_user", adminUser));
}
use of org.graylog2.users.UserOverviewDTO in project graylog2-server by Graylog2.
the class UsersResource method getAdminUserDTO.
private UserOverviewDTO getAdminUserDTO(AllUserSessions sessions) {
final Optional<User> optionalAdmin = userManagementService.getRootUser();
if (!optionalAdmin.isPresent()) {
return null;
}
final User admin = optionalAdmin.get();
final Set<String> adminRoles = userManagementService.getRoleNames(admin);
final Optional<MongoDbSession> lastSession = sessions.forUser(admin);
return UserOverviewDTO.builder().username(admin.getName()).fullName(admin.getFullName()).email(admin.getEmail()).externalUser(admin.isExternalUser()).readOnly(admin.isReadOnly()).id(admin.getId()).fillSession(lastSession).roles(adminRoles).build();
}
Aggregations