use of org.graylog2.plugin.database.ValidationException 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();
}
use of org.graylog2.plugin.database.ValidationException 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();
}
use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.
the class StreamAlarmCallbackResource method create.
@POST
@Timed
@ApiOperation(value = "Create an alarm callback", response = CreateAlarmCallbackResponse.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_CREATE)
public Response create(@ApiParam(name = "streamid", value = "The stream id this new alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "JSON body", required = true) CreateAlarmCallbackRequest originalCr) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_EDIT, streamid);
// make sure the values are correctly converted to the declared configuration types
final CreateAlarmCallbackRequest cr = CreateAlarmCallbackRequest.create(originalCr.type(), originalCr.title(), convertConfigurationValues(originalCr));
final AlarmCallbackConfiguration alarmCallbackConfiguration = alarmCallbackConfigurationService.create(streamid, cr, getCurrentUser().getName());
final String id;
try {
alarmCallbackFactory.create(alarmCallbackConfiguration).checkConfiguration();
id = alarmCallbackConfigurationService.save(alarmCallbackConfiguration);
} catch (ValidationException | AlarmCallbackConfigurationException | ConfigurationException e) {
LOG.error("Invalid alarm callback configuration.", e);
throw new BadRequestException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
LOG.error("Invalid alarm callback type.", e);
throw new BadRequestException("Invalid alarm callback type.", e);
}
final URI alarmCallbackUri = getUriBuilderToSelf().path(StreamAlarmCallbackResource.class).path("{alarmCallbackId}").build(streamid, id);
return Response.created(alarmCallbackUri).entity(CreateAlarmCallbackResponse.create(id)).build();
}
use of org.graylog2.plugin.database.ValidationException 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);
}
}
}
use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.
the class EsNodeProviderTest method singletonListZenUnicastHostsWorks.
@Test
public void singletonListZenUnicastHostsWorks() throws IOException, ValidationException, RepositoryException {
Map<String, String> settings = ImmutableMap.of("password_secret", "thisisatest", "retention_strategy", "delete", "root_password_sha2", "thisisatest", "elasticsearch_discovery_zen_ping_unicast_hosts", "example.com");
final ElasticsearchConfiguration config = new ElasticsearchConfiguration();
new JadConfig(new InMemoryRepository(settings), config).process();
final Settings nodeSettings = EsNodeProvider.readNodeSettings(config, nodeId);
assertThat(nodeSettings.getAsArray("discovery.zen.ping.unicast.hosts")).contains("example.com");
}
Aggregations