Search in sources :

Example 6 with Roles

use of org.graylog2.shared.users.Roles in project graylog2-server by Graylog2.

the class RolesResource method addMember.

@PUT
@Path("{rolename}/members/{username}")
@ApiOperation("Add a user to a role")
@AuditEvent(type = AuditEventTypes.ROLE_MEMBERSHIP_UPDATE)
public Response addMember(@ApiParam(name = "rolename") @PathParam("rolename") String rolename, @ApiParam(name = "username") @PathParam("username") String username, @ApiParam(name = "JSON Body", value = "Placeholder because PUT requests should have a body. Set to '{}', the content will be ignored.", defaultValue = "{}") String body) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " has not been found.");
    }
    // verify that the role exists
    final Role role = roleService.load(rolename);
    final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
    roles.add(role.getId());
    user.setRoleIds(roles);
    try {
        userService.save(user);
    } catch (ValidationException e) {
        throw new BadRequestException("Validation failed", e);
    }
    return status(Response.Status.NO_CONTENT).build();
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 7 with Roles

use of org.graylog2.shared.users.Roles in project graylog2-server by Graylog2.

the class RolesResource method removeMember.

@DELETE
@Path("{rolename}/members/{username}")
@ApiOperation("Remove a user from a role")
@AuditEvent(type = AuditEventTypes.ROLE_MEMBERSHIP_DELETE)
public Response removeMember(@ApiParam(name = "rolename") @PathParam("rolename") String rolename, @ApiParam(name = "username") @PathParam("username") String username) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " has not been found.");
    }
    // verify that the role exists
    final Role role = roleService.load(rolename);
    final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
    roles.remove(role.getId());
    user.setRoleIds(roles);
    try {
        userService.save(user);
    } catch (ValidationException e) {
        throw new BadRequestException("Validation failed", e);
    }
    return status(Response.Status.NO_CONTENT).build();
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 8 with Roles

use of org.graylog2.shared.users.Roles in project graylog2-server by Graylog2.

the class UserServiceImpl method dissociateAllUsersFromRole.

@Override
public void dissociateAllUsersFromRole(Role role) {
    final Collection<User> usersInRole = loadAllForRole(role);
    // remove role from any user still assigned
    for (User user : usersInRole) {
        if (user.isLocalAdmin()) {
            continue;
        }
        final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
        roles.remove(role.getId());
        user.setRoleIds(roles);
        try {
            save(user);
        } catch (ValidationException e) {
            LOG.error("Unable to remove role {} from user {}", role.getName(), user);
        }
    }
}
Also used : User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException)

Aggregations

Role (org.graylog2.shared.users.Role)5 ValidationException (org.graylog2.plugin.database.ValidationException)4 User (org.graylog2.plugin.database.users.User)4 ApiOperation (io.swagger.annotations.ApiOperation)3 NotFoundException (org.graylog2.database.NotFoundException)3 BadRequestException (javax.ws.rs.BadRequestException)2 Path (javax.ws.rs.Path)2 AuditEvent (org.graylog2.audit.jersey.AuditEvent)2 Predicate (com.google.common.base.Predicate)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 List (java.util.List)1 Set (java.util.Set)1 Inject (javax.inject.Inject)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 PUT (javax.ws.rs.PUT)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 UserPermissionMigrationState (org.graylog2.cluster.UserPermissionMigrationState)1