Skip to content

Commit 3717b74

Browse files
authored
Merge pull request #236 from netbox-community/LBegnaud-master
Permission Wildcards
2 parents 0574ffc + 3d80cc5 commit 3717b74

File tree

5 files changed

+75
-29
lines changed

5 files changed

+75
-29
lines changed

initializers/groups.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## To list all permissions, run:
2+
##
3+
## docker-compose run --rm --entrypoint /bin/bash netbox
4+
## $ ./manage.py migrate
5+
## $ ./manage.py shell
6+
## > from django.contrib.auth.models import Permission
7+
## > print('\n'.join([p.codename for p in Permission.objects.all()]))
8+
##
9+
## Permission lists support wildcards. See the examples below.
10+
##
11+
## Examples:
12+
113
# applications:
214
# users:
315
# - technical_user
@@ -8,9 +20,16 @@
820
# users:
921
# - writer
1022
# permissions:
11-
# - add_device
12-
# - change_device
1323
# - delete_device
14-
# - add_virtualmachine
15-
# - change_virtualmachine
1624
# - delete_virtualmachine
25+
# - add_*
26+
# - change_*
27+
# vm_managers:
28+
# permissions:
29+
# - '*_virtualmachine'
30+
# device_managers:
31+
# permissions:
32+
# - '*device*'
33+
# creators:
34+
# permissions:
35+
# - add_*

initializers/users.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
## To list all permissions, run:
2+
##
3+
## docker-compose run --rm --entrypoint /bin/bash netbox
4+
## $ ./manage.py migrate
5+
## $ ./manage.py shell
6+
## > from django.contrib.auth.models import Permission
7+
## > print('\n'.join([p.codename for p in Permission.objects.all()]))
8+
##
9+
## Permission lists support wildcards. See the examples below.
10+
##
11+
## Examples:
12+
113
# technical_user:
214
# api_token: 0123456789technicaluser789abcdef01234567 # must be looooong!
315
# reader:
416
# password: reader
517
# writer:
618
# password: writer
719
# permissions:
8-
# - add_device
9-
# - change_device
1020
# - delete_device
11-
# - add_virtualmachine
12-
# - change_virtualmachine
1321
# - delete_virtualmachine
22+
# - add_*
23+
# - change_*

startup_scripts/000_users.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@
2020
username = username,
2121
password = user_details.get('password', 0) or User.objects.make_random_password)
2222

23-
print("👤 Created user ",username)
23+
print("👤 Created user",username)
2424

2525
if user_details.get('api_token', 0):
2626
Token.objects.create(user=user, key=user_details['api_token'])
2727

28-
user_permissions = user_details.get('permissions', [])
29-
if user_permissions:
30-
user.user_permissions.clear()
31-
for permission_codename in user_details.get('permissions', []):
32-
for permission in Permission.objects.filter(codename=permission_codename):
33-
user.user_permissions.add(permission)
34-
user.save()
28+
yaml_permissions = user_details.get('permissions', [])
29+
if yaml_permissions:
30+
subject = user.user_permissions
31+
subject.clear()
32+
for yaml_permission in yaml_permissions:
33+
if '*' in yaml_permission:
34+
permission_filter = '^' + yaml_permission.replace('*','.*') + '$'
35+
permissions = Permission.objects.filter(codename__iregex=permission_filter)
36+
print(" ⚿ Granting", permissions.count(), "permissions matching '" + yaml_permission + "'")
37+
else:
38+
permissions = Permission.objects.filter(codename=yaml_permission)
39+
print(" ⚿ Granting permission", yaml_permission)
40+
41+
for permission in permissions:
42+
subject.add(permission)

startup_scripts/010_groups.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@
2424
if user:
2525
user.groups.add(group)
2626

27-
group_permissions = group_details.get('permissions', [])
28-
if group_permissions:
29-
group.permissions.clear()
30-
for permission_codename in group_details.get('permissions', []):
31-
for permission in Permission.objects.filter(codename=permission_codename):
32-
group.permissions.add(permission)
27+
yaml_permissions = group_details.get('permissions', [])
28+
if yaml_permissions:
29+
subject = group.permissions
30+
subject.clear()
31+
for yaml_permission in yaml_permissions:
32+
if '*' in yaml_permission:
33+
permission_filter = '^' + yaml_permission.replace('*','.*') + '$'
34+
permissions = Permission.objects.filter(codename__iregex=permission_filter)
35+
print(" ⚿ Granting", permissions.count(), "permissions matching '" + yaml_permission + "'")
36+
else:
37+
permissions = Permission.objects.filter(codename=yaml_permission)
38+
print(" ⚿ Granting permission", yaml_permission)
39+
40+
for permission in permissions:
41+
subject.add(permission)

startup_scripts/__main__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
this_dir = dirname(abspath(__file__))
88

99
def filename(f):
10-
return f.name
10+
return f.name
1111

1212
with scandir(dirname(abspath(__file__))) as it:
13-
for f in sorted(it, key = filename):
14-
if f.name.startswith('__') or not f.is_file():
15-
continue
16-
17-
print(f"Running {f.path}")
18-
runpy.run_path(f.path)
13+
for f in sorted(it, key = filename):
14+
if f.name.startswith('__') or not f.is_file():
15+
continue
16+
17+
print(f"Running {f.path}")
18+
runpy.run_path(f.path)

0 commit comments

Comments
 (0)