}
}
public Result saveChanges(String username) {
final Form<ChangeUserRequestForm> requestForm = Form.form(ChangeUserRequestForm.class).bindFromRequest();
final User user = userService.load(username);
if (requestForm.hasErrors()) {
final BreadcrumbList bc = new BreadcrumbList();
bc.addCrumb("System", routes.SystemController.index(0));
bc.addCrumb("Users", routes.UsersController.index());
bc.addCrumb("Edit " + username, routes.UsersController.editUserForm(username));
final List<String> all = permissionsService.all();
boolean requiresOldPassword = checkRequireOldPassword(username);
try {
return badRequest(edit.render(
requestForm,
username,
currentUser(),
user,
requiresOldPassword,
all,
ImmutableSet.copyOf(requestForm.get().permissions),
DateTools.getGroupedTimezoneIds().asMap(),
streamService.all(),
dashboardService.getAll(),
bc));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not fetch streams. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
final ChangeUserRequestForm formData = requestForm.get();
// translate session timeout value from form fields to millis
if (!formData.session_timeout_never) {
TimeUnit timeoutUnit;
if (formData.timeout_unit != null) {
try {
timeoutUnit = TimeUnit.valueOf(formData.timeout_unit.toUpperCase());
formData.sessionTimeoutMs = timeoutUnit.toMillis(formData.timeout);
} catch (IllegalArgumentException e) {
log.warn("Unknown value for session timeout unit. Cannot set session timeout value.", e);
}
}
} else {
formData.sessionTimeoutMs = -1; // which translates to "never".
}
Set<String> permissions = Sets.newHashSet(user.getPermissions());
// TODO this does not handle combined permissions like streams:edit,read:1,2 !
// remove all streams:edit, streams:read permissions and add the ones from the form back.
permissions = Sets.newHashSet(Sets.filter(permissions, new Predicate<String>() {
@Override
public boolean apply(@Nullable String input) {
return (input != null) &&
!(input.startsWith(STREAMS_READ) || input.startsWith(STREAMS_EDIT) ||
input.startsWith(DASHBOARDS_READ) || input.startsWith(DASHBOARDS_EDIT));
}
}));
for (String streampermission : formData.streampermissions) {
permissions.add(RestPermissions.STREAMS_READ + ":" + streampermission);
}
for (String streameditpermission : formData.streameditpermissions) {
permissions.add(RestPermissions.STREAMS_EDIT + ":" + streameditpermission);
}
for (String dashboardpermission : formData.dashboardpermissions) {
permissions.add(RestPermissions.DASHBOARDS_READ + ":" + dashboardpermission);
}
for (String dashboardeditpermissions : formData.dashboardeditpermissions) {
permissions.add(RestPermissions.DASHBOARDS_EDIT + ":" + dashboardeditpermissions);
}
final ChangeUserRequest changeRequest = formData.toApiRequest();
changeRequest.permissions = Lists.newArrayList(permissions);
user.update(changeRequest);
return redirect(routes.UsersController.index());
}