Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Roles to Django Groups? #465

Open
mke21 opened this issue Dec 14, 2021 · 5 comments
Open

Roles to Django Groups? #465

mke21 opened this issue Dec 14, 2021 · 5 comments

Comments

@mke21
Copy link

mke21 commented Dec 14, 2021

Hi,

Is it possible to map the roles in AzureAD to django groups? I can't seem to find any example of that.

@AngellusMortis
Copy link
Owner

Not implemented. Make a PR to add if you want it.

@mke21
Copy link
Author

mke21 commented Dec 14, 2021

Okay, clear.

BTW, I just found out that I could do this by using the 'MICROSOFT_AUTH_AUTHENTICATION_HOOK' setting. So maybe just a simple update to the documentation would suffice.

@lisabutti
Copy link

How did you use the MICROSOFT_AUTH_AUTHENTICATION_HOOK setting for map the roles into django groups?

@mke21
Copy link
Author

mke21 commented May 9, 2022

First I installed de PyJWT package.

Then I added a module with a function in one of the apps, say the module is `my_app.msauth:

import jwt


def add_to_group(user, token):
    from django.contrib.auth.models import Group
    id_token = token['id_token']
    token_data = jwt.decode(id_token, options={"verify_signature": False})
    roles = token_data.get('roles', [])
    user.groups.clear()
    for r in roles:
        current_group, created = Group.objects.get_or_create(name=r)
        current_group.user_set.add(user)

And I added the following to the django settings.py:

MICROSOFT_AUTH_AUTHENTICATE_HOOK = "my_app.msauth.add_to_group"

Of course, use your own app and module name.

@lisabutti
Copy link

Thank you @mke21 ! this worked for me

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants