|
| 1 | +from django.dispatch import receiver |
| 2 | + |
| 3 | +from djangosaml2.signals import pre_user_save |
| 4 | + |
| 5 | +from server.models import UserProfile, ProfileLevel |
| 6 | +from server.utils import get_django_setting |
| 7 | + |
| 8 | + |
| 9 | +READ_ONLY_GROUPS = set(get_django_setting('SAML_READ_ONLY_GROUPS', [])) |
| 10 | +READ_WRITE_GROUPS = set(get_django_setting('SAML_READ_WRITE_GROUPS', [])) |
| 11 | +GLOBAL_ADMIN_GROUPS = set(get_django_setting('SAML_GLOBAL_ADMIN_GROUPS', [])) |
| 12 | +GROUPS_ATTRIBUTE = get_django_setting('SAML_GROUPS_ATTRIBUTE', 'memberOf') |
| 13 | + |
| 14 | + |
| 15 | +@receiver(pre_user_save) |
| 16 | +def update_group_membership( |
| 17 | + sender, instance, attributes: dict, user_modified: bool, **kwargs) -> bool: |
| 18 | + """Update user's group membership based on passed SAML groups |
| 19 | +
|
| 20 | + Sal access level is based on the highest access level granted across |
| 21 | + all groups a user is a member of. For example, if you are in a group |
| 22 | + with RO access and a group with GA access, the GA level "wins". |
| 23 | +
|
| 24 | + Users who have no group membership in any of the configured |
| 25 | + SAML_X_GROUPS settings will be unchanged, allowing changes to these |
| 26 | + users via the admin panel to persist. |
| 27 | +
|
| 28 | + Args: |
| 29 | + sender: The class of the user that just logged in. |
| 30 | + instance: User instance |
| 31 | + attributes: SAML attributes dict. |
| 32 | + user_modified: Bool whether the user has been modified |
| 33 | + kwargs: |
| 34 | + signal: The signal instance |
| 35 | +
|
| 36 | + Returns: |
| 37 | + Whether or not the user has been modified. This allows the user |
| 38 | + instance to be saved once at the conclusion of the auth process |
| 39 | + to keep the writes to a minimum. |
| 40 | + """ |
| 41 | + assertion_groups = set(attributes.get(GROUPS_ATTRIBUTE, [])) |
| 42 | + if GLOBAL_ADMIN_GROUPS.intersection(assertion_groups): |
| 43 | + instance.userprofile.delete() |
| 44 | + user_profile = UserProfile(user=instance, level=ProfileLevel.global_admin) |
| 45 | + user_profile.save() |
| 46 | + instance.is_superuser = True |
| 47 | + instance.is_staff = True |
| 48 | + instance.is_active = True |
| 49 | + user_modified = True |
| 50 | + elif READ_WRITE_GROUPS.intersection(assertion_groups): |
| 51 | + instance.userprofile.delete() |
| 52 | + user_profile = UserProfile(user=instance, level=ProfileLevel.read_write) |
| 53 | + user_profile.save() |
| 54 | + instance.is_superuser = False |
| 55 | + instance.is_staff = False |
| 56 | + instance.is_active = True |
| 57 | + user_modified = True |
| 58 | + elif READ_ONLY_GROUPS.intersection(assertion_groups): |
| 59 | + instance.userprofile.delete() |
| 60 | + user_profile = UserProfile(user=instance, level=ProfileLevel.read_only) |
| 61 | + user_profile.save() |
| 62 | + instance.is_superuser = False |
| 63 | + instance.is_staff = False |
| 64 | + instance.is_active = True |
| 65 | + user_modified = True |
| 66 | + return user_modified |
0 commit comments