Skip to content

Commit

Permalink
Added section membership spawn from ESN Accounts login
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Feb 9, 2022
1 parent ab320f1 commit 8ae76d3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
13 changes: 13 additions & 0 deletions fiesta/apps/accounts/social.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from allauth.socialaccount.models import SocialAccount, SocialLogin
from django.http import HttpRequest
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter

from apps.esnaccounts.provider import ESNAccountsProvider


class SocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request: HttpRequest, login: SocialLogin):

sa: SocialAccount = login.account
if sa.provider == ESNAccountsProvider.id:
ESNAccountsProvider.pre_social_login(request, login)
59 changes: 57 additions & 2 deletions fiesta/apps/esnaccounts/provider.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
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):
pass
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

# TODO: fix extract_common_fields to load data from extra_data
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]
2 changes: 2 additions & 0 deletions fiesta/apps/sections/models/membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class State(models.TextChoices):
verbose_name=_("membership state"),
)

# TODO: add flag to signalize, if membership has been added from ESN Accounts

class Meta:
verbose_name = _("section membership")
verbose_name_plural = _("section memberships")
Expand Down
7 changes: 7 additions & 0 deletions fiesta/apps/sections/models/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Section(BaseTimestampedModel):
help_text=_("Universities, for whose this section offers services."),
)

code = models.SlugField(
verbose_name=_('code'),
help_text=_('Official code used in ESN world, especially in ESN Accounts database.'),
# TODO: remove blankness after proper migration from ESN accounts
null=True, blank=True,
)

class Meta:
verbose_name = _("ESN section")
verbose_name_plural = _("ESN sections")
Expand Down
1 change: 1 addition & 0 deletions fiesta/fiesta/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
ACCOUNT_USERNAME_MIN_LENGTH = 4 # a personal preference
ACCOUNT_SESSION_REMEMBER = True # None by default (to ask 'Remember me?'). I want the user to be always logged in
ACCOUNT_DEFAULT_HTTP_PROTOCOL = "https"
SOCIALACCOUNT_ADAPTER = 'apps.accounts.social.SocialAccountAdapter'

LOGIN_URL = "/accounts/auth/login"
LOGIN_REDIRECT_URL = "/accounts/profile"
Expand Down

0 comments on commit 8ae76d3

Please sign in to comment.