Search in sources :

Example 1 with UserOverviewDTO

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);
}
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 2 with UserOverviewDTO

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;
}
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 3 with UserOverviewDTO

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())));
}
Also used : UserOverviewDTO(org.graylog2.users.UserOverviewDTO) AuthServiceBackendDTO(org.graylog.security.authservice.AuthServiceBackendDTO) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with UserOverviewDTO

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));
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) USERS_TOKENCREATE(org.graylog2.shared.security.RestPermissions.USERS_TOKENCREATE) StringUtils(org.apache.commons.lang.StringUtils) DateTimeZone(org.joda.time.DateTimeZone) UserList(org.graylog2.rest.models.users.responses.UserList) USERS_PERMISSIONSEDIT(org.graylog2.shared.security.RestPermissions.USERS_PERMISSIONSEDIT) Produces(javax.ws.rs.Produces) UserManagementService(org.graylog2.shared.users.UserManagementService) USERS_TOKENLIST(org.graylog2.shared.security.RestPermissions.USERS_TOKENLIST) Date(java.util.Date) UserContext(org.graylog.security.UserContext) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) MongoDBSessionService(org.graylog2.security.MongoDBSessionService) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) SearchQueryField(org.graylog2.search.SearchQueryField) AccessTokenService(org.graylog2.security.AccessTokenService) USERS_ROLESEDIT(org.graylog2.shared.security.RestPermissions.USERS_ROLESEDIT) CreateUserRequest(org.graylog2.rest.models.users.requests.CreateUserRequest) Locale(java.util.Locale) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) ChangePasswordRequest(org.graylog2.rest.models.users.requests.ChangePasswordRequest) AccessToken(org.graylog2.security.AccessToken) DELETE(javax.ws.rs.DELETE) Token(org.graylog2.rest.models.users.responses.Token) Context(javax.ws.rs.core.Context) ImmutableMap(com.google.common.collect.ImmutableMap) TokenSummary(org.graylog2.rest.models.users.responses.TokenSummary) Collection(java.util.Collection) Set(java.util.Set) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) TokenList(org.graylog2.rest.models.users.responses.TokenList) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) PaginatedUserService(org.graylog2.users.PaginatedUserService) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) AuditEventTypes(org.graylog2.audit.AuditEventTypes) UserSummary(org.graylog2.rest.models.users.responses.UserSummary) Startpage(org.graylog2.rest.models.users.requests.Startpage) Optional(java.util.Optional) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) GRNPermission(org.graylog.security.permissions.GRNPermission) USERS_TOKENREMOVE(org.graylog2.shared.security.RestPermissions.USERS_TOKENREMOVE) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) Iterables(com.google.common.collect.Iterables) PathParam(javax.ws.rs.PathParam) NotBlank(javax.validation.constraints.NotBlank) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) RoleService(org.graylog2.users.RoleService) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) Roles(org.graylog2.shared.users.Roles) ArrayList(java.util.ArrayList) MongoDbSession(org.graylog2.security.MongoDbSession) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SearchQuery(org.graylog2.search.SearchQuery) Nullable(javax.annotation.Nullable) PermissionEditRequest(org.graylog2.rest.models.users.requests.PermissionEditRequest) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ChangeUserRequest(org.graylog2.shared.users.ChangeUserRequest) ForbiddenException(javax.ws.rs.ForbiddenException) RestResource(org.graylog2.shared.rest.resources.RestResource) Collectors.maxBy(java.util.stream.Collectors.maxBy) UpdateUserPreferences(org.graylog2.rest.models.users.requests.UpdateUserPreferences) ApiResponse(io.swagger.annotations.ApiResponse) ValidationException(org.graylog2.plugin.database.ValidationException) RestPermissions(org.graylog2.shared.security.RestPermissions) USERS_EDIT(org.graylog2.shared.security.RestPermissions.USERS_EDIT) PUT(javax.ws.rs.PUT) PaginatedResponse(org.graylog2.rest.models.PaginatedResponse) User(org.graylog2.plugin.database.users.User) Role(org.graylog2.shared.users.Role) Comparator(java.util.Comparator) Collections(java.util.Collections) NotFoundException(javax.ws.rs.NotFoundException) 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) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with UserOverviewDTO

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();
}
Also used : User(org.graylog2.plugin.database.users.User) MongoDbSession(org.graylog2.security.MongoDbSession)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)4 User (org.graylog2.plugin.database.users.User)4 Timed (com.codahale.metrics.annotation.Timed)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Api (io.swagger.annotations.Api)3 ApiParam (io.swagger.annotations.ApiParam)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 NotBlank (javax.validation.constraints.NotBlank)3 BadRequestException (javax.ws.rs.BadRequestException)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Objects (java.util.Objects)2