-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure SSO works on admin level (#237)
* Ensure SSO works on admin level and change implementation to be an external plugin * remove unsed template * fix pipeline * using provider name constant instead of text * update transaction for storing key * handle case sso_provider not configured --------- Co-authored-by: odkhang <odkhang@tma.com.vn>
- Loading branch information
Showing
11 changed files
with
158 additions
and
121 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
26 changes: 26 additions & 0 deletions
26
src/pretalx/eventyay_common/templates/eventyay_common/sso/detail.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,26 @@ | ||
{% extends "orga/base.html" %} | ||
{% load i18n %} | ||
{% load rules %} | ||
{% block extra_title %}{% translate "SSO settings" %} :: {% endblock extra_title %} | ||
{% block content %} | ||
<h2>{% translate "SSO client settings" %}</h2> | ||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form }} | ||
<div class="submit-group panel"> | ||
<span> | ||
{% if sso_provider %} | ||
<a class="btn-outline-danger btn-lg" role="button" href='{% url "orga:admin.sso.delete" %}'> | ||
{% translate "Delete key" %} | ||
</a> | ||
{% endif %} | ||
</span> | ||
<span> | ||
<button type="submit" class="btn-success btn-lg"> | ||
<i class="fa fa-check"></i> | ||
{{ phrases.base.save }} | ||
</button> | ||
</span> | ||
</div> | ||
</form> | ||
{% endblock content %} |
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,91 @@ | ||
from allauth.socialaccount.models import SocialApp | ||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.contrib.sites.models import Site | ||
from django.db import transaction | ||
from django.http import HttpResponseRedirect | ||
from django.shortcuts import redirect | ||
from django.urls import reverse | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.views.generic import DetailView | ||
|
||
from pretalx.common.text.phrases import phrases | ||
from pretalx.common.views import CreateOrUpdateView | ||
from pretalx.common.views.mixins import ActionConfirmMixin, PermissionRequired | ||
from pretalx.orga.forms.sso_client_form import SSOClientForm | ||
|
||
|
||
class SSOConfigureView(PermissionRequired, CreateOrUpdateView): | ||
template_name = "eventyay_common/sso/detail.html" | ||
permission_required = "person.is_administrator" | ||
form_class = SSOClientForm | ||
model = SocialApp | ||
|
||
def get_object(self): | ||
""" | ||
Get the SocialApp instance for the 'eventyay' provider if it exists. | ||
If not, return None to create a new instance. | ||
Note: "eventyay" is the provider name for the Eventyay Ticket Provider. | ||
""" | ||
return SocialApp.objects.filter(provider=settings.EVENTYAY_SSO_PROVIDER).first() | ||
|
||
def get_success_url(self): | ||
messages.success(self.request, phrases.base.saved) | ||
return self.request.path | ||
|
||
def form_valid(self, form): | ||
""" | ||
Handle the form submission and save the instance. | ||
""" | ||
instance = form.save(commit=False) | ||
instance.provider = settings.EVENTYAY_SSO_PROVIDER | ||
instance.name = "Eventyay Ticket Provider" | ||
with transaction.atomic(): | ||
instance.save() | ||
site = Site.objects.get(pk=settings.SITE_ID) | ||
instance.sites.add(site) | ||
return redirect(self.get_success_url()) | ||
|
||
def form_invalid(self, form): | ||
""" | ||
Handle invalid form submissions. | ||
""" | ||
messages.error( | ||
self.request, | ||
"There was an error updating the Eventyay Ticket " | ||
"Provider configuration.", | ||
) | ||
return self.render_to_response(self.get_context_data(form=form)) | ||
|
||
def get_context_data(self, **kwargs): | ||
""" | ||
Add additional context to the template if necessary. | ||
""" | ||
context = super().get_context_data(**kwargs) | ||
context["sso_provider"] = self.get_object() | ||
return context | ||
|
||
|
||
class SSODeleteView(PermissionRequired, ActionConfirmMixin, DetailView): | ||
permission_required = "person.is_administrator" | ||
model = SocialApp | ||
action_text = ( | ||
_("You will not able to login with eventyay-tickets account.") | ||
+ " " | ||
+ phrases.base.delete_warning | ||
) | ||
|
||
def get_object(self, queryset=None): | ||
return SocialApp.objects.filter(provider=settings.EVENTYAY_SSO_PROVIDER).first() | ||
|
||
def action_object_name(self): | ||
return _("SSO Provider") + f": {self.get_object().name}" | ||
|
||
@property | ||
def action_back_url(self): | ||
return reverse("orga:admin.sso.settings") | ||
|
||
def post(self, *args, **kwargs): | ||
sso_provider = self.get_object() | ||
sso_provider.delete() | ||
return HttpResponseRedirect(reverse("orga:admin.sso.settings")) |
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 |
---|---|---|
@@ -1,22 +1,12 @@ | ||
from allauth.socialaccount.models import SocialApp | ||
from django import forms | ||
from django.conf import settings | ||
from django.contrib.sites.models import Site | ||
|
||
|
||
class SSOClientForm(forms.ModelForm): | ||
def __init__(self, provider_id, *args, **kwargs): | ||
social_app = SocialApp.objects.filter(provider=provider_id).first() | ||
kwargs["instance"] = social_app | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.fields["secret"].required = True # Secret is required | ||
self.fields["secret"].required = True | ||
|
||
class Meta: | ||
model = SocialApp | ||
fields = ["client_id", "secret"] | ||
|
||
def save(self, organiser=None): | ||
self.instance.name = organiser | ||
self.instance.provider = organiser | ||
super().save() | ||
self.instance.sites.add(Site.objects.get(pk=settings.SITE_ID)) |
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
26 changes: 0 additions & 26 deletions
26
src/pretalx/orga/templates/orga/organiser/organiser_sso.html
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.