Removed the eval from the code

... and changed it to make it work with the latest Netbox version.
This commit is contained in:
Christian Mäder 2020-01-31 11:39:05 +01:00
parent ba3176f140
commit 69ef7b7827
2 changed files with 20 additions and 22 deletions

View File

@ -20,25 +20,23 @@ with file.open('r') as stream:
username = username, username = username,
password = user_details.get('password', 0) or User.objects.make_random_password) password = user_details.get('password', 0) or User.objects.make_random_password)
print("👤 Created user ",username) print("👤 Created user",username)
if user_details.get('api_token', 0): if user_details.get('api_token', 0):
Token.objects.create(user=user, key=user_details['api_token']) Token.objects.create(user=user, key=user_details['api_token'])
yaml_permissions = user_details.get('permissions', []) yaml_permissions = user_details.get('permissions', [])
permission_object = user subject = user.user_permissions
if yaml_permissions: if yaml_permissions:
permission_object.permissions.clear() subject.clear()
for yaml_permission in yaml_permissions: for yaml_permission in yaml_permissions:
if '*' in yaml_permission: if '*' in yaml_permission:
permission_codename_function = 'codename__iregex' permission_filter = '^' + yaml_permission.replace('*','.*') + '$'
permission_codename = '^' + yaml_permission.replace('*','.*') + '$' permissions = Permission.objects.filter(codename__iregex=permission_filter)
print(" ⚿ Granting", permissions.count(), "permissions matching '" + yaml_permission + "'")
else: else:
permission_codename_function = 'codename' permissions = Permission.objects.filter(codename=yaml_permission)
permission_codename = yaml_permission print(" ⚿ Granting permission", yaml_permission)
# supports non-unique permission codenames for permission in permissions:
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'): subject.add(permission)
permission_object.permissions.add(permission)
permission_object.save()

View File

@ -25,17 +25,17 @@ with file.open('r') as stream:
user.groups.add(group) user.groups.add(group)
yaml_permissions = group_details.get('permissions', []) yaml_permissions = group_details.get('permissions', [])
permission_object = group subject = group.permissions
if yaml_permissions: if yaml_permissions:
permission_object.permissions.clear() subject.clear()
for yaml_permission in yaml_permissions: for yaml_permission in yaml_permissions:
if '*' in yaml_permission: if '*' in yaml_permission:
permission_codename_function = 'codename__iregex' permission_filter = '^' + yaml_permission.replace('*','.*') + '$'
permission_codename = '^' + yaml_permission.replace('*','.*') + '$' permissions = Permission.objects.filter(codename__iregex=permission_filter)
print(" ⚿ Granting", permissions.count(), "permissions matching '" + yaml_permission + "'")
else: else:
permission_codename_function = 'codename' permissions = Permission.objects.filter(codename=yaml_permission)
permission_codename = yaml_permission print(" ⚿ Granting permission", yaml_permission)
# supports non-unique permission codenames for permission in permissions:
for permission in eval('Permission.objects.filter(' + permission_codename_function + '=permission_codename)'): subject.add(permission)
permission_object.permissions.add(permission)