diff --git a/management/univention-management-console-module-ucr/umc/js/ucr.js b/management/univention-management-console-module-ucr/umc/js/ucr.js index 59fa709..6ec7967 100644 --- a/management/univention-management-console-module-ucr/umc/js/ucr.js +++ b/management/univention-management-console-module-ucr/umc/js/ucr.js @@ -106,24 +106,7 @@ define([ label: _( 'Save' ), style: 'float: right', callback: lang.hitch(this, function() { - var keyWidget = this._form.getWidget('key'); - tools.umcpCommand('ucr/validate', {'key': keyWidget.get('value')}).then(lang.hitch(this, function(data) { - var keyIsValid = data.result; - if (keyIsValid) { - // save the UCR variable - keyWidget.setValid(true); - this._form.save(); - this.hide(); - } else { - // show invalid UCR variable message - var invalidMessage = _('A valid key must contain at least one character and can only contain letters, numerals, and "/", ".", ":", "_" and "-".'); - if (keyWidget.get('value').indexOf(': ') !== -1) { - invalidMessage = lang.replace('{0}
{1}', [_('The sequence ": " in the name of a UCR variable is not allowed.'), invalidMessage]); - } - keyWidget.setValid(false, invalidMessage); - keyWidget.focus(); - } - })); + this._form.save(); }) }, { //FIXME: Should be much simpler. The key name should be enough @@ -165,7 +148,10 @@ define([ this._position(); this.standby(false); })); - this._form.on('saved', lang.hitch(this, function() { + this._form.on('saved', lang.hitch(this, function(success) { + if (success) { + this.hide(); + } this._position(); this.standby(false); })); @@ -173,6 +159,8 @@ define([ clearForm: function() { var emptyValues = {}; + this._form.getWidget('key').setValid(true); + this._form.getWidget('value').setValid(true); tools.forIn(this._form.get('value'), function(ikey) { emptyValues[ikey] = ''; }); diff --git a/management/univention-management-console-module-ucr/umc/python/ucr/__init__.py b/management/univention-management-console-module-ucr/umc/python/ucr/__init__.py index 589063f..5ae5ebd 100644 --- a/management/univention-management-console-module-ucr/umc/python/ucr/__init__.py +++ b/management/univention-management-console-module-ucr/umc/python/ucr/__init__.py @@ -31,6 +31,8 @@ # /usr/share/common-licenses/AGPL-3; if not, see # . +from io import BytesIO + from univention.lib.i18n import Translation from univention.management.console.base import Base, UMC_Error from univention.management.console.config import ucr @@ -46,6 +48,18 @@ _ = Translation('univention-management-console-module-ucr').translate +class UCRKeySanitizer(StringSanitizer): + + def _sanitize(self, value, name, further_arguments): + value = super(UCRKeySanitizer, self)._sanitize(value, name, further_arguments) + b = BytesIO() + if not validate_key(value, b): + error_message = b.getvalue() + self.raise_validation_error('%s %s' % (_('A valid UCR variable name must contain at least one character and can only contain letters, numerals, "/", ".", ":", "_" and "-".'), error_message)) + return + return value + + class Instance(Base): def init(self): @@ -88,22 +102,24 @@ def is_readonly(self, key): return var.get('readonly') in ('yes', '1', 'true') return False + @sanitize(DictSanitizer({ + 'object': DictSanitizer({ + 'key': UCRKeySanitizer(required=True), + 'value': StringSanitizer(default=''), + }) + })) def add(self, request): # does the same as put ucr.load() already_set = set(ucr.keys()) & set(v['object']['key'] for v in request.options) if already_set: - raise UMC_Error(_('The UCR variable %r is already set.') % (', '.join(already_set))) + raise UMC_Error(_('The UCR variable %s is already set.') % ('", "'.join(already_set))) self.put(request) - @simple_response - def validate(self, key): - return validate_key(key) - @sanitize(DictSanitizer({ 'object': DictSanitizer({ - 'key': StringSanitizer(required=True), + 'key': UCRKeySanitizer(required=True), 'value': StringSanitizer(default=''), }) })) @@ -112,8 +128,6 @@ def put(self, request): var = _var['object'] value = var['value'] or '' key = var['key'] - if not validate_key(key): - raise UMC_Error(_('The UCR variable %r has an invalid name.') % (key,)) if self.is_readonly(key): raise UMC_Error(_('The UCR variable %s is read-only and can not be changed!') % (key,)) arg = ['%s=%s' % (key.encode(), value.encode())] diff --git a/management/univention-management-console-module-ucr/umc/ucr.xml b/management/univention-management-console-module-ucr/umc/ucr.xml index 3cb3fc4..1975b21 100644 --- a/management/univention-management-console-module-ucr/umc/ucr.xml +++ b/management/univention-management-console-module-ucr/umc/ucr.xml @@ -9,7 +9,6 @@ -