58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
# authentic2 - versatile identity manager
|
|
# Copyright (C) 2022 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 django import forms
|
|
from django.core.exceptions import ValidationError
|
|
from django.template import Template, TemplateSyntaxError, VariableDoesNotExist
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from authentic2.forms.mixins import SlugMixin
|
|
|
|
from .models import BaseAuthenticator, LoginPasswordAuthenticator
|
|
|
|
|
|
class AuthenticatorFormMixin:
|
|
def clean_show_condition(self):
|
|
condition = self.cleaned_data['show_condition']
|
|
if condition:
|
|
try:
|
|
Template('{%% if %s %%}OK{%% endif %%}' % condition)
|
|
except (TemplateSyntaxError, VariableDoesNotExist) as e:
|
|
raise ValidationError(_('template syntax error: %s') % e)
|
|
return condition
|
|
|
|
|
|
class AuthenticatorAddForm(SlugMixin, forms.ModelForm):
|
|
field_order = ('authenticator', 'name', 'ou')
|
|
authenticators = {x.type: x for x in BaseAuthenticator.__subclasses__() if not x.internal}
|
|
|
|
authenticator = forms.ChoiceField(choices=[(k, v._meta.verbose_name) for k, v in authenticators.items()])
|
|
|
|
class Meta:
|
|
model = BaseAuthenticator
|
|
fields = ('name', 'ou')
|
|
|
|
def save(self):
|
|
Authenticator = self.authenticators[self.cleaned_data['authenticator']]
|
|
self.instance = Authenticator(name=self.cleaned_data['name'], ou=self.cleaned_data['ou'])
|
|
return super().save()
|
|
|
|
|
|
class LoginPasswordAuthenticatorEditForm(AuthenticatorFormMixin, forms.ModelForm):
|
|
class Meta:
|
|
model = LoginPasswordAuthenticator
|
|
exclude = ('name', 'slug', 'ou')
|