Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing role membership when giving creator permissions #15058

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion awx/main/models/rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,37 @@ def give_or_remove_permission(role, actor, giving=True):
rd.give_or_remove_permission(actor, obj, giving=giving)


class SyncEnabled(threading.local):
def __init__(self):
self.enabled = True


rbac_sync_enabled = SyncEnabled()


@contextlib.contextmanager
def disable_rbac_sync():
try:
previous_value = rbac_sync_enabled.enabled
rbac_sync_enabled.enabled = False
yield
finally:
rbac_sync_enabled.enabled = previous_value


def give_creator_permissions(user, obj):
RoleDefinition.objects.give_creator_permissions(user, obj)
assignment = RoleDefinition.objects.give_creator_permissions(user, obj)
if assignment:
with disable_rbac_sync():
old_role = get_role_from_object_role(assignment.object_role)
old_role.members.add(user)


def sync_members_to_new_rbac(instance, action, model, pk_set, reverse, **kwargs):
if action.startswith('pre_'):
return
if not rbac_sync_enabled.enabled:
return

if action == 'post_add':
is_giving = True
Expand Down
9 changes: 8 additions & 1 deletion awx/main/tests/functional/dab_rbac/test_translation_layer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from awx.main.models.rbac import get_role_from_object_role
from awx.main.models.rbac import get_role_from_object_role, give_creator_permissions
from awx.main.models import User, Organization, WorkflowJobTemplate, WorkflowJobTemplateNode
from awx.api.versioning import reverse

Expand Down Expand Up @@ -74,3 +74,10 @@ def test_workflow_approval_list(get, post, admin_user):

r = get(url=reverse('api:workflow_approval_list'), user=admin_user, expect=200)
assert r.data['count'] >= 1


@pytest.mark.django_db
def test_creator_permission(rando, admin_user, inventory):
give_creator_permissions(rando, inventory)
assert rando in inventory.admin_role
assert rando in inventory.admin_role.members.all()
Loading