1
0
mirror of https://github.com/Yubico/yubiadmin.git synced 2024-11-29 10:24:11 +01:00

Started work on adding lists of items to forms.

This commit is contained in:
Dain Nilsson 2013-04-25 18:36:50 +02:00
parent a2037d8c63
commit f3c6be25e6
2 changed files with 116 additions and 28 deletions

View File

@ -1,4 +1,6 @@
from wtforms.fields import IntegerField import re
from wtforms.fields import IntegerField, StringField, Field
from wtforms.widgets import TextInput
from wtforms.validators import NumberRange from wtforms.validators import NumberRange
from yubiadmin.util import App, DBConfigForm, ConfigForm, FileConfig from yubiadmin.util import App, DBConfigForm, ConfigForm, FileConfig
@ -7,28 +9,25 @@ __all__ = [
] ]
def yk_read(varname, prefix='', suffix='', flags=None):
regex = r'(?m)^(?!#)\$baseParams\[\'__YKVAL_%s__\'\]\s*=' \
'\s*%s(.*?)%s\s*;\s*$' % (varname, prefix, suffix)
if flags:
regex = '(?%s)' % flags + regex
return regex
def yk_write(varname, prefix='', suffix=''):
return lambda x: '$baseParams[\'__YKVAL_%s__\'] = %s%s%s;' % \
(varname, prefix, x, suffix)
def yk_read_str(varname): def yk_read_str(varname):
return r'\$baseParams\[\'__YKVAL_%s__\'\] = [\'"](.*)[\'"];' % varname return yk_read(varname, '[\'"]', '[\'"]')
def yk_write_str(varname): def yk_write_str(varname):
return lambda x: '$baseParams[\'__YKVAL_%s__\'] = "%s";' % (varname, x) return yk_write(varname, '"', '"')
def yk_read_int(varname):
return r'\$baseParams\[\'__YKVAL_%s__\'\] = (\d+);' % varname
def yk_write_int(varname):
return lambda x: '$baseParams[\'__YKVAL_%s__\'] = %s;' % (varname, x)
def db_read(varname):
return r'\$db%s=\'(.*)\';' % varname
def db_write(varname):
return lambda x: '$db%s=\'%s\';' % (varname, x)
class SyncLevelsForm(ConfigForm): class SyncLevelsForm(ConfigForm):
@ -43,18 +42,18 @@ class SyncLevelsForm(ConfigForm):
[ [
( (
'sync_default', 'sync_default',
yk_read_int('SYNC_DEFAULT_LEVEL'), yk_read('SYNC_DEFAULT_LEVEL'),
yk_write_int('SYNC_DEFAULT_LEVEL'), yk_write('SYNC_DEFAULT_LEVEL'),
60 60
), ( ), (
'sync_secure', 'sync_secure',
yk_read_int('SYNC_SECURE_LEVEL'), yk_read('SYNC_SECURE_LEVEL'),
yk_write_int('SYNC_SECURE_LEVEL'), yk_write('SYNC_SECURE_LEVEL'),
40 40
), ( ), (
'sync_fast', 'sync_fast',
yk_read_int('SYNC_FAST_LEVEL'), yk_read('SYNC_FAST_LEVEL'),
yk_write_int('SYNC_FAST_LEVEL'), yk_write('SYNC_FAST_LEVEL'),
1 1
), ),
@ -70,13 +69,96 @@ class MiscForm(ConfigForm):
'/home/dain/yubico/yubiadmin/ykval-config.php', '/home/dain/yubico/yubiadmin/ykval-config.php',
[( [(
'default_timeout', 'default_timeout',
yk_read_int('SYNC_DEFAULT_TIMEOUT'), yk_read('SYNC_DEFAULT_TIMEOUT'),
yk_write_int('SYNC_DEFAULT_TIMEOUT'), yk_write('SYNC_DEFAULT_TIMEOUT'),
1 1
)] )]
) )
class ListField(Field):
COMMENT = re.compile(r'/\*.*?\*/')
VALUE = re.compile(r'\s*[\'"](.*)[\'"]\s*')
widget = TextInput()
def process_formdata(self, values):
if values:
self.data = filter(None, [x.strip() for x in values[0].split(',')])
def process_data(self, value):
if value:
data = []
value = self.COMMENT.sub('', value)
for val in value.split(','):
match = self.VALUE.match(val)
if match:
data.append(match.group(1))
self.data = data
else:
self.data = []
def _value(self):
if self.data:
return ', '.join(self.data)
else:
return ''
def yk_array_write(varname):
str_write = yk_write(varname, 'array(', ')')
return lambda xs: str_write(', '.join(['"%s"' % x for x in xs]))
class SyncPoolForm(ConfigForm):
legend = 'Sync Settings'
sync_interval = IntegerField('Sync Interval', [NumberRange(1)])
resync_timeout = IntegerField('Resync Timeout', [NumberRange(1)])
old_limit = IntegerField('Old Limit', [NumberRange(1)])
sync_pool = ListField('Servers')
sync_pool_add = StringField('Add Server')
config = FileConfig(
'/home/dain/yubico/yubiadmin/ykval-config.php',
[
(
'sync_interval',
yk_read('SYNC_INTERVAL'),
yk_write('SYNC_INTERVAL'),
10
), (
'resync_timeout',
yk_read('SYNC_RESYNC_TIMEOUT'),
yk_write('SYNC_RESYNC_TIMEOUT'),
30
), (
'old_limit',
yk_read('SYNC_OLD_LIMIT'),
yk_write('SYNC_OLD_LIMIT'),
10
), (
'sync_pool',
yk_read('SYNC_POOL', 'array\(', '\)', 's'),
yk_array_write('SYNC_POOL'),
''
)
]
)
def validate(self):
if super(SyncPoolForm, self).validate():
if self.sync_pool_add.data:
self.sync_pool.data.append(self.sync_pool_add.data)
self.sync_pool_add.process_data(None)
return True
return False
COMMENT = re.compile(r'/\*.*?\*/')
def remove_comments(content):
return COMMENT.sub('', content)
class YubikeyVal(App): class YubikeyVal(App):
""" """
YubiKey Validation Server YubiKey Validation Server
@ -104,7 +186,12 @@ class YubikeyVal(App):
""" """
Sync pool Sync pool
""" """
return 'Not yet implemented.' sync_pool_form = SyncPoolForm()
form_page = self.render_forms(request, [sync_pool_form])
print 'Sync pool: %s' % \
remove_comments(sync_pool_form.config['sync_pool'])
return form_page
def ksms(self, request): def ksms(self, request):
""" """

View File

@ -77,6 +77,7 @@ class ValueHandler(object):
class FileConfig(DictMixin): class FileConfig(DictMixin):
""" """
Maps key-value pairs to a backing config file. Maps key-value pairs to a backing config file.
You can manually edit the file by modifying self.content.
""" """
def __init__(self, filename, params=[]): def __init__(self, filename, params=[]):
self.filename = filename self.filename = filename