-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.py
74 lines (61 loc) · 2.3 KB
/
provider.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import typing
from allauth.socialaccount.providers.base import ProviderAccount
from allauth_cas.providers import CASProvider
from django.http import HttpRequest
from apps.accounts.models import User
from apps.sections.models import Section, SectionMembership
if typing.TYPE_CHECKING:
from allauth.socialaccount.models import SocialAccount, SocialLogin
class ESNAccountsAccount(ProviderAccount):
def get_avatar_url(self):
sa: "SocialAccount" = self.account
return sa.extra_data.get("picture")
class ESNAccountsProvider(CASProvider):
id = "esnaccounts"
name = "ESN Accounts"
account_class = ESNAccountsAccount
def extract_common_fields(self, data):
uid, extra = data
return {
"username": extra.get("username", uid),
"email": extra.get("mail"),
"first_name": extra.get("first"),
"last_name": extra.get("last"),
}
MEMBER_ROLE = "Local.activeMember"
EDITOR_ROLE = "Local.regularBoardMember"
@classmethod
def pre_social_login(
cls,
request: HttpRequest,
login: "SocialLogin",
):
user: User = login.user
sa: SocialAccount = login.account
roles = sa.extra_data.get("roles", [])
section_code = sa.extra_data.get("sc")
section_name = sa.extra_data.get("section")
user_nationality = sa.extra_data.get("nationality")
# national_section = sa.extra_data.get("country")
user.save()
SectionMembership.objects.update_or_create(
user=user,
section=Section.objects.get_or_create(
name=section_name,
defaults=dict(
code=section_code,
# TODO: definitely not, user nationality != section assignment
country=user_nationality,
),
)[0],
defaults=dict(
# TODO: check all possible for ESN Accounts roles
state=SectionMembership.State.ACTIVE,
role=SectionMembership.Role.EDITOR
if cls.EDITOR_ROLE in roles
else SectionMembership.Role.MEMBER
if cls.MEMBER_ROLE in roles
else SectionMembership.Role.INTERNATIONAL,
),
)
provider_classes = [ESNAccountsProvider]