-
Notifications
You must be signed in to change notification settings - Fork 1
/
samlgetgroups.py
34 lines (28 loc) · 929 Bytes
/
samlgetgroups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.contrib.auth.models import Group
class AuthFailed(Exception):
pass
def set_role(response, user, backend, *args, **kwargs):
try:
conndetails = user.social_auth.get(provider='saml')
roles = conndetails.extra_data['groups']
except KeyError:
user.groups.clear()
raise AuthFailed("No role assigned")
try:
user.is_superuser = False
user.is_staff = False
for role in roles:
if role == 'group-admins':
user.is_superuser = True
user.save()
user.is_staff = True
user.save()
else:
user.is_superuser = False
user.save()
user.is_staff = False
user.save()
group, created = Group.objects.get_or_create(name=role)
group.user_set.add(user)
except Group.DoesNotExist:
pass