|
31 |
# /usr/share/common-licenses/AGPL-3; if not, see |
31 |
# /usr/share/common-licenses/AGPL-3; if not, see |
32 |
# <http://www.gnu.org/licenses/>. |
32 |
# <http://www.gnu.org/licenses/>. |
33 |
|
33 |
|
|
|
34 |
from io import BytesIO |
35 |
|
34 |
from univention.lib.i18n import Translation |
36 |
from univention.lib.i18n import Translation |
35 |
from univention.management.console.base import Base, UMC_Error |
37 |
from univention.management.console.base import Base, UMC_Error |
36 |
from univention.management.console.config import ucr |
38 |
from univention.management.console.config import ucr |
|
46 |
_ = Translation('univention-management-console-module-ucr').translate |
48 |
_ = Translation('univention-management-console-module-ucr').translate |
47 |
|
49 |
|
48 |
|
50 |
|
|
|
51 |
class UCRKeySanitizer(StringSanitizer): |
52 |
|
53 |
def _sanitize(self, value, name, further_arguments): |
54 |
value = super(UCRKeySanitizer, self)._sanitize(value, name, further_arguments) |
55 |
b = BytesIO() |
56 |
if not validate_key(value, b): |
57 |
error_message = b.getvalue() |
58 |
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)) |
59 |
return |
60 |
return value |
61 |
|
62 |
|
49 |
class Instance(Base): |
63 |
class Instance(Base): |
50 |
|
64 |
|
51 |
def init(self): |
65 |
def init(self): |
Lines 88-109
def is_readonly(self, key):
|
Link Here
|
---|
|
88 |
return var.get('readonly') in ('yes', '1', 'true') |
102 |
return var.get('readonly') in ('yes', '1', 'true') |
89 |
return False |
103 |
return False |
90 |
|
104 |
|
|
|
105 |
@sanitize(DictSanitizer({ |
106 |
'object': DictSanitizer({ |
107 |
'key': UCRKeySanitizer(required=True), |
108 |
'value': StringSanitizer(default=''), |
109 |
}) |
110 |
})) |
91 |
def add(self, request): |
111 |
def add(self, request): |
92 |
# does the same as put |
112 |
# does the same as put |
93 |
ucr.load() |
113 |
ucr.load() |
94 |
already_set = set(ucr.keys()) & set(v['object']['key'] for v in request.options) |
114 |
already_set = set(ucr.keys()) & set(v['object']['key'] for v in request.options) |
95 |
if already_set: |
115 |
if already_set: |
96 |
raise UMC_Error(_('The UCR variable %r is already set.') % (', '.join(already_set))) |
116 |
raise UMC_Error(_('The UCR variable %s is already set.') % ('", "'.join(already_set))) |
97 |
|
117 |
|
98 |
self.put(request) |
118 |
self.put(request) |
99 |
|
119 |
|
100 |
@simple_response |
|
|
101 |
def validate(self, key): |
102 |
return validate_key(key) |
103 |
|
104 |
@sanitize(DictSanitizer({ |
120 |
@sanitize(DictSanitizer({ |
105 |
'object': DictSanitizer({ |
121 |
'object': DictSanitizer({ |
106 |
'key': StringSanitizer(required=True), |
122 |
'key': UCRKeySanitizer(required=True), |
107 |
'value': StringSanitizer(default=''), |
123 |
'value': StringSanitizer(default=''), |
108 |
}) |
124 |
}) |
109 |
})) |
125 |
})) |
Lines 112-119
def put(self, request):
|
Link Here
|
---|
|
112 |
var = _var['object'] |
128 |
var = _var['object'] |
113 |
value = var['value'] or '' |
129 |
value = var['value'] or '' |
114 |
key = var['key'] |
130 |
key = var['key'] |
115 |
if not validate_key(key): |
|
|
116 |
raise UMC_Error(_('The UCR variable %r has an invalid name.') % (key,)) |
117 |
if self.is_readonly(key): |
131 |
if self.is_readonly(key): |
118 |
raise UMC_Error(_('The UCR variable %s is read-only and can not be changed!') % (key,)) |
132 |
raise UMC_Error(_('The UCR variable %s is read-only and can not be changed!') % (key,)) |
119 |
arg = ['%s=%s' % (key.encode(), value.encode())] |
133 |
arg = ['%s=%s' % (key.encode(), value.encode())] |