private void readAgreements()

in termsOfService-server/src/main/java/jetbrains/buildServer/termsOfService/TermsOfServiceManagerImpl.java [72:178]


    private void readAgreements(@NotNull Element config) {
        myAgreements.clear();
        List agreementElements = config.getChildren("agreement");
        if (agreementElements.isEmpty()) {
            TermsOfServiceLogger.LOGGER.warn("Broken configuration: no 'agreement' elements found in " + myConfig.getMainConfig());
        }

        for (Object agreementElObj : agreementElements) {
            Element agreementEl = (Element) agreementElObj;

            Element paramsElement = agreementEl.getChild("parameters");
            Map<String, String> params = paramsElement == null ? emptyMap() : XmlUtil.readParameters(paramsElement);
            String agreementId = agreementEl.getAttributeValue("id");
            String agreementFileParam = params.get("content-file");

            if (StringUtil.isEmptyOrSpaces(agreementId)) {
                TermsOfServiceLogger.LOGGER.warn("Broken configuration: missing agreement id, the agreement is ignored.");
                continue;
            }

            if (StringUtil.isEmptyOrSpaces(agreementFileParam)) {
                TermsOfServiceLogger.LOGGER.warn("Broken configuration: missing 'content-file' parameter for agreement id = '" + agreementId + "', the agreement is ignored.");
                continue;
            }

            if (isEnabled(agreementEl)) {
                TermsOfServiceLogger.LOGGER.info("Agreement '" + agreementId + "' is disabled, to enable change 'enabled' attribute value to 'true'");
                continue;
            }


            File agreementFile = myConfig.getConfigFile(agreementFileParam);

            if (!FileUtil.isAncestor(myConfig.getConfigDir(), agreementFile, false)) {
                TermsOfServiceLogger.LOGGER.warn("Agreement file '" + agreementFile + "' is outside of the allowed directory '" + myConfig.getConfigDir() + "', the agreement is ignored.");
                continue;
            }

            String agreementContent;

            if (agreementFile.exists() && agreementFile.isFile()) {
                try {
                    agreementContent = FileUtil.readText(agreementFile, "UTF-8");
                } catch (IOException e) {
                    TermsOfServiceLogger.LOGGER.warnAndDebugDetails("Error while reading agreement file from " + agreementFile + " for agreement id = '" + agreementId + "'", e);
                    continue;
                }
            } else {
                TermsOfServiceLogger.LOGGER.warn("Broken configuration: agreement file '" + agreementFile + "' doesn't exist for agreement id = '" + agreementId + "'");
                continue;
            }


            List<Consent> consents = new ArrayList<>();
            Element consentsEl = agreementEl.getChild("consents");
            if (consentsEl != null) {
                for (Object consent : consentsEl.getChildren("consent")) {
                    Element consentEl = ((Element) consent);
                    String id = consentEl.getAttributeValue("id");
                    String html;

                    if (consentEl.getAttributeValue("file") != null) {
                        File consentContentFile = myConfig.getConfigFile(consentEl.getAttributeValue("file"));
                        if (consentContentFile.exists() && consentContentFile.isFile()) {
                            try {
                                html = FileUtil.readText(consentContentFile, "UTF-8");
                            } catch (IOException e) {
                                TermsOfServiceLogger.LOGGER.warnAndDebugDetails("Error while reading consent file from " + consentContentFile + " for agreement id = '" + agreementId + "'", e);
                                continue;
                            }
                        } else {
                            TermsOfServiceLogger.LOGGER.warn("Broken configuration: consent file '" + consentContentFile + "' doesn't exist for agreement id = '" + agreementId + "', consent is skipped");
                            continue;
                        }
                    } else {
                        html = consentEl.getAttributeValue("text");
                    }

                    if (StringUtil.isEmptyOrSpaces(id)) {
                        TermsOfServiceLogger.LOGGER.warn("Broken configuration: missing consent id, the consent is ignored.");
                        continue;
                    }

                    if (StringUtil.isEmptyOrSpaces(html)) {
                        TermsOfServiceLogger.LOGGER.warn("Broken configuration: missing consent text/file, the consent is ignored.");
                        continue;
                    }

                    boolean checked = Boolean.parseBoolean(consentEl.getAttributeValue("default"));
                    consents.add(new ConsentImpl(id, html, checked, agreementId));
                }
            }

            Predicate<SUser> userPredicate = user -> true;
            String usersFilter = agreementEl.getAttributeValue("user-filter");
            if (usersFilter != null) {
                if (!usersFilter.startsWith("username:")) {
                    TermsOfServiceLogger.LOGGER.warn("Broken configuration: unsupported user filter '" + usersFilter + "'. " +
                            "Currently only username filters are supported, for example 'username:admin'.");
                } else {
                    userPredicate = user -> user.getUsername().equals(usersFilter.substring("username:".length()));
                }
            }

            myAgreements.add(new AgreementImpl(agreementId, agreementContent, params, consents, userPredicate));
        }
    }