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 and test data migration error from DAB RBAC #15138

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion awx/main/migrations/_dab_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ def get_permissions_for_role(role_field, children_map, apps):
return perm_list


def model_class(ct, apps):
"""
You can not use model methods in migrations, so this duplicates
what ContentType.model_class does, using current apps
"""
try:
return apps.get_model(ct.app_label, ct.model)
except LookupError:
return None


def migrate_to_new_rbac(apps, schema_editor):
"""
This method moves the assigned permissions from the old rbac.py models
Expand Down Expand Up @@ -197,7 +208,7 @@ def migrate_to_new_rbac(apps, schema_editor):
role_definition = managed_definitions[permissions]
else:
action = role.role_field.rsplit('_', 1)[0] # remove the _field ending of the name
role_definition_name = f'{role.content_type.model_class().__name__} {action.title()}'
role_definition_name = f'{model_class(role.content_type).__name__} {action.title()}'

description = role_descriptions[role.role_field]
if type(description) == dict:
Expand Down
15 changes: 15 additions & 0 deletions awx/main/tests/functional/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ def test_receptor_address(self, migrator):
bar_peers = bar.peers.all()
assert len(bar_peers) == 1
assert fooaddr in bar_peers

def test_migrate_DAB_RBAC(self, migrator):
old_state = migrator.apply_initial_migration(('main', '0190_alter_inventorysource_source_and_more'))
# Role = old_state.apps.get_model('main', 'Role')
Organization = old_state.apps.get_model('main', 'Organization')
# ContentType = old_state.apps.get_model('contenttypes', 'ContentType')
User = old_state.apps.get_model('auth', 'User')

org = Organization.objects.create()
user = User.objects.create(username='random-user')
org.read_role.members.add(user)

new_state = migrator.apply_tested_migration(
('main', '0192_custom_roles'),
)