[WIP] manager: add CUT partner BO management interface (#73170)

This commit is contained in:
Paul Marillonnet 2023-01-10 12:05:09 +01:00
parent 3e7ecae49e
commit c322a62aab
4 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,48 @@
# authentic2_cut - Authentic2 plugin for CUT
# Copyright (C) 2023 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from authentic2_idp_oidc.manager.forms import OIDCClientForm
from django import forms
from django.utils.translation import gettext_lazy as _
class CUTPartnerForm(OIDCClientForm):
domains = forms.CharField(
label=_('(Comma-separated) domains'),
required=False,
widget=forms.Textarea,
)
url = forms.URLField(label=_('Partner URL'), required=False)
stat_emails = forms.CharField(
label=_('(Comma-separated) statistics email recipients'),
required=False,
widget=forms.Textarea,
)
def _clean_array(self, key):
data = self.cleaned_data[key].replace(' ', '')
if data:
return data.split(',')
return None
def clean_domains(self):
return self._clean_array('domains')
def clean_stat_emails(self):
return self._clean_array('stat_emails')
class Meta(OIDCClientForm.Meta):
fields = OIDCClientForm.Meta.fields + ['domains', 'url', 'stat_emails']

View File

@ -0,0 +1,83 @@
# authentic2_cut - Authentic2 plugin for CUT
# Copyright (C) 2023 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from authentic2.manager.service_views import ServiceEditView, ServiceSettingsView
from authentic2_idp_oidc.manager.views import OIDCServiceAddView
from .. import models
from . import forms
class CUTPartnerAddView(OIDCServiceAddView):
form_class = forms.CUTPartnerForm
def form_valid(self, form):
response = super().form_valid(form)
# create associated cut partner
models.CUTPartner.objects.create(
oidc_client=self.object,
domains=form.cleaned_data.get('domains'),
url=form.cleaned_data.get('url'),
stat_emails=form.cleaned_data.get('stat_emails'),
)
return response
add_cut_partner = CUTPartnerAddView.as_view()
class CUTPartnerEditView(ServiceEditView):
def get_initial(self):
initial = super().get_initial()
if getattr(self.object, 'cut_partner', None):
cut_partner = self.object.cut_partner
# pre-populate CUT partner specific fields
initial['domains'] = cut_partner.domains
initial['url'] = cut_partner.url
initial['stat_emails'] = cut_partner.stat_emails
return initial
def form_valid(self, form):
if getattr(self.object, 'cut_partner', None):
cut_partner = self.object.cut_partner
# save CUT partner specific fields into dedicated model object
cut_partner.domains = form.cleaned_data.get('domains', [])
cut_partner.url = form.cleaned_data.get('url')
cut_partner.stat_emails = form.cleaned_data.get('stat_emails', [])
cut_partner.save()
def get_form_class(self):
return forms.CUTPartnerForm
edit_cut_partner = CUTPartnerEditView.as_view()
class CUTPartnerSettingsView(ServiceSettingsView):
template_name = 'authentic2/cut_partner_settings.html'
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
if getattr(self.object, 'cut_partner', None):
ctx['cut_partner_fields_values'] = {
'domains': self.object.cut_partner.domains,
'url': self.object.cut_partner.url,
'stat_emails': self.object.cut_partner.stat_emails,
}
return ctx
cut_partner_settings = CUTPartnerSettingsView.as_view()

View File

@ -0,0 +1,31 @@
{% extends "authentic2/manager/service_settings.html" %}
{% load i18n %}
{% block main %}
<div class="section">
{% for field, value in object_fields_values %}
<div class="service-field">
<div class="service-field--name">{{ field|capfirst }}{% trans ":" %}</div>
<div class="service-field--value">{% if value == True %}{% trans "yes" %}
{% elif value == False %}{% trans "no" %}
{% elif value == None %}{% trans "none" %}
{% else %}{{ value|linebreaksbr }}
{% endif %}</div>
</div>
{% endfor %}
{% for field, value in cut_partner_fields_values.items %}
<div class="cut-partner service-field">
<div class="service-field--name">{{ field|capfirst }}{% trans ":" %}</div>
<div class="service-field--value">{% if value == True %}{% trans "yes" %}
{% elif value == False %}{% trans "no" %}
{% elif value == None %}{% trans "none" %}
{% else %}{{ value|linebreaksbr }}
{% endif %}</div>
</div>
{% endfor %}
</div>
{% if extra_details_template %}
{% include extra_details_template %}
{% endif %}
{% endblock %}

View File

@ -19,6 +19,7 @@ from authentic2.manager.utils import manager_login_required
from django.conf.urls import url
from . import api_views, views
from .manager import views as manager_views
urlpatterns = required(
manager_login_required,
@ -49,6 +50,22 @@ urlpatterns = required(
views.validation_attachment_thumbnail,
name='cut-manager-user-validation-attachment-thumbnail',
),
# override a2's generic service edition views
url(
r'^manage/services/(?P<service_pk>\d*)/settings/$',
manager_views.cut_partner_settings,
name='a2-manager-service-settings',
),
url(
r'^manage/services/(?P<service_pk>\d*)/settings/edit/$',
manager_views.edit_cut_partner,
name='a2-manager-service-settings-edit',
),
url(
r'^manage/services/add-oidc/$',
manager_views.add_cut_partner,
name='a2-manager-add-oidc-service',
),
],
)