use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.
the class DataSourceService method copy.
/**
* Copy a data source and optionally its points
*/
public DataSourceVO copy(String xid, String copyXid, String copyName, String copyDeviceName, boolean enabled, boolean copyPoints) throws PermissionException, NotFoundException {
DataSourceVO existing = get(xid);
PermissionHolder user = Common.getUser();
ensureCreatePermission(user, existing);
// Determine the new name
String newName;
if (StringUtils.isEmpty(copyName)) {
newName = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", existing.getName()), 40);
} else {
newName = copyName;
}
// Determine the new xid
String newXid;
if (StringUtils.isEmpty(copyXid)) {
newXid = DataSourceDao.getInstance().generateUniqueXid();
} else {
newXid = copyXid;
}
String newDeviceName;
if (StringUtils.isEmpty(copyDeviceName)) {
newDeviceName = existing.getName();
} else {
newDeviceName = copyDeviceName;
}
// Ensure device name is valid
if (StringValidation.isLengthGreaterThan(newDeviceName, 255)) {
ProcessResult result = new ProcessResult();
result.addMessage("deviceName", new TranslatableMessage("validate.notLongerThan", 255));
throw new ValidationException(result);
}
DataSourceVO copy = (DataSourceVO) existing.copy();
copy.setId(Common.NEW_ID);
copy.setName(newName);
copy.setXid(newXid);
copy.setEnabled(enabled);
ensureValid(copy);
// Save it
insert(copy);
if (copyPoints) {
// Copy the points from this data source
dataPointService.copyDataSourcePoints(existing.getId(), copy.getId(), newDeviceName);
}
return get(newXid);
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.
the class AbstractBasicVOService method insert.
/**
*/
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());
}
ensureValid(vo);
dao.insert(vo);
return vo;
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.
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;
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.
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 MangoAutomation.
the class NumericPointValueWithDifferentSeriesIdTest method createMockDataPoints.
@Override
protected List<IDataPoint> createMockDataPoints(int count) {
List<IDataPoint> points = super.createMockDataPoints(count);
// Change series id
DataPointService service = Common.getBean(DataPointService.class);
DataPointDao dao = Common.getBean(DataPointDao.class);
for (IDataPoint point : points) {
DataPointVO vo = (DataPointVO) point;
vo.setSeriesId(dao.insertNewTimeSeries());
try {
service.update(vo.getId(), vo);
} catch (ValidationException e) {
String failureMessage = "";
for (ProcessMessage m : e.getValidationResult().getMessages()) {
String messagePart = m.getContextKey() + " -> " + m.getContextualMessage().translate(Common.getTranslations()) + "\n";
failureMessage += messagePart;
}
fail(failureMessage);
}
}
return points;
}
Aggregations