Search in sources :

Example 16 with ValidationException

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));
    }
}
Also used : JsonException(com.serotonin.json.JsonException) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 17 with ValidationException

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;
    }
}
Also used : User(com.serotonin.m2m2.vo.User) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) AuthenticationException(org.springframework.security.core.AuthenticationException) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserDetails(org.springframework.security.core.userdetails.UserDetails) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 18 with ValidationException

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);
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 19 with ValidationException

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);
    }
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 20 with ValidationException

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;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Aggregations

ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)75 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)31 JsonException (com.serotonin.json.JsonException)22 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)20 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)16 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)14 JsonValue (com.serotonin.json.type.JsonValue)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 JsonObject (com.serotonin.json.type.JsonObject)8 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)8 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)7 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)6 JsonArray (com.serotonin.json.type.JsonArray)6 DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 ArrayList (java.util.ArrayList)6 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)4 PublishedPointService (com.infiniteautomation.mango.spring.service.PublishedPointService)4