use of org.springframework.validation.BindingResult in project entando-core by entando.
the class UserController method deleteUserAuthorities.
@RestAccessControl(permission = Permission.MANAGE_USERS)
@RequestMapping(value = "/{target:.+}/authorities", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SimpleRestResponse> deleteUserAuthorities(@ModelAttribute("user") UserDetails user, @PathVariable String target) throws ApsSystemException {
logger.debug("user {} requesting delete authorities for username {}", user.getUsername(), target);
DataBinder binder = new DataBinder(target);
BindingResult bindingResult = binder.getBindingResult();
// field validations
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}
// business validations
getUserValidator().validateUpdateSelf(target, user.getUsername(), bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}
this.getUserService().deleteUserAuthorities(target);
return new ResponseEntity<>(new SimpleRestResponse<>(new ArrayList<>()), HttpStatus.OK);
}
use of org.springframework.validation.BindingResult in project entando-core by entando.
the class UserValidator method createDeleteAdminError.
public static BindingResult createDeleteAdminError() {
Map<String, String> map = new HashMap<>();
map.put("username", "admin");
BindingResult bindingResult = new MapBindingResult(map, "username");
bindingResult.reject(UserValidator.ERRCODE_DELETE_ADMIN, new String[] {}, "user.admin.cant.delete");
return bindingResult;
}
use of org.springframework.validation.BindingResult in project entando-core by entando.
the class EntityAttributeDto method fillEntityAttribute.
public void fillEntityAttribute(AttributeInterface attribute, BindingResult bindingResult) {
if (attribute instanceof ITextAttribute) {
ITextAttribute textAttribute = (ITextAttribute) attribute;
if (attribute.isMultilingual() && this.getValues() != null && !this.getValues().isEmpty()) {
this.getValues().keySet().stream().forEach(langCode -> textAttribute.setText(this.getValues().get(langCode).toString(), langCode));
} else if (null != this.getValue()) {
textAttribute.setText(this.getValue().toString(), null);
}
}
if (attribute instanceof NumberAttribute && (null != this.getValue())) {
BigDecimal number = new BigDecimal(this.getValue().toString());
((NumberAttribute) attribute).setValue(number);
}
if (attribute instanceof BooleanAttribute) {
((BooleanAttribute) attribute).setBooleanValue((Boolean) this.getValue());
}
if (attribute instanceof DateAttribute && (null != this.getValue())) {
Date date = null;
String dateValue = null;
try {
date = DateConverter.parseDate(this.getValue().toString(), SystemConstants.API_DATE_FORMAT);
} catch (Exception e) {
dateValue = this.getValue().toString();
}
((DateAttribute) attribute).setDate(date);
((DateAttribute) attribute).setFailedDateString(dateValue);
}
if (attribute instanceof CompositeAttribute && (null != this.getCompositeElements())) {
this.getCompositeElements().stream().forEach(i -> {
AttributeInterface compositeElement = ((CompositeAttribute) attribute).getAttribute(i.getCode());
i.fillEntityAttribute(compositeElement, bindingResult);
});
} else if (attribute instanceof MonoListAttribute && (null != this.getElements())) {
this.getElements().stream().forEach(i -> {
AttributeInterface prototype = ((MonoListAttribute) attribute).addAttribute();
prototype.setName(((MonoListAttribute) attribute).getName());
i.fillEntityAttribute(prototype, bindingResult);
});
} else if (attribute instanceof ListAttribute && (null != this.getListElements())) {
((ListAttribute) attribute).getAttributeListMap().clear();
this.getListElements().keySet().stream().forEach(langCode -> {
List<EntityAttributeDto> list = this.getListElements().get(langCode);
list.stream().forEach(i -> {
AttributeInterface prototype = ((ListAttribute) attribute).addAttribute(langCode);
prototype.setName(((ListAttribute) attribute).getName());
i.fillEntityAttribute(prototype, bindingResult);
});
});
}
}
use of org.springframework.validation.BindingResult in project entando-core by entando.
the class ApiConsumerValidator method validateForUpdate.
public void validateForUpdate(String consumerKey, ApiConsumer consumer) {
if (!consumer.getKey().equals(consumerKey)) {
DataBinder dataBinder = new DataBinder(consumer);
BindingResult bindingResult = dataBinder.getBindingResult();
bindingResult.rejectValue("key", ERRCODE_URINAME_MISMATCH, new String[] { consumerKey, consumer.getKey() }, "api.consumer.key.mismatch");
throw new ValidationGenericException(bindingResult);
}
}
use of org.springframework.validation.BindingResult in project entando-core by entando.
the class ApiConsumerValidator method validateForCreate.
/**
* The secret must be not null for creating the consumer. Instead it can be
* null during the update.
*/
public void validateForCreate(ApiConsumer consumer) {
if (StringUtils.isEmpty(consumer.getSecret())) {
DataBinder dataBinder = new DataBinder(consumer);
BindingResult bindingResult = dataBinder.getBindingResult();
bindingResult.rejectValue("secret", "notBlank", new String[] { "secret" }, "string.notBlank");
throw new ValidationGenericException(bindingResult);
}
}
Aggregations