use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class RoleImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
String name = json.getString("name");
RoleVO vo = null;
if (StringUtils.isBlank(xid)) {
xid = service.generateUniqueXid();
} else {
try {
vo = service.get(xid);
} catch (NotFoundException e) {
}
}
if (vo == null) {
vo = new RoleVO(Common.NEW_ID, xid, name);
}
try {
// Read into the VO to get all properties
ctx.getReader().readInto(vo, json);
boolean isnew = vo.getId() == Common.NEW_ID;
if (isnew) {
service.insert(vo);
} else {
service.update(vo.getId(), vo);
}
addSuccessMessage(isnew, "emport.role.prefix", xid);
} catch (ValidationException e) {
setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
} catch (JsonException e) {
addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class MangoPasswordAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (!(authentication instanceof UsernamePasswordAuthenticationToken)) {
return null;
}
if (!(authentication.getDetails() instanceof WebAuthenticationDetails)) {
throw new InternalAuthenticationServiceException("Expected authentication details to be instance of WebAuthenticationDetails");
}
String username = authentication.getName();
WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
String ip = details.getRemoteAddress();
try {
boolean ipRateExceeded = this.ipRateLimiter != null && this.ipRateLimiter.checkRateExceeded(ip);
boolean usernameRateExceeded = this.usernameRateLimiter != null && this.usernameRateLimiter.checkRateExceeded(username);
if (ipRateExceeded) {
throw new IpAddressAuthenticationRateException(ip);
}
if (usernameRateExceeded) {
throw new UsernameAuthenticationRateException(username);
}
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
if (!(userDetails instanceof User)) {
throw new InternalAuthenticationServiceException("Expected user details to be instance of User");
}
User user = (User) userDetails;
String newPassword = null;
if (authentication instanceof PasswordResetAuthenticationToken) {
newPassword = ((PasswordResetAuthenticationToken) authentication).getNewPassword();
}
try {
this.userDetailsChecker.check(user);
} catch (CredentialsExpiredException e) {
if (newPassword == null) {
throw e;
}
}
String credentials = (String) authentication.getCredentials();
// Validating the password against the database.
if (!Common.checkPassword(credentials, user.getPassword())) {
throw new BadCredentialsException(Common.translate("login.validation.invalidLogin"));
}
if (newPassword != null) {
if (newPassword.equals(credentials)) {
throw new PasswordChangeException(new TranslatableMessage("rest.exception.samePassword"));
}
final String password = newPassword;
runAs.runAs(user, () -> {
try {
usersService.updatePassword(user, password);
} catch (ValidationException e) {
throw new PasswordChangeException(new TranslatableMessage("rest.exception.complexityRequirementsFailed"));
}
return null;
});
}
return new UsernamePasswordAuthenticationToken(user, user.getPassword(), Collections.unmodifiableCollection(user.getAuthorities()));
} catch (AuthenticationException e) {
if (this.ipRateLimiter != null) {
this.ipRateLimiter.hit(ip);
}
if (this.usernameRateLimiter != null) {
this.usernameRateLimiter.hit(username);
}
throw e;
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class DataSourceVO method setAlarmLevel.
/**
* Set an alarm level based on the sub-type of the data source event type
* which MUST (and already is) one of the codes in getEventCodes()
*/
public void setAlarmLevel(String subType, AlarmLevels level) throws ValidationException {
ExportCodes codes = getEventCodes();
int eventId = codes.getId(subType);
if (eventId == -1) {
ProcessResult result = new ProcessResult();
result.addContextualMessage("alarmLevel", "emport.error.eventCode", subType, codes.getCodeList());
throw new ValidationException(result);
}
alarmLevels.put(eventId, level);
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class Validatable method ensureValid.
/**
* Validates the object and throws a ValidationException if it is not valid
*/
default void ensureValid() throws ValidationException {
ProcessResult response = new ProcessResult();
this.validate(response);
if (!response.isValid()) {
throw new ValidationException(response);
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class AbstractVOService method insert.
/**
*/
@Override
public T insert(T vo) throws PermissionException, ValidationException {
PermissionHolder user = Common.getUser();
// Ensure they can create
ensureCreatePermission(user, vo);
// Ensure id is not set
if (vo.getId() != Common.NEW_ID) {
ProcessResult result = new ProcessResult();
result.addContextualMessage("id", "validate.invalidValue");
throw new ValidationException(result, vo.getClass());
}
// Generate an Xid if necessary
if (StringUtils.isEmpty(vo.getXid()))
vo.setXid(dao.generateUniqueXid());
ensureValid(vo);
dao.insert(vo);
return vo;
}
Aggregations