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 503b6f2..f6418d0 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 @@ -32,13 +32,14 @@ # . from univention.lib.i18n import Translation -from univention.management.console.modules import Base +from univention.management.console.modules import Base, UMC_Error +from univention.management.console.config import ucr from univention.management.console.protocol.definitions import BAD_REQUEST_INVALID_OPTS, SUCCESS from univention.management.console.modules.decorators import simple_response, sanitize from univention.management.console.modules.sanitizers import PatternSanitizer, ChoicesSanitizer -import univention.config_registry as ucr +from univention.config_registry import handler_set, handler_unset, ConfigRegistry, validate_key from univention.config_registry_info import ConfigRegistryInfo, Variable import univention.info_tools as uit @@ -87,7 +88,12 @@ def is_readonly( self, key ): def add( self, request ): # does the same as put - self.put( request ) + ucr.load() + already_set = set(ucr.keys()) & set(v['object']['key'] for v in request.options.values()) + if already_set: + raise UMC_Error(_('The keys %r are already set.') % (', '.join(already_set)) + + self.put(request) def put( self, request ): message = '' @@ -99,24 +105,19 @@ 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 ): - success = False - message = _( 'The UCR variable %s is read-only and can not be changed!' ) % key - break + raise UMC_Error(_('The UCR variable %s is read-only and can not be changed!') % (key,)) arg = [ '%s=%s' % ( key.encode(), value.encode() ) ] - ucr.handler_set( arg ) + handler_set( arg ) # handle descriptions, type, and categories if 'descriptions' in var or 'type' in var or 'categories' in var: self.__create_variable_info( var ) except KeyError: # handle the case that neither key nor value are given for an UCR variable entry - request.status = BAD_REQUEST_INVALID_OPTS - self.finished(request.id, False, message = _('Invalid UCR variable entry, the properties "key" and "value" need to be specified.')) - return - else: - success = False - request.status = BAD_REQUEST_INVALID_OPTS + raise UMC_Error(_('Invalid UCR variable entry, the properties "key" and "value" need to be specified.'))) self.finished( request.id, success, message ) @@ -128,11 +129,11 @@ def remove( self, request ): self.finished( request.id, False, message ) return - ucr.handler_unset( variables ) + handler_unset( variables ) self.finished( request.id, True ) def get( self, request ): - ucrReg = ucr.ConfigRegistry() + ucrReg = ConfigRegistry() ucrReg.load() ucrInfo = ConfigRegistryInfo( registered_only = False )