Add permissions to user/group initializers

Thank you @bdlamprecht for researching how to add permissions to
users and groups in #92.

Fixes #92
This commit is contained in:
Christian Mäder 2018-10-13 17:44:01 +02:00
parent f1ebd4d246
commit 4053a714f8
5 changed files with 42 additions and 3 deletions

View File

@ -188,6 +188,15 @@ text_field:
[netbox-docker-initializers]: https://github.com/ninech/netbox-docker/tree/master/initializers [netbox-docker-initializers]: https://github.com/ninech/netbox-docker/tree/master/initializers
[netbox-docker-compose]: https://github.com/ninech/netbox-docker/blob/master/docker-compose.yml [netbox-docker-compose]: https://github.com/ninech/netbox-docker/blob/master/docker-compose.yml
##### Available Groups for User/Group initializers
To get an up-to-date list about all the available permissions, run the following command.
```bash
# Make sure the 'netbox' container is already running! If unsure, run `docker-compose up -d`
echo "from django.contrib.auth.models import Permission\nfor p in Permission.objects.all():\n print(p.codename);" | docker-compose exec -T netbox ./manage.py shell
```
#### Custom Docker Image #### Custom Docker Image
You can also build your own NetBox Docker image containing your own startup scripts, custom fields, users and groups You can also build your own NetBox Docker image containing your own startup scripts, custom fields, users and groups

View File

@ -7,3 +7,10 @@
# writers: # writers:
# users: # users:
# - writer # - writer
# permissions:
# - add_device
# - change_device
# - delete_device
# - add_virtualmachine
# - change_virtualmachine
# - delete_virtualmachine

View File

@ -4,3 +4,10 @@
# password: reader # password: reader
# writer: # writer:
# password: writer # password: writer
# permissions:
# - add_device
# - change_device
# - delete_device
# - add_virtualmachine
# - change_virtualmachine
# - delete_virtualmachine

View File

@ -1,4 +1,4 @@
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Permission, Group, User
from users.models import Token from users.models import Token
from ruamel.yaml import YAML from ruamel.yaml import YAML
@ -18,3 +18,11 @@ with open('/opt/netbox/initializers/users.yml', 'r') as stream:
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'])
user_permissions = user_details.get('permissions', [])
if user_permissions:
user.user_permissions.clear()
for permission_codename in user_details.get('permissions', []):
permission = Permission.objects.get(codename=permission_codename)
user.user_permissions.add(permission)
user.save()

View File

@ -1,4 +1,4 @@
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Permission, Group, User
from ruamel.yaml import YAML from ruamel.yaml import YAML
with open('/opt/netbox/initializers/groups.yml', 'r') as stream: with open('/opt/netbox/initializers/groups.yml', 'r') as stream:
@ -12,8 +12,16 @@ with open('/opt/netbox/initializers/groups.yml', 'r') as stream:
if created: if created:
print("👥 Created group", groupname) print("👥 Created group", groupname)
for username in group_details['users']: for username in group_details.get('users', []):
user = User.objects.get(username=username) user = User.objects.get(username=username)
if user: if user:
user.groups.add(group) user.groups.add(group)
group_permissions = group_details.get('permissions', [])
if group_permissions:
group.permissions.clear()
print("Permissions:", group.permissions.all())
for permission_codename in group_details.get('permissions', []):
permission = Permission.objects.get(codename=permission_codename)
group.permissions.add(permission)