Skip to content

Commit f1038dd

Browse files
committed
squash!: Handle Group permissions
1 parent faf20b7 commit f1038dd

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

openedx_authz/migrations/0002_migrate_legacy_permissions.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from django.db import migrations
66

7-
from openedx_authz.api.users import assign_role_to_user_in_scope
7+
from openedx_authz.api.users import assign_role_to_user_in_scope, batch_assign_role_to_users_in_scope
88
from openedx_authz.constants.roles import LIBRARY_ADMIN, LIBRARY_AUTHOR, LIBRARY_USER
99

1010
logger = logging.getLogger(__name__)
@@ -43,16 +43,10 @@ def migrate_legacy_permissions(apps, schema_editor):
4343
return
4444

4545
legacy_permissions = ContentLibraryPermission.objects.select_related(
46-
'library', 'library__org', 'user'
46+
'library', 'library__org', 'user', 'group'
4747
).all()
4848

4949
for permission in legacy_permissions:
50-
if permission.group:
51-
# TODO: Consider creating individual role assignments for each user in the group
52-
logger.warning(
53-
f"Skipping group-based permission for Group: {permission.group}")
54-
continue
55-
5650
# Migrate the permission to the new model
5751

5852
# Derive equivalent role based on access level
@@ -73,16 +67,28 @@ def migrate_legacy_permissions(apps, schema_editor):
7367
# Generating scope based on library identifier
7468
scope = f"lib:{permission.library.org.name}:{permission.library.slug}"
7569

76-
logger.info(
77-
f"Migrating permission for User: {permission.user.username} to Role: {role.external_key} in Scope: {scope}"
78-
)
79-
80-
# TODO: not sure if this can/should be done in an atomic transaction
81-
assign_role_to_user_in_scope(
82-
user_external_key=permission.user.username,
83-
role_external_key=role.external_key,
84-
scope_external_key=scope
85-
)
70+
if permission.group:
71+
# Permission applied to a group
72+
users = [user.username for user in permission.group.user_set.all()]
73+
logger.info(
74+
f"Migrating permissions for Users: {users} in Group: {permission.group.name} to Role: {role.external_key} in Scope: {scope}"
75+
)
76+
batch_assign_role_to_users_in_scope(
77+
users=users,
78+
role_external_key=role.external_key,
79+
scope_external_key=scope
80+
)
81+
else:
82+
# Permission applied to individual user
83+
logger.info(
84+
f"Migrating permission for User: {permission.user.username} to Role: {role.external_key} in Scope: {scope}"
85+
)
86+
87+
assign_role_to_user_in_scope(
88+
user_external_key=permission.user.username,
89+
role_external_key=role.external_key,
90+
scope_external_key=scope
91+
)
8692

8793

8894
class Migration(migrations.Migration):

0 commit comments

Comments
 (0)