Skip to content

Commit 288b18f

Browse files
committed
Basic functionality for saving the parent model
1 parent 7760df4 commit 288b18f

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

ansible_base/rbac/management/create_types.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import logging
12
from typing import Type
23

34
from django.apps import apps as global_apps
45
from django.db import DEFAULT_DB_ALIAS, models
56

67
from ansible_base.rbac import permission_registry
78

9+
logger = logging.getLogger(__name__)
10+
811

912
def get_local_DAB_contenttypes(using: str, ct_model: Type[models.Model], service: str) -> dict[tuple[str, str], models.Model]:
1013
# This should work in migration scenarios, but other code checks for existence of it on manager
@@ -44,4 +47,14 @@ def create_DAB_contenttypes(
4447
DABContentType.objects.using(using).bulk_create(cts)
4548
if verbosity >= 2:
4649
for ct in cts:
47-
print("Adding DAB content type " f"'{ct.service}:{ct.app_label} | {ct.model}'")
50+
logger.debug("Adding DAB content type " f"'{ct.service}:{ct.app_label} | {ct.model}'")
51+
52+
for ct in DABContentType.objects.all():
53+
if not permission_registry.is_registered(ct.model_class()):
54+
logger.warning(f'{ct.model} is a stale content type in DAB RBAC')
55+
continue
56+
if parent_model := permission_registry.get_parent_model(ct.model_class()):
57+
parent_content_type = DABContentType.objects.get_for_model(parent_model)
58+
if ct.parent_content_type != parent_content_type:
59+
ct.parent_content_type = parent_content_type
60+
ct.save(update_fields=['parent_content_type'])

ansible_base/rbac/migrations/0004_remote_permissions_additions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class Migration(migrations.Migration):
2727
(
2828
'app_label',
2929
models.CharField(
30-
help_text='Django app that the model is in. This is an internal technical detail that does not affect API use.', max_length=100
30+
help_text='Django app that the model is in. This is an internal technical detail that does not affect API use.',
31+
max_length=100
3132
),
3233
),
3334
(
@@ -37,6 +38,15 @@ class Migration(migrations.Migration):
3738
max_length=100,
3839
),
3940
),
41+
(
42+
'parent_content_type',
43+
models.ForeignKey(
44+
help_text='Parent model within the RBAC system. Being assigned to a role in objects of the parent model can confer permissions to child objects.',
45+
null=True,
46+
on_delete=models.deletion.SET_NULL,
47+
to='dab_rbac.dabcontenttype'
48+
)
49+
)
4050
],
4151
options={
4252
'unique_together': {('service', 'app_label', 'model')},

ansible_base/rbac/models/content_type.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ class DABContentType(django_models.Model):
157157
max_length=100,
158158
help_text=_("Name of the type according to the Django ORM Meta model_name convention. Comes from the python class, but lowercase with no spaces."),
159159
)
160+
parent_content_type = django_models.ForeignKey(
161+
"self",
162+
null=True,
163+
help_text=_("Parent model within the RBAC system. Being assigned to a role in objects of the parent model can confer permissions to child objects."),
164+
on_delete=django_models.SET_NULL,
165+
)
160166

161167
objects = DABContentTypeManager()
162168

0 commit comments

Comments
 (0)