Skip to content

Commit 474ca9e

Browse files
committed
fully working object permissions
1 parent 12401f2 commit 474ca9e

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

initializers/object_permissions.yml

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
1-
#- name: all.ro
2-
# description: 'Read Only for All Objects'
3-
# enabled: true
4-
# # object_types: all
5-
# groups:
6-
# - applications
7-
# - readers
8-
# actions:
9-
# - view
10-
#- name: all.rw
11-
# description: 'Read/Write for All Objects'
12-
# enabled: true
13-
# # object_types: all
14-
# groups:
15-
# - writers
16-
# users:
17-
# - jdoe
18-
# actions:
19-
# - add
20-
# - change
21-
# - delete
22-
# - view
1+
# all.ro:
2+
# actions:
3+
# - view
4+
# description: 'Read Only for All Objects'
5+
# enabled: true
6+
# groups:
7+
# - applications
8+
# - readers
9+
# object_types: all
10+
# users:
11+
# - jdoe
12+
# all.rw:
13+
# actions:
14+
# - add
15+
# - change
16+
# - delete
17+
# - view
18+
# description: 'Read/Write for All Objects'
19+
# enabled: true
20+
# groups:
21+
# - writers
22+
# object_types: all
23+
# network_team.rw:
24+
# actions:
25+
# - add
26+
# - change
27+
# - delete
28+
# - view
29+
# description: "Network Team Permissions"
30+
# enabled: true
31+
# object_types:
32+
# circuits:
33+
# - circuit
34+
# - circuittermination
35+
# - circuittype
36+
# - provider
37+
# dcim: all
38+
# ipam:
39+
# - aggregate
40+
# - ipaddress
41+
# - prefix
42+
# - rir
43+
# - role
44+
# - routetarget
45+
# - service
46+
# - vlan
47+
# - vlangroup
48+
# - vrf

startup_scripts/015_object_permissions.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@
1919
actions=permission_details["actions"],
2020
)
2121

22-
# Need to try to pass a list of model_name and app_label for more than the current ALL
23-
# object_types = ContentType.objects.filter(app_label__in=permission_details["object_types"])
24-
# object_permission.object_types.set(ContentType.objects.filter(app_label__in=permission_details"object_types"]))
25-
object_permission.object_types.set(ContentType.objects.all())
26-
object_permission.save()
22+
if permission_details.get("object_types", 0):
23+
object_types = permission_details["object_types"]
24+
25+
if object_types == "all":
26+
object_permission.object_types.set(ContentType.objects.all())
27+
28+
else:
29+
for app_label, models in object_types.items():
30+
if models == "all":
31+
app_models = ContentType.objects.filter(app_label=app_label)
32+
33+
for app_model in app_models:
34+
object_permission.object_types.add(app_model.id)
35+
else:
36+
# There is
37+
for model in models:
38+
object_permission.object_types.add(
39+
ContentType.objects.get(app_label=app_label, model=model)
40+
)
2741

2842
print("🔓 Created object permission", object_permission.name)
2943

0 commit comments

Comments
 (0)