in app/src/main/java/com/amazon/aws/partners/saasfactory/pgrls/controller/TenantController.java [139:191]
public String saveTenant(Authentication authentication, @ModelAttribute User user, BindingResult binding, Model model, final RedirectAttributes redirectAttributes, WebRequest request) {
String view = null;
if (user.getEmail() == null || user.getEmail().isEmpty()) {
binding.addError(new FieldError("user", "email", "User email is required"));
view = "editUser";
} else if (user.getGivenName() == null || user.getGivenName().isEmpty()) {
binding.addError(new FieldError("user", "giveName", "User first name is required"));
view = "editUser";
} else if (user.getFamilyName() == null || user.getFamilyName().isEmpty()) {
binding.addError(new FieldError("user", "familyName", "User last name is required"));
view = "editUser";
} else if (user.getTenant() == null || user.getTenant().getId() == null) {
String requestedTenantId = request.getParameter("tenant");
if (requestedTenantId != null && !requestedTenantId.isEmpty() && adminService.tenantExists(UUID.fromString(requestedTenantId))) {
Tenant authenticatedTenant = (Tenant) authentication.getPrincipal();
LOGGER.warn("Row Level Security policies prevented " + authenticatedTenant.getIdAsString() + " from accessing data for tenant " + requestedTenantId);
redirectAttributes.addFlashAttribute("css", "danger");
redirectAttributes.addFlashAttribute("msg", "Row Level Security policies prevented " + authenticatedTenant.getIdAsString() + " from accessing data for tenant " + requestedTenantId);
view = "redirect:/tenant";
} else {
LOGGER.error("Unable to load tenant for user from input " + requestedTenantId);
redirectAttributes.addFlashAttribute("css", "danger");
redirectAttributes.addFlashAttribute("msg", "Unable to load tenant for user from input");
view = "redirect:/tenant";
}
} else {
try {
boolean isNew = (user.getId() == null);
LOGGER.info("Saving {}user {}", isNew ? "new " : "", user.getEmail());
user = tenantService.saveUser(user);
redirectAttributes.addFlashAttribute("css", "success");
if (isNew) {
redirectAttributes.addFlashAttribute("msg", "New user added");
} else {
redirectAttributes.addFlashAttribute("msg", "User updated");
}
// Add the tenant back into model for the redirect
redirectAttributes.addFlashAttribute("selectedTenant", user.getTenant().getId());
view = "redirect:/tenant";
} catch (UnauthorizedException e) {
LOGGER.warn("Authenticated tenant is not authorized to save user for current tenant");
Tenant authenticatedTenant = (Tenant) authentication.getPrincipal();
redirectAttributes.addFlashAttribute("css", "danger");
redirectAttributes.addFlashAttribute("msg", "Row Level Security policies prevented " + authenticatedTenant.getIdAsString() + " from creating a user");
view = "editUser";
} catch (UniqueRecordException e) {
LOGGER.warn("Duplicate user email error");
binding.addError(new FieldError("user", "email", "User already exists"));
view = "editUser";
}
}
return view;
}