Repository navigation
OpenConceptLab/ocl_online#307 | User profile sync to django | job to sync groups from Keycloak to django #933
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
188ac75
OpenConceptLab/ocl_online#307 | User profile sync to django | job to …
snyaggarwal 6d7438f
OpenConceptLab/ocl_online#307 | fixing pylint
snyaggarwal 10fb982
OpenConceptLab/ocl_online#307 | review feedbacks
snyaggarwal c4184ee
Apply batched suggestions from code review
snyaggarwal 7ed4f1c
OpenConceptLab/ocl_online#307 | review feedbacks
snyaggarwal 7ade5bd
OpenConceptLab/ocl_online#307 | review feedbacks
snyaggarwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| from django.core.management import BaseCommand | ||
| from django.db.models import Count | ||
| from django.db.models.functions import Lower | ||
|
|
||
| from core.services.auth.openid import OpenIDAuthService | ||
| from core.users.models import UserProfile | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = ('Links active accounts without an sso_id to the Keycloak user with exactly their username. A mixed-case ' | ||
| 'account links to the Keycloak user with its lowercased username when no other account has that username ' | ||
| 'in another case and the emails match. Every link and every skip is listed.') | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument('--dry-run', action='store_true') | ||
|
|
||
| def handle(self, *args, **options): | ||
| keycloak_users = {user['username']: user for user in OpenIDAuthService.get_users()} | ||
| linked = set(UserProfile.objects.filter(sso_id__isnull=False).values_list('sso_id', flat=True)) | ||
| case_twins = set(UserProfile.objects.order_by().values(lower=Lower('username')).annotate( | ||
| count=Count('id')).filter(count__gt=1).values_list('lower', flat=True)) | ||
| links, skipped = [], [] | ||
| for user in UserProfile.objects.filter(is_active=True, sso_id__isnull=True).order_by('username'): | ||
| kc_user, reason = self.match(user, keycloak_users, case_twins) | ||
| if kc_user and kc_user['id'] in linked: | ||
| kc_user, reason = None, 'its Keycloak user is linked to another account' | ||
| if not kc_user: | ||
| skipped.append(f'{user.username} ({reason})') | ||
| continue | ||
| linked.add(kc_user['id']) | ||
| links.append(f"{user.username} -> {kc_user['username']}") | ||
| if not options['dry_run']: | ||
| UserProfile.objects.filter(id=user.id, sso_id__isnull=True).update(sso_id=kc_user['id']) | ||
| verb = 'would be linked' if options['dry_run'] else 'linked' | ||
| self.stdout.write(f'{len(links)} account(s) {verb}, {len(skipped)} skipped') | ||
| for link in links: | ||
| self.stdout.write(f' {verb}: {link}') | ||
| for username in skipped: | ||
| self.stdout.write(f' skipped: {username}') | ||
|
snyaggarwal marked this conversation as resolved.
|
||
|
|
||
| @staticmethod | ||
| def match(user, keycloak_users, case_twins): | ||
| if user.username in keycloak_users: | ||
| return keycloak_users[user.username], None | ||
| lower = user.username.lower() | ||
| if lower == user.username or lower not in keycloak_users: | ||
| return None, 'no Keycloak user has this username' | ||
| if lower in case_twins: | ||
| return None, 'another account has this username in another case' | ||
| kc_user = keycloak_users[lower] | ||
| email = (user.email or '').strip().lower() | ||
| if not email or email != (kc_user.get('email') or '').strip().lower(): | ||
| return None, 'emails differ' | ||
| return kc_user, None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from django.core.management import BaseCommand | ||
|
|
||
| from core.common.tasks import sync_groups_from_keycloak | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = 'Syncs Keycloak group memberships into Django, including for users who only use API tokens.' | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument('--dry-run', action='store_true') | ||
|
|
||
| def handle(self, *args, **options): | ||
| dry_run = options['dry_run'] | ||
| summary = sync_groups_from_keycloak(dry_run=dry_run) | ||
| self.stdout.write(f"{summary['changed']} user(s) {'would change' if dry_run else 'changed'}") | ||
| for name in sorted(set(summary['gained']) | set(summary['lost'])): | ||
| self.stdout.write(f" {name}: +{summary['gained'].get(name, 0)} -{summary['lost'].get(name, 0)}") | ||
| for change in summary['access_changes']: | ||
| self.stdout.write(f' access change: {change}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.