-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for CAS ESN Accounts login #27 * Added section membership spawn from ESN Accounts login * [pre-commit.ci] auto fixes from pre-commit.com hooks * Linted Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
67cf837
commit 2926377
Showing
20 changed files
with
320 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter | ||
from allauth.socialaccount.models import SocialAccount, SocialLogin | ||
from django.http import HttpRequest | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% extends "fiesta/base.html" %} | ||
|
||
{% block main %} | ||
<ul class="list-disc"> | ||
{% for sa in request.user.socialaccount_set.all %} | ||
<li> | ||
{% if sa.provider == 'esnaccounts' %} | ||
<ul class="list-disc"> | ||
{% for role in sa.extra_data.roles %} | ||
<li>role: {{ role }}</li> | ||
{% endfor %} | ||
|
||
<li>joined: {{ sa.date_joined }} | ||
<li>email: {{ sa.extra_data.mail }} | ||
<li>uid: {{ sa.extra_data.uid }} | ||
<li><img src="{{ sa.extra_data.picture }}" width="60px"> | ||
<li>data: {{ sa.extra_data }} | ||
</ul> | ||
{% else %} | ||
{{ sa.provider }} | ||
{% endif %} | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.views.generic import TemplateView | ||
|
||
from apps.utils.breadcrumbs import with_breadcrumb | ||
|
||
|
||
@with_breadcrumb(_("My Profile")) | ||
class ProfileView(TemplateView): | ||
template_name = "accounts/profile.html" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = "0.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ESNAccountsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "apps.esnaccounts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from allauth_cas.urls import default_urlpatterns | ||
|
||
from .provider import ESNAccountsProvider | ||
|
||
urlpatterns = default_urlpatterns(ESNAccountsProvider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from allauth_cas.views import CASAdapter, CASCallbackView, CASLoginView, CASLogoutView | ||
|
||
from .provider import ESNAccountsProvider | ||
|
||
|
||
class ESNAccountsAdapter(CASAdapter): | ||
provider_id = ESNAccountsProvider.id | ||
url = "https://accounts.esn.org/cas/" | ||
version = 3 | ||
|
||
|
||
login = CASLoginView.adapter_view(ESNAccountsAdapter) | ||
|
||
callback = CASCallbackView.adapter_view(ESNAccountsAdapter) | ||
|
||
logout = CASLogoutView.adapter_view(ESNAccountsAdapter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.