Search in sources :

Example 1 with UserSummary

use of org.graylog2.rest.models.users.responses.UserSummary in project graylog2-server by Graylog2.

the class UsersResource method get.

@GET
@Path("{username}")
@ApiOperation(value = "Get user details", notes = "The user's permissions are only included if a user asks for his " + "own account or for users with the necessary permissions to edit permissions.")
@ApiResponses({ @ApiResponse(code = 404, message = "The user could not be found.") })
public UserSummary get(@ApiParam(name = "username", value = "The username to return information for.", required = true) @PathParam("username") String username) {
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("Couldn't find user " + username);
    }
    // if the requested username does not match the authenticated user, then we don't return permission information
    final boolean allowedToSeePermissions = isPermitted(USERS_PERMISSIONSEDIT, username);
    final boolean permissionsAllowed = getSubject().getPrincipal().toString().equals(username) || allowedToSeePermissions;
    return toUserResponse(user, permissionsAllowed, Optional.empty());
}
Also used : User(org.graylog2.plugin.database.users.User) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with UserSummary

use of org.graylog2.rest.models.users.responses.UserSummary in project graylog2-server by Graylog2.

the class UsersResource method listUsers.

@GET
@Deprecated
@RequiresPermissions(RestPermissions.USERS_LIST)
@ApiOperation(value = "List all users", notes = "The permissions assigned to the users are always included.")
public UserList listUsers() {
    final List<User> users = userManagementService.loadAll();
    final AllUserSessions sessions = AllUserSessions.create(sessionService);
    final List<UserSummary> resultUsers = Lists.newArrayListWithCapacity(users.size() + 1);
    userManagementService.getRootUser().ifPresent(adminUser -> resultUsers.add(toUserResponse(adminUser, sessions)));
    for (User user : users) {
        resultUsers.add(toUserResponse(user, sessions));
    }
    return UserList.create(resultUsers);
}
Also used : User(org.graylog2.plugin.database.users.User) UserSummary(org.graylog2.rest.models.users.responses.UserSummary) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with UserSummary

use of org.graylog2.rest.models.users.responses.UserSummary in project graylog2-server by Graylog2.

the class UsersResource method toUserResponse.

private UserSummary toUserResponse(User user, boolean includePermissions, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<MongoDbSession> mongoDbSession) {
    final Set<String> roleIds = user.getRoleIds();
    Set<String> roleNames = Collections.emptySet();
    if (!roleIds.isEmpty()) {
        roleNames = userService.getRoleNames(user);
        if (roleNames.isEmpty()) {
            LOG.error("Unable to load role names for role IDs {} for user {}", roleIds, user);
        }
    }
    boolean sessionActive = false;
    Date lastActivity = null;
    String clientAddress = null;
    if (mongoDbSession.isPresent()) {
        final MongoDbSession session = mongoDbSession.get();
        sessionActive = true;
        lastActivity = session.getLastAccessTime();
        clientAddress = session.getHost();
    }
    return UserSummary.create(user.getId(), user.getName(), user.getEmail(), user.getFullName(), includePermissions ? userService.getPermissionsForUser(user) : Collections.emptyList(), user.getPreferences(), firstNonNull(user.getTimeZone(), DateTimeZone.UTC).getID(), user.getSessionTimeoutMs(), user.isReadOnly(), user.isExternalUser(), user.getStartpage(), roleNames, sessionActive, lastActivity, clientAddress);
}
Also used : MongoDbSession(org.graylog2.security.MongoDbSession) Date(java.util.Date)

Example 4 with UserSummary

use of org.graylog2.rest.models.users.responses.UserSummary in project graylog2-server by Graylog2.

the class RolesResource method getMembers.

@GET
@Path("{rolename}/members")
@RequiresPermissions({ RestPermissions.USERS_LIST, RestPermissions.ROLES_READ })
@ApiOperation("Retrieve the role's members")
public RoleMembershipResponse getMembers(@ApiParam(name = "rolename", required = true) @PathParam("rolename") String name) throws NotFoundException {
    final Role role = roleService.load(name);
    final Collection<User> users = userService.loadAllForRole(role);
    Set<UserSummary> userSummaries = Sets.newHashSetWithExpectedSize(users.size());
    for (User user : users) {
        final Set<String> roleNames = userService.getRoleNames(user);
        List<WildcardPermission> wildcardPermissions;
        List<GRNPermission> grnPermissions;
        if (isPermitted(RestPermissions.USERS_PERMISSIONSEDIT, user.getName())) {
            wildcardPermissions = userService.getWildcardPermissionsForUser(user);
            grnPermissions = userService.getGRNPermissionsForUser(user);
        } else {
            wildcardPermissions = ImmutableList.of();
            grnPermissions = ImmutableList.of();
        }
        userSummaries.add(UserSummary.create(user.getId(), user.getName(), user.getEmail(), user.getFirstName().orElse(null), user.getLastName().orElse(null), user.getFullName(), wildcardPermissions, grnPermissions, user.getPreferences(), firstNonNull(user.getTimeZone(), DateTimeZone.UTC).getID(), user.getSessionTimeoutMs(), user.isReadOnly(), user.isExternalUser(), user.getStartpage(), roleNames, // there is no session information available in this call, so we set it to null
        false, null, null, user.getAccountStatus()));
    }
    return RoleMembershipResponse.create(role.getName(), userSummaries);
}
Also used : Role(org.graylog2.shared.users.Role) GRNPermission(org.graylog.security.permissions.GRNPermission) User(org.graylog2.plugin.database.users.User) UserSummary(org.graylog2.rest.models.users.responses.UserSummary) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with UserSummary

use of org.graylog2.rest.models.users.responses.UserSummary in project graylog2-server by Graylog2.

the class UsersResource method toUserResponse.

private UserSummary toUserResponse(User user, boolean includePermissions, AllUserSessions sessions) {
    final Set<String> roleIds = user.getRoleIds();
    Set<String> roleNames = Collections.emptySet();
    if (!roleIds.isEmpty()) {
        roleNames = userManagementService.getRoleNames(user);
        if (roleNames.isEmpty()) {
            LOG.error("Unable to load role names for role IDs {} for user {}", roleIds, user);
        }
    }
    boolean sessionActive = false;
    Date lastActivity = null;
    String clientAddress = null;
    final Optional<MongoDbSession> mongoDbSession = sessions.forUser(user);
    if (mongoDbSession.isPresent()) {
        final MongoDbSession session = mongoDbSession.get();
        sessionActive = true;
        lastActivity = session.getLastAccessTime();
        clientAddress = session.getHost();
    }
    List<WildcardPermission> wildcardPermissions;
    List<GRNPermission> grnPermissions;
    if (includePermissions) {
        wildcardPermissions = userManagementService.getWildcardPermissionsForUser(user);
        grnPermissions = userManagementService.getGRNPermissionsForUser(user);
    } else {
        wildcardPermissions = ImmutableList.of();
        grnPermissions = ImmutableList.of();
    }
    return UserSummary.create(user.getId(), user.getName(), user.getEmail(), user.getFirstName().orElse(null), user.getLastName().orElse(null), user.getFullName(), wildcardPermissions, grnPermissions, user.getPreferences(), user.getTimeZone() == null ? null : user.getTimeZone().getID(), user.getSessionTimeoutMs(), user.isReadOnly(), user.isExternalUser(), user.getStartpage(), roleNames, sessionActive, lastActivity, clientAddress, user.getAccountStatus());
}
Also used : GRNPermission(org.graylog.security.permissions.GRNPermission) MongoDbSession(org.graylog2.security.MongoDbSession) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Date(java.util.Date)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 GET (javax.ws.rs.GET)4 User (org.graylog2.plugin.database.users.User)4 Path (javax.ws.rs.Path)3 ApiResponses (io.swagger.annotations.ApiResponses)2 Date (java.util.Date)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 WildcardPermission (org.apache.shiro.authz.permission.WildcardPermission)2 GRNPermission (org.graylog.security.permissions.GRNPermission)2 UserSummary (org.graylog2.rest.models.users.responses.UserSummary)2 MongoDbSession (org.graylog2.security.MongoDbSession)2 ForbiddenException (javax.ws.rs.ForbiddenException)1 NotFoundException (javax.ws.rs.NotFoundException)1 Role (org.graylog2.shared.users.Role)1