authentic/src/authentic2/apps/authenticators/views.py

112 lines
3.5 KiB
Python

# authentic2 - versatile identity manager
# Copyright (C) 2010-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.contrib import messages
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.utils.translation import ugettext as _
from django.views.generic import CreateView, DeleteView, DetailView, UpdateView
from django.views.generic.list import ListView
from authentic2.apps.authenticators import forms
from authentic2.apps.authenticators.models import BaseAuthenticator
from authentic2.manager.views import MediaMixin, TitleMixin
class AuthenticatorsMixin(MediaMixin, TitleMixin):
def get_queryset(self):
return self.model.authenticators.all()
class AuthenticatorsView(AuthenticatorsMixin, ListView):
template_name = 'authentic2/authenticators/authenticators.html'
model = BaseAuthenticator
title = _('Authenticators')
authenticators = AuthenticatorsView.as_view()
class AuthenticatorAddView(AuthenticatorsMixin, CreateView):
template_name = 'authentic2/authenticators/authenticator_add_form.html'
title = _('New authenticator')
form_class = forms.AuthenticatorAddForm
add = AuthenticatorAddView.as_view()
class AuthenticatorDetailView(AuthenticatorsMixin, DetailView):
template_name = 'authentic2/authenticators/authenticator_detail.html'
model = BaseAuthenticator
@property
def title(self):
return str(self.object)
detail = AuthenticatorDetailView.as_view()
class AuthenticatorEditView(AuthenticatorsMixin, UpdateView):
template_name = 'authentic2/authenticators/authenticator_edit_form.html'
title = _('Edit authenticator')
model = BaseAuthenticator
def get_form_class(self):
return self.object.manager_form_class
edit = AuthenticatorEditView.as_view()
class AuthenticatorDeleteView(AuthenticatorsMixin, DeleteView):
template_name = 'authentic2/authenticators/authenticator_delete_form.html'
title = _('Delete authenticator')
model = BaseAuthenticator
success_url = reverse_lazy('a2-manager-authenticators')
def dispatch(self, *args, **kwargs):
if self.get_object().internal:
raise PermissionDenied
return super().dispatch(*args, **kwargs)
delete = AuthenticatorDeleteView.as_view()
class AuthenticatorToggleView(DetailView):
model = BaseAuthenticator
def get(self, request, *args, **kwargs):
authenticator = self.get_object()
if authenticator.enabled:
authenticator.enabled = False
authenticator.save()
message = _('Authenticator has been disabled.')
else:
authenticator.enabled = True
authenticator.save()
message = _('Authenticator has been enabled.')
messages.info(self.request, message)
return HttpResponseRedirect(authenticator.get_absolute_url())
toggle = AuthenticatorToggleView.as_view()