simplify yml definitions to use wildcard syntax

This commit is contained in:
Les Begnaud 2019-12-11 10:14:38 -06:00 committed by Christian Mäder
parent cce4370d41
commit aa0d2a6e01
4 changed files with 25 additions and 36 deletions

View File

@ -7,19 +7,15 @@
# writers:
# users:
# - writer
## specify explicit permission codenames or codename filter functions and filters to match on
## specify explicit permission codenames or include wildcard to match multiple permissions
# permissions:
# - delete_device
# - delete_virtualmachine
# - codename__startswith:
# - add_
# - change_
# - add_*
# - change_*
# vm_managers:
# - codename__endswith:
# - _virtualmachine
# - *_virtualmachine
# device_managers:
# - codename__contains:
# - device
# - *device*
# creators:
# - codename__startswith:
# - add_
# - add_*

View File

@ -4,10 +4,9 @@
# password: reader
# writer:
# password: writer
## specify explicit permission codenames or codename filter functions and filters to match on
## specify explicit permission codenames or include wildcard to match multiple permissions
# permissions:
# - delete_device
# - delete_virtualmachine
# - codename__startswith:
# - add_
# - change_
# - add_*
# - change_*

View File

@ -30,18 +30,15 @@ with file.open('r') as stream:
if yaml_permissions:
permission_object.permissions.clear()
for yaml_permission in yaml_permissions:
if isinstance(yaml_permission,dict):
# assume this is the specific codename filter function instead of an exact codename
permission_codename_function = list(yaml_permission.keys())[0]
permission_codenames = yaml_permission[permission_codename_function]
if '*' in yaml_permission:
permission_codename_function = 'codename__iregex'
permission_codename = '^' + yaml_permission.replace('*','.*') + '$'
else:
permission_codename_function = 'codename'
permission_codenames = list({yaml_permission})
# supports either one codename from the permissions list, or multiple codenames in a codename_function dict
for permission_codename in permission_codenames:
# supports non-unique permission codenames
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'):
permission_object.permissions.add(permission)
permission_codename = yaml_permission
# supports non-unique permission codenames
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'):
permission_object.permissions.add(permission)
permission_object.save()

View File

@ -29,16 +29,13 @@ with file.open('r') as stream:
if yaml_permissions:
permission_object.permissions.clear()
for yaml_permission in yaml_permissions:
if isinstance(yaml_permission,dict):
# assume this is the specific codename filter function instead of an exact codename
permission_codename_function = list(yaml_permission.keys())[0]
permission_codenames = yaml_permission[permission_codename_function]
if '*' in yaml_permission:
permission_codename_function = 'codename__iregex'
permission_codename = '^' + yaml_permission.replace('*','.*') + '$'
else:
permission_codename_function = 'codename'
permission_codenames = list({yaml_permission})
# supports either one codename from the permissions list, or multiple codenames in a codename_function dict
for permission_codename in permission_codenames:
# supports non-unique permission codenames
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'):
permission_object.permissions.add(permission)
permission_codename = yaml_permission
# supports non-unique permission codenames
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'):
permission_object.permissions.add(permission)