Search in sources :

Example 1 with RoleImpl

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

the class RolesResource method create.

@POST
@RequiresPermissions(RestPermissions.ROLES_CREATE)
@ApiOperation(value = "Create a new role", notes = "")
@AuditEvent(type = AuditEventTypes.ROLE_CREATE)
public Response create(@ApiParam(name = "JSON body", value = "The new role to create", required = true) @Valid @NotNull RoleResponse roleResponse) {
    if (roleService.exists(roleResponse.name())) {
        throw new BadRequestException("Role " + roleResponse.name() + " already exists.");
    }
    Role role = new RoleImpl();
    role.setName(roleResponse.name());
    role.setPermissions(roleResponse.permissions());
    role.setDescription(roleResponse.description().orNull());
    try {
        role = roleService.save(role);
    } catch (ValidationException e) {
        log.error("Invalid role creation request.");
        throw new BadRequestException(e);
    }
    final URI uri = getUriBuilderToSelf().path(RolesResource.class).path("{rolename}").build(role.getName());
    return Response.created(uri).entity(RoleResponse.create(role.getName(), Optional.fromNullable(role.getDescription()), role.getPermissions(), role.isReadOnly())).build();
}
Also used : Role(org.graylog2.shared.users.Role) ValidationException(org.graylog2.plugin.database.ValidationException) RoleImpl(org.graylog2.users.RoleImpl) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 URI (java.net.URI)1 BadRequestException (javax.ws.rs.BadRequestException)1 POST (javax.ws.rs.POST)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 AuditEvent (org.graylog2.audit.jersey.AuditEvent)1 ValidationException (org.graylog2.plugin.database.ValidationException)1 Role (org.graylog2.shared.users.Role)1 RoleImpl (org.graylog2.users.RoleImpl)1