Search in sources :

Example 1 with PasswordInvalidException

use of com.infiniteautomation.mango.spring.service.PasswordService.PasswordInvalidException in project ma-core-public by infiniteautomation.

the class UsersService method commonValidation.

protected ProcessResult commonValidation(User vo, PermissionHolder holder) {
    ProcessResult response = new ProcessResult();
    if (StringUtils.isBlank(vo.getUsername()))
        response.addMessage("username", new TranslatableMessage("validate.required"));
    if (!UserDao.getInstance().isUsernameUnique(vo.getUsername(), vo.getId()))
        response.addMessage("username", new TranslatableMessage("users.validate.usernameInUse"));
    if (StringUtils.isBlank(vo.getEmail()))
        response.addMessage("email", new TranslatableMessage("validate.required"));
    else if (!UserDao.getInstance().isEmailUnique(vo.getEmail(), vo.getId()))
        response.addMessage("email", new TranslatableMessage("users.validate.emailUnique"));
    if (StringUtils.isBlank(vo.getPassword())) {
        response.addMessage("password", new TranslatableMessage("validate.required"));
    } else {
        Matcher m = Common.EXTRACT_ALGORITHM_HASH.matcher(vo.getPassword());
        if (!m.matches()) {
            response.addMessage("password", new TranslatableMessage("validate.illegalValue"));
        } else {
            String algorithm = m.group(1);
            String hashOrPassword = m.group(2);
            // Validate against our rules
            if (User.PLAIN_TEXT_ALGORITHM.equals(algorithm) || User.NONE_ALGORITHM.equals(algorithm)) {
                if (StringUtils.isBlank(hashOrPassword)) {
                    response.addMessage("password", new TranslatableMessage("validate.required"));
                }
                try {
                    passwordService.validatePassword(hashOrPassword);
                } catch (PasswordInvalidException e) {
                    for (TranslatableMessage message : e.getMessages()) {
                        response.addMessage("password", message);
                    }
                }
            }
        }
    }
    if (StringUtils.isBlank(vo.getName())) {
        response.addMessage("name", new TranslatableMessage("validate.required"));
    } else if (StringValidation.isLengthGreaterThan(vo.getName(), 255)) {
        response.addMessage("name", new TranslatableMessage("validate.notLongerThan", 255));
    }
    // Check field lengths
    if (StringValidation.isLengthGreaterThan(vo.getUsername(), 40))
        response.addMessage("username", new TranslatableMessage("validate.notLongerThan", 40));
    if (StringValidation.isLengthGreaterThan(vo.getEmail(), 255))
        response.addMessage("email", new TranslatableMessage("validate.notLongerThan", 255));
    if (StringValidation.isLengthGreaterThan(vo.getPhone(), 40))
        response.addMessage("phone", new TranslatableMessage("validate.notLongerThan", 40));
    if (vo.getReceiveAlarmEmails() == null) {
        response.addMessage("receiveAlarmEmails", new TranslatableMessage("validate.required"));
    }
    String locale = vo.getLocale();
    if (StringUtils.isNotEmpty(locale)) {
        if (StringValidation.isLengthGreaterThan(locale, 50)) {
            response.addMessage("locale", new TranslatableMessage("validate.notLongerThan", 50));
        }
        try {
            new Locale.Builder().setLanguageTag(locale).build();
        } catch (IllformedLocaleException e) {
            response.addMessage("locale", new TranslatableMessage("validate.invalidValue"));
        }
    }
    String timezone = vo.getTimezone();
    if (StringUtils.isNotEmpty(vo.getTimezone())) {
        if (StringValidation.isLengthGreaterThan(vo.getTimezone(), 50)) {
            response.addMessage("timezone", new TranslatableMessage("validate.notLongerThan", 50));
        }
        try {
            // noinspection ResultOfMethodCallIgnored
            ZoneId.of(timezone);
        } catch (DateTimeException e) {
            response.addMessage("timezone", new TranslatableMessage("validate.invalidValue"));
        }
    }
    // Can't set email verified
    if (vo.getEmailVerifiedDate() != null && !permissionService.hasAdminRole(holder)) {
        response.addContextualMessage("emailVerified", "validate.invalidValue");
    }
    if (StringUtils.isNotEmpty(vo.getOrganization())) {
        if (StringValidation.isLengthGreaterThan(vo.getOrganization(), 80)) {
            response.addMessage("organization", new TranslatableMessage("validate.notLongerThan", 80));
        }
    }
    if (StringUtils.isNotEmpty(vo.getOrganizationalRole())) {
        if (StringValidation.isLengthGreaterThan(vo.getOrganizationalRole(), 80)) {
            response.addMessage("organizationalRole", new TranslatableMessage("validate.notLongerThan", 80));
        }
    }
    // Every user must have the user role, must be directly assigned otherwise if role inheritance changes the user may lose the role
    if (vo.getRoles() != null && !vo.getRoles().contains(PermissionHolder.USER_ROLE)) {
        Set<Role> updated = new HashSet<>(vo.getRoles());
        updated.add(PermissionHolder.USER_ROLE);
        vo.setRoles(Collections.unmodifiableSet(updated));
    }
    return response;
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) DateTimeException(java.time.DateTimeException) Matcher(java.util.regex.Matcher) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IllformedLocaleException(java.util.IllformedLocaleException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PasswordInvalidException(com.infiniteautomation.mango.spring.service.PasswordService.PasswordInvalidException) HashSet(java.util.HashSet)

Example 2 with PasswordInvalidException

use of com.infiniteautomation.mango.spring.service.PasswordService.PasswordInvalidException in project ma-core-public by MangoAutomation.

the class UsersService method commonValidation.

protected ProcessResult commonValidation(User vo, PermissionHolder holder) {
    ProcessResult response = new ProcessResult();
    if (StringUtils.isBlank(vo.getUsername()))
        response.addMessage("username", new TranslatableMessage("validate.required"));
    if (!UserDao.getInstance().isUsernameUnique(vo.getUsername(), vo.getId()))
        response.addMessage("username", new TranslatableMessage("users.validate.usernameInUse"));
    if (StringUtils.isBlank(vo.getEmail()))
        response.addMessage("email", new TranslatableMessage("validate.required"));
    else if (!UserDao.getInstance().isEmailUnique(vo.getEmail(), vo.getId()))
        response.addMessage("email", new TranslatableMessage("users.validate.emailUnique"));
    if (StringUtils.isBlank(vo.getPassword())) {
        response.addMessage("password", new TranslatableMessage("validate.required"));
    } else {
        Matcher m = Common.EXTRACT_ALGORITHM_HASH.matcher(vo.getPassword());
        if (!m.matches()) {
            response.addMessage("password", new TranslatableMessage("validate.illegalValue"));
        } else {
            String algorithm = m.group(1);
            String hashOrPassword = m.group(2);
            // Validate against our rules
            if (User.PLAIN_TEXT_ALGORITHM.equals(algorithm) || User.NONE_ALGORITHM.equals(algorithm)) {
                if (StringUtils.isBlank(hashOrPassword)) {
                    response.addMessage("password", new TranslatableMessage("validate.required"));
                }
                try {
                    passwordService.validatePassword(hashOrPassword);
                } catch (PasswordInvalidException e) {
                    for (TranslatableMessage message : e.getMessages()) {
                        response.addMessage("password", message);
                    }
                }
            }
        }
    }
    if (StringUtils.isBlank(vo.getName())) {
        response.addMessage("name", new TranslatableMessage("validate.required"));
    } else if (StringValidation.isLengthGreaterThan(vo.getName(), 255)) {
        response.addMessage("name", new TranslatableMessage("validate.notLongerThan", 255));
    }
    // Check field lengths
    if (StringValidation.isLengthGreaterThan(vo.getUsername(), 40))
        response.addMessage("username", new TranslatableMessage("validate.notLongerThan", 40));
    if (StringValidation.isLengthGreaterThan(vo.getEmail(), 255))
        response.addMessage("email", new TranslatableMessage("validate.notLongerThan", 255));
    if (StringValidation.isLengthGreaterThan(vo.getPhone(), 40))
        response.addMessage("phone", new TranslatableMessage("validate.notLongerThan", 40));
    if (vo.getReceiveAlarmEmails() == null) {
        response.addMessage("receiveAlarmEmails", new TranslatableMessage("validate.required"));
    }
    String locale = vo.getLocale();
    if (StringUtils.isNotEmpty(locale)) {
        if (StringValidation.isLengthGreaterThan(locale, 50)) {
            response.addMessage("locale", new TranslatableMessage("validate.notLongerThan", 50));
        }
        try {
            new Locale.Builder().setLanguageTag(locale).build();
        } catch (IllformedLocaleException e) {
            response.addMessage("locale", new TranslatableMessage("validate.invalidValue"));
        }
    }
    String timezone = vo.getTimezone();
    if (StringUtils.isNotEmpty(vo.getTimezone())) {
        if (StringValidation.isLengthGreaterThan(vo.getTimezone(), 50)) {
            response.addMessage("timezone", new TranslatableMessage("validate.notLongerThan", 50));
        }
        try {
            // noinspection ResultOfMethodCallIgnored
            ZoneId.of(timezone);
        } catch (DateTimeException e) {
            response.addMessage("timezone", new TranslatableMessage("validate.invalidValue"));
        }
    }
    // Can't set email verified
    if (vo.getEmailVerifiedDate() != null && !permissionService.hasAdminRole(holder)) {
        response.addContextualMessage("emailVerified", "validate.invalidValue");
    }
    if (StringUtils.isNotEmpty(vo.getOrganization())) {
        if (StringValidation.isLengthGreaterThan(vo.getOrganization(), 80)) {
            response.addMessage("organization", new TranslatableMessage("validate.notLongerThan", 80));
        }
    }
    if (StringUtils.isNotEmpty(vo.getOrganizationalRole())) {
        if (StringValidation.isLengthGreaterThan(vo.getOrganizationalRole(), 80)) {
            response.addMessage("organizationalRole", new TranslatableMessage("validate.notLongerThan", 80));
        }
    }
    // Every user must have the user role, must be directly assigned otherwise if role inheritance changes the user may lose the role
    if (vo.getRoles() != null && !vo.getRoles().contains(PermissionHolder.USER_ROLE)) {
        Set<Role> updated = new HashSet<>(vo.getRoles());
        updated.add(PermissionHolder.USER_ROLE);
        vo.setRoles(Collections.unmodifiableSet(updated));
    }
    return response;
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) DateTimeException(java.time.DateTimeException) Matcher(java.util.regex.Matcher) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IllformedLocaleException(java.util.IllformedLocaleException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PasswordInvalidException(com.infiniteautomation.mango.spring.service.PasswordService.PasswordInvalidException) HashSet(java.util.HashSet)

Aggregations

PasswordInvalidException (com.infiniteautomation.mango.spring.service.PasswordService.PasswordInvalidException)2 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 Role (com.serotonin.m2m2.vo.role.Role)2 DateTimeException (java.time.DateTimeException)2 HashSet (java.util.HashSet)2 IllformedLocaleException (java.util.IllformedLocaleException)2 Matcher (java.util.regex.Matcher)2