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

Update backends.py #1259

Open
wants to merge 10 commits into
base: replace_cas
Choose a base branch
from
49 changes: 42 additions & 7 deletions pod/authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def update_owner_params(user, params) -> None:
settings, "OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES", []
)

OIDC_CLAIM_AFFILIATION = getattr(settings, "OIDC_CLAIM_AFFILIATION", "affiliations")

class OIDCBackend(OIDCAuthenticationBackend):
"""OIDC backend authentication."""
Expand All @@ -92,13 +93,26 @@ def create_user(self, claims):
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, "")
user.username = claims.get(OIDC_CLAIM_PREFERRED_USERNAME, "")
user.owner.affiliation = OIDC_DEFAULT_AFFILIATION
for code_name in OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES:
try:
user.owner.accessgroup_set.add(
AccessGroup.objects.get(code_name=code_name)
)
except ObjectDoesNotExist:
pass
affiliations = claims.get(OIDC_CLAIM_AFFILIATION, "")
if affiliations:
for affiliation in affiliations:
if CREATE_GROUP_FROM_AFFILIATION:
accessgroup, group_created = AccessGroup.objects.get_or_create(code_name=affiliation)
if group_created:
accessgroup.display_name = affiliation
accessgroup.auto_sync = True
accessgroup.sites.add(Site.objects.get_current())
accessgroup.save()
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.affiliation = OIDC_DEFAULT_AFFILIATION
for code_name in OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES:
try:
user.owner.accessgroup_set.add(AccessGroup.objects.get(code_name=code_name))
except ObjectDoesNotExist:
pass
user.is_staff = is_staff_affiliation(affiliation=user.owner.affiliation)
user.owner.save()
user.save()
Expand All @@ -110,6 +124,27 @@ def update_user(self, user, claims):
user.first_name = claims.get(OIDC_CLAIM_GIVEN_NAME, "")
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, "")
user.username = claims.get(OIDC_CLAIM_PREFERRED_USERNAME, "")
affiliations = claims.get(OIDC_CLAIM_AFFILIATION, "")
if affiliations:
for affiliation in affiliations:
if CREATE_GROUP_FROM_AFFILIATION:
accessgroup, group_created = AccessGroup.objects.get_or_create(code_name=affiliation)
if group_created:
accessgroup.display_name = affiliation
accessgroup.auto_sync = True
accessgroup.sites.add(Site.objects.get_current())
accessgroup.save()
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.affiliation = OIDC_DEFAULT_AFFILIATION
for code_name in OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES:
try:
user.owner.accessgroup_set.add(AccessGroup.objects.get(code_name=code_name))
except ObjectDoesNotExist:
pass

user.save()

user.owner.auth_type = "OIDC"
Expand Down