Skip to content

Commit

Permalink
Allow role assignment queries to use .only (#482)
Browse files Browse the repository at this point in the history
Fixes #449
  • Loading branch information
AlanCoding authored Nov 15, 2024
1 parent 595937b commit dc5ff4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ansible_base/rbac/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ class Meta:
app_label = 'dab_rbac'
abstract = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Cache fields from the associated object_role
if self.object_role_id and not self.object_id:
self.object_id = self.object_role.object_id
self.content_type_id = self.object_role.content_type_id
self.role_definition_id = self.object_role.role_definition_id
def save(self, *args, **kwargs):
if not self.id: # usually only new objects can be saved, but super needs to do error handling
if self.object_role_id and (not self.object_id):
# Cache fields from the associated object_role
self.object_id = self.object_role.object_id
self.content_type_id = self.object_role.content_type_id
self.role_definition_id = self.object_role.role_definition_id
return super().save(*args, **kwargs)


class RoleUserAssignment(AssignmentBase):
Expand Down
17 changes: 17 additions & 0 deletions test_app/tests/rbac/models/test_role_assignments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from ansible_base.rbac.models import RoleUserAssignment


@pytest.mark.django_db
def test_load_assignment_list(rando, inventory, inv_rd, global_inv_rd):
assignment = inv_rd.give_permission(rando, inventory)
global_inv_rd.give_global_permission(rando)
assert assignment.id in [asmt.id for asmt in RoleUserAssignment.objects.only('id')]


@pytest.mark.django_db
def test_load_assignment_property(rando, inventory, inv_rd, global_inv_rd):
assignment = inv_rd.give_permission(rando, inventory)
global_inv_rd.give_global_permission(rando)
assert str(assignment.object_id) in [asmt.object_id for asmt in RoleUserAssignment.objects.only('object_id')]

0 comments on commit dc5ff4c

Please sign in to comment.