From 5bb81614941afd8e9022d2f3e333b52f43d316fa Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Tue, 14 Jul 2020 12:11:01 -0400 Subject: [PATCH] fix(preferences(js)): honor SOGoForwardConstraints in Sieve filters --- .../PreferencesUI/UIxFilterEditor.wox | 4 + .../js/Preferences/FiltersDialogController.js | 113 ++++++++++-------- .../js/Preferences/PreferencesController.js | 68 ++++++----- 3 files changed, 105 insertions(+), 80 deletions(-) diff --git a/UI/Templates/PreferencesUI/UIxFilterEditor.wox b/UI/Templates/PreferencesUI/UIxFilterEditor.wox index f2cd12cc8..604490733 100644 --- a/UI/Templates/PreferencesUI/UIxFilterEditor.wox +++ b/UI/Templates/PreferencesUI/UIxFilterEditor.wox @@ -189,6 +189,10 @@ + +
{{filterEditor.invalid}}
+
-1) - vm.fieldLabels.body = l("Body"); + this.fieldLabels.body = l("Body"); - vm.methodLabels = { + this.methodLabels = { "discard": l("Discard the message"), "keep": l("Keep the message"), "stop": l("Stop processing filter rules") }; if (forwardEnabled) - vm.methodLabels.redirect = l("Forward the message to"); + this.methodLabels.redirect = l("Forward the message to"); //if (vacationEnabled) - // vm.methodLabels.vacation = l("Send a vacation message"); + // this.methodLabels.vacation = l("Send a vacation message"); if (sieveCapabilities.indexOf("reject") > -1) - vm.methodLabels.reject = l("Send a reject message"); + this.methodLabels.reject = l("Send a reject message"); if (sieveCapabilities.indexOf("fileinto") > -1) - vm.methodLabels.fileinto = l("File the message in"); + this.methodLabels.fileinto = l("File the message in"); if (sieveCapabilities.indexOf("imapflags") > -1 || sieveCapabilities.indexOf("imap4flags") > -1) - vm.methodLabels.addflag = l("Flag the message with"); + this.methodLabels.addflag = l("Flag the message with"); - vm.numberOperatorLabels = { + this.numberOperatorLabels = { "under": l("is under"), "over": l("is over") }; - vm.textOperatorLabels = { + this.textOperatorLabels = { "is": l("is"), "is_not": l("is not"), "contains": l("contains"), @@ -74,49 +67,65 @@ }; if (sieveCapabilities.indexOf("regex") > -1) { - vm.textOperatorLabels.regex = l("matches regex"); - vm.textOperatorLabels.regex_not = l("does not match regex"); + this.textOperatorLabels.regex = l("matches regex"); + this.textOperatorLabels.regex_not = l("does not match regex"); } - function cancel() { + this.cancel = function () { $mdDialog.cancel(); - } + }; - function hasRulesAndActions() { - var requirements = [ vm.filter.actions ]; - if (vm.filter.match != 'allmessages') + this.hasRulesAndActions = function () { + var requirements = [ this.filter.actions ]; + if (this.filter.match != 'allmessages') // When matching all messages, no rules are required - requirements.push(vm.filter.rules); + requirements.push(this.filter.rules); return _.every(requirements, function(a) { return a && a.length > 0; }); - } - - function save(form) { + }; + + this.save = function (form) { + var i; + + this.invalid = false; + + // We do some sanity checks + if (this.filter.actions) { + try { + _.forEach(_.filter(this.filter.actions, { 'method': 'redirect' }), function (action) { + validateForwardAddress(action.argument); + }); + } catch (err) { + //Dialog.alert(l('Error'), err); + this.invalid = err.message; + return false; + } + } $mdDialog.hide(); - } + }; - function addMailFilterRule(event) { - if (!vm.filter.rules) - vm.filter.rules = []; + this.addMailFilterRule = function (event) { + if (!this.filter.rules) + this.filter.rules = []; - vm.filter.rules.push({ field: 'subject', operator: 'contains' }); - } - - function removeMailFilterRule(index) { - vm.filter.rules.splice(index, 1); - } - - function addMailFilterAction(event) { - if (!vm.filter.actions) - vm.filter.actions = []; + this.filter.rules.push({ field: 'subject', operator: 'contains' }); + }; - vm.filter.actions.push({ method: 'discard' }); - } + this.removeMailFilterRule = function (index) { + this.filter.rules.splice(index, 1); + }; - function removeMailFilterAction(index) { - vm.filter.actions.splice(index, 1); - } + this.addMailFilterAction = function (event) { + if (!this.filter.actions) + this.filter.actions = []; + + this.filter.actions.push({ method: 'discard' }); + }; + + this.removeMailFilterAction = function (index) { + this.filter.actions.splice(index, 1); + }; } angular diff --git a/UI/WebServerResources/js/Preferences/PreferencesController.js b/UI/WebServerResources/js/Preferences/PreferencesController.js index 94dbf739e..1d51e7bfc 100644 --- a/UI/WebServerResources/js/Preferences/PreferencesController.js +++ b/UI/WebServerResources/js/Preferences/PreferencesController.js @@ -223,7 +223,8 @@ locals: { filter: filter, mailboxes: mailboxes, - labels: this.preferences.defaults.SOGoMailLabelsColors + labels: this.preferences.defaults.SOGoMailLabelsColors, + validateForwardAddress: validateForwardAddress } }).then(function() { if (!vm.preferences.defaults.SOGoSieveFilters) @@ -341,23 +342,18 @@ } }; - this.save = function(form, options) { - var i, sendForm, addresses, defaultAddresses, domains, domain; + function validateForwardAddress(address) { + var defaultAddresses, domains, domain; - sendForm = true; domains = []; - // We do some sanity checks if ($window.forwardConstraints > 0 && - angular.isDefined(this.preferences.defaults.Forward) && - this.preferences.defaults.Forward.enabled && - angular.isDefined(this.preferences.defaults.Forward.forwardAddress)) { - - addresses = this.preferences.defaults.Forward.forwardAddress; + angular.isDefined(Preferences.defaults.Forward) && + Preferences.defaults.Forward.enabled && + angular.isDefined(Preferences.defaults.Forward.forwardAddress)) { // We first extract the list of 'known domains' to SOGo defaultAddresses = $window.defaultEmailAddresses; - _.forEach(defaultAddresses, function(adr) { var domain = adr.split("@")[1]; if (domain) { @@ -366,23 +362,39 @@ }); // We check if we're allowed or not to forward based on the domain defaults - for (i = 0; i < addresses.length && sendForm; i++) { - domain = addresses[i].split("@")[1].toLowerCase(); - if (domains.indexOf(domain) < 0 && $window.forwardConstraints == 1) { - Dialog.alert(l('Error'), l("You are not allowed to forward your messages to an external email address.")); - sendForm = false; - } - else if (domains.indexOf(domain) >= 0 && $window.forwardConstraints == 2) { - Dialog.alert(l('Error'), l("You are not allowed to forward your messages to an internal email address.")); - sendForm = false; - } - else if ($window.forwardConstraints == 2 && - $window.forwardConstraintsDomains.length > 0 && - $window.forwardConstraintsDomains.indexOf(domain) < 0) { - Dialog.alert(l('Error'), l("You are not allowed to forward your messages to this domain:") + " " + domain); - sendForm = false; - } + domain = address.split("@")[1].toLowerCase(); + if (domains.indexOf(domain) < 0 && $window.forwardConstraints == 1) { + throw new Error(l("You are not allowed to forward your messages to an external email address.")); } + else if (domains.indexOf(domain) >= 0 && $window.forwardConstraints == 2) { + throw new Error(l("You are not allowed to forward your messages to an internal email address.")); + } + else if ($window.forwardConstraints == 2 && + $window.forwardConstraintsDomains.length > 0 && + $window.forwardConstraintsDomains.indexOf(domain) < 0) { + throw new Error(l("You are not allowed to forward your messages to this domain:") + " " + domain); + } + } + + return true; + } + + this.save = function(form, options) { + var i, sendForm, addresses; + + sendForm = true; + + // We do some sanity checks + + // We check if we're allowed or not to forward based on the domain defaults + addresses = this.preferences.defaults.Forward.forwardAddress; + try { + for (i = 0; i < addresses.length; i++) { + validateForwardAddress(addresses[i]); + } + } catch (err) { + Dialog.alert(l('Error'), err); + sendForm = false; } // IMAP labels must be unique @@ -441,7 +453,7 @@ } }); - return $q.reject(); + return $q.reject('Invalid form'); }; this.canChangePassword = function() {