IdHub/idhub/admin/views.py

1058 lines
32 KiB
Python
Raw Normal View History

2023-10-20 14:50:53 +00:00
import os
2023-10-24 12:08:24 +00:00
import json
2023-10-20 14:50:53 +00:00
from pathlib import Path
2023-10-18 15:30:11 +00:00
from smtplib import SMTPException
from django_tables2 import SingleTableView
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
from django.conf import settings
from django.template.loader import get_template
2023-10-09 15:59:15 +00:00
from django.utils.translation import gettext_lazy as _
2024-01-19 19:37:17 +00:00
from django.views.generic.base import TemplateView, View
2023-11-08 10:24:37 +00:00
from django.views.generic.edit import (
CreateView,
DeleteView,
FormView,
UpdateView,
)
2023-10-13 10:35:06 +00:00
from django.shortcuts import get_object_or_404, redirect
2023-10-09 15:59:15 +00:00
from django.urls import reverse_lazy
2023-10-23 17:24:48 +00:00
from django.http import HttpResponse
2023-10-09 15:59:15 +00:00
from django.contrib import messages
2024-02-21 10:38:00 +00:00
from django.core.cache import cache
2023-11-07 16:44:15 +00:00
from utils import credtools
2023-10-25 15:49:17 +00:00
from idhub_auth.models import User
2023-11-13 17:09:37 +00:00
from idhub_auth.forms import ProfileForm
2024-02-02 14:40:52 +00:00
from idhub.mixins import AdminView, Http403
2023-10-18 15:30:11 +00:00
from idhub.email.views import NotifyActivateUserByEmail
2023-11-14 14:55:17 +00:00
from idhub.admin.forms import (
EncryptionKeyForm,
ImportCertificateForm,
2023-11-14 14:55:17 +00:00
ImportForm,
MembershipForm,
TermsConditionsForm,
2023-11-14 14:55:17 +00:00
SchemaForm,
UserRolForm
2023-11-14 14:55:17 +00:00
)
from idhub.admin.tables import (
2023-11-29 13:41:15 +00:00
DashboardTable,
2023-11-30 16:05:31 +00:00
UserTable,
2023-12-01 11:08:22 +00:00
RolesTable,
ServicesTable,
CredentialTable,
2023-12-06 10:45:04 +00:00
DIDTable,
2023-12-15 17:40:56 +00:00
DataTable,
TemplateTable
)
2023-10-25 10:18:42 +00:00
from idhub.models import (
2023-10-26 11:33:13 +00:00
DID,
2023-11-09 16:58:06 +00:00
Event,
2023-10-25 10:18:42 +00:00
File_datas,
Membership,
Rol,
Service,
Schemas,
UserRol,
2023-10-30 12:53:19 +00:00
VerificableCredential,
2023-10-25 10:18:42 +00:00
)
2023-10-09 15:59:15 +00:00
class TermsAndConditionsView(AdminView, FormView):
template_name = "idhub/admin/terms_conditions.html"
2024-02-05 11:25:02 +00:00
title = _('Data protection')
section = ""
subtitle = _('Terms and Conditions')
icon = 'bi bi-file-earmark-medical'
form_class = TermsConditionsForm
success_url = reverse_lazy('idhub:admin_dashboard')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
2024-02-05 11:25:02 +00:00
if self.request.user.accept_gdpr:
kwargs['initial'] = {
"accept_privacy": True,
"accept_legal": True,
"accept_cookies": True
}
return kwargs
def form_valid(self, form):
form.save()
return super().form_valid(form)
class EncryptionKeyView(AdminView, FormView):
template_name = "idhub/admin/encryption_key.html"
title = _('Encryption Key')
section = ""
subtitle = _('Encryption Key')
icon = 'bi bi-key'
form_class = EncryptionKeyForm
success_url = reverse_lazy('idhub:admin_dashboard')
def get(self, request, *args, **kwargs):
2024-02-21 10:38:00 +00:00
if cache.get("KEY_DIDS"):
return redirect(self.success_url)
return super().get(request, *args, **kwargs)
def form_valid(self, form):
form.save()
return super().form_valid(form)
2024-01-19 19:37:17 +00:00
class DobleFactorAuthView(AdminView, View):
2024-01-19 09:59:35 +00:00
url = reverse_lazy('idhub:admin_dashboard')
def get(self, request, *args, **kwargs):
2024-02-02 14:40:52 +00:00
if not self.request.user.is_admin:
raise Http403()
2024-01-19 09:59:35 +00:00
if not self.request.session.get("2fauth"):
return redirect(self.url)
2024-01-19 19:37:17 +00:00
if self.request.session.get("2fauth") == str(kwargs.get("admin2fauth")):
self.request.session.pop("2fauth", None)
return redirect(self.url)
2024-01-19 09:59:35 +00:00
2024-01-19 19:37:17 +00:00
return redirect(reverse_lazy("idhub:login"))
2024-01-19 09:59:35 +00:00
class DashboardView(AdminView, SingleTableView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/dashboard.html"
table_class = DashboardTable
2023-10-09 15:59:15 +00:00
title = _('Dashboard')
2023-11-21 11:38:12 +00:00
subtitle = _('Events')
2023-10-09 15:59:15 +00:00
icon = 'bi bi-bell'
section = "Home"
model = Event
2023-10-09 15:59:15 +00:00
2023-11-09 16:58:06 +00:00
2023-10-13 11:37:26 +00:00
class People(AdminView):
2023-11-21 11:38:12 +00:00
title = _("User management")
2023-10-09 15:59:15 +00:00
section = "People"
2023-10-11 07:52:05 +00:00
class AccessControl(AdminView, TemplateView):
2023-11-13 09:15:52 +00:00
title = _("Access control management")
2023-10-09 15:59:15 +00:00
section = "AccessControl"
2023-10-11 07:52:05 +00:00
class Credentials(AdminView, TemplateView):
2023-11-21 11:38:12 +00:00
title = _("Credential management")
2023-11-13 09:15:52 +00:00
section = "Credential"
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
class SchemasMix(AdminView, TemplateView):
2023-11-21 11:38:12 +00:00
title = _("Template management")
2023-11-13 09:15:52 +00:00
section = "Template"
2023-10-09 15:59:15 +00:00
2023-11-08 10:24:37 +00:00
class ImportExport(AdminView):
2023-11-13 09:15:52 +00:00
title = _("Data file management")
2023-10-09 15:59:15 +00:00
section = "ImportExport"
2023-11-29 13:41:15 +00:00
class PeopleListView(People, SingleTableView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people.html"
2023-11-13 09:15:52 +00:00
subtitle = _('View users')
2023-10-09 15:59:15 +00:00
icon = 'bi bi-person'
2023-11-29 13:41:15 +00:00
table_class = UserTable
model = User
2023-10-09 15:59:15 +00:00
2023-10-11 14:23:45 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'users': User.objects.filter(),
})
return context
2023-10-09 15:59:15 +00:00
2023-11-29 13:41:15 +00:00
def get_queryset(self, **kwargs):
queryset = super().get_queryset(**kwargs)
return queryset
2023-10-11 16:08:20 +00:00
2023-11-03 15:42:45 +00:00
class PeopleView(People, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/user.html"
2023-11-13 09:15:52 +00:00
subtitle = _('User personal information')
2023-10-11 16:08:20 +00:00
icon = 'bi bi-person'
model = User
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.object,
})
return context
2023-11-03 15:42:45 +00:00
class PeopleActivateView(PeopleView):
2023-10-13 10:35:06 +00:00
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-13 10:35:06 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
2023-10-13 19:04:44 +00:00
if self.object == self.request.user:
2023-11-21 11:38:12 +00:00
messages.error(self.request, _('It is not possible deactivate your account!'))
2023-10-13 19:04:44 +00:00
return redirect('idhub:admin_people', self.object.id)
2023-10-13 10:35:06 +00:00
if self.object.is_active:
self.object.is_active = False
2023-11-09 16:58:06 +00:00
Event.set_EV_USR_DEACTIVATED_BY_ADMIN(self.object)
2023-10-13 10:35:06 +00:00
else:
self.object.is_active = True
2023-11-09 16:58:06 +00:00
Event.set_EV_USR_ACTIVATED_BY_ADMIN(self.object)
2023-10-13 10:35:06 +00:00
self.object.save()
return redirect('idhub:admin_people', self.object.id)
2023-11-03 15:42:45 +00:00
class PeopleDeleteView(PeopleView):
2023-10-13 10:35:06 +00:00
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-13 10:35:06 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
2023-10-13 19:04:44 +00:00
if self.object != self.request.user:
2023-11-09 16:58:06 +00:00
Event.set_EV_USR_DELETED_BY_ADMIN(self.object)
2023-10-13 19:04:44 +00:00
self.object.delete()
else:
2023-11-21 11:38:12 +00:00
messages.error(self.request, _('It is not possible delete your account!'))
2023-10-13 10:35:06 +00:00
return redirect('idhub:admin_people_list')
2023-11-09 16:58:06 +00:00
2023-11-13 17:09:37 +00:00
class PeopleEditView(People, FormView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/user_edit.html"
2023-11-13 17:09:37 +00:00
subtitle = _('Update user')
icon = 'bi bi-person'
form_class = ProfileForm
2023-10-13 11:37:26 +00:00
success_url = reverse_lazy('idhub:admin_people_list')
2023-10-13 10:35:06 +00:00
2023-11-13 17:09:37 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().post(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.user
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.user,
})
return context
2023-11-09 16:58:06 +00:00
def form_valid(self, form):
user = form.save()
2023-11-21 11:38:12 +00:00
messages.success(self.request, _('The account was updated successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_USR_UPDATED_BY_ADMIN(user)
Event.set_EV_USR_UPDATED(user)
return super().form_valid(form)
2023-10-13 11:37:26 +00:00
2023-11-03 15:42:45 +00:00
class PeopleRegisterView(NotifyActivateUserByEmail, People, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_register.html"
2023-11-13 10:12:40 +00:00
subtitle = _('Add user')
2023-10-09 15:59:15 +00:00
icon = 'bi bi-person'
2023-11-13 17:09:37 +00:00
form_class = ProfileForm
2023-10-13 11:37:26 +00:00
success_url = reverse_lazy('idhub:admin_people_list')
2023-10-09 15:59:15 +00:00
2023-10-16 17:08:18 +00:00
def get_success_url(self):
self.success_url = reverse_lazy(
'idhub:admin_people_membership_new',
kwargs={"pk": self.object.id}
)
return self.success_url
2023-10-18 15:30:11 +00:00
def form_valid(self, form):
2024-02-22 12:49:08 +00:00
super().form_valid(form)
user = form.instance
user.set_encrypted_sensitive_data()
user.save()
self.create_defaults_dids(user)
2023-11-13 09:15:52 +00:00
messages.success(self.request, _('The account was created successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_USR_REGISTERED(user)
Event.set_EV_USR_WELCOME(user)
2023-10-18 15:30:11 +00:00
if user.is_active:
try:
self.send_email(user)
except SMTPException as e:
messages.error(self.request, e)
return super().form_valid(form)
2024-02-22 12:49:08 +00:00
def create_defaults_dids(self, user):
did = DID(label="Default", user=user, type=DID.Types.WEB)
did.set_did()
did.save()
2023-10-16 17:08:18 +00:00
2023-11-13 17:09:37 +00:00
class PeopleMembershipRegisterView(People, FormView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_membership_register.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Associate a membership to the user')
2023-10-16 17:08:18 +00:00
icon = 'bi bi-person'
2023-11-13 17:09:37 +00:00
form_class = MembershipForm
2023-10-16 17:08:18 +00:00
model = Membership
success_url = reverse_lazy('idhub:admin_people_list')
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().post(request, *args, **kwargs)
def get_form(self):
form = super().get_form()
form.fields['start_date'].widget.input_type = 'date'
form.fields['end_date'].widget.input_type = 'date'
2023-11-13 17:09:37 +00:00
form.fields['start_date'].required = True
2023-10-16 17:08:18 +00:00
return form
def get_form_kwargs(self):
self.object = self.model(user=self.user)
kwargs = super().get_form_kwargs()
2023-11-13 17:09:37 +00:00
kwargs['instance'] = self.object
2023-10-16 17:08:18 +00:00
return kwargs
2023-11-13 17:09:37 +00:00
def form_valid(self, form):
form.save()
messages.success(self.request, _('Membership created successfully'))
return super().form_valid(form)
2023-10-17 08:53:03 +00:00
def get_success_url(self):
self.success_url = reverse_lazy(
2023-10-17 15:42:48 +00:00
'idhub:admin_people_rol_new',
2023-10-17 08:53:03 +00:00
kwargs={"pk": self.user.id}
)
return self.success_url
2023-11-13 17:09:37 +00:00
class PeopleMembershipEditView(People, FormView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_membership_register.html"
2023-11-13 17:09:37 +00:00
subtitle = _('Associate a membership to the user')
2023-10-17 08:53:03 +00:00
icon = 'bi bi-person'
2023-11-13 17:09:37 +00:00
form_class = MembershipForm
2023-10-17 08:53:03 +00:00
model = Membership
success_url = reverse_lazy('idhub:admin_people_list')
2023-11-13 17:09:37 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.user = self.object.user
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.user = self.object.user
return super().post(request, *args, **kwargs)
2023-10-17 08:53:03 +00:00
def get_form(self):
form = super().get_form()
form.fields['start_date'].widget.input_type = 'date'
form.fields['end_date'].widget.input_type = 'date'
2023-11-13 17:09:37 +00:00
form.fields['start_date'].required = True
2023-10-17 08:53:03 +00:00
return form
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
2023-11-13 17:09:37 +00:00
kwargs['instance'] = self.object
2023-10-17 08:53:03 +00:00
return kwargs
2023-11-13 17:09:37 +00:00
def form_valid(self, form):
form.save()
messages.success(self.request, _('Membership updated successfully'))
return super().form_valid(form)
2023-10-17 08:53:03 +00:00
2023-11-03 15:42:45 +00:00
class PeopleMembershipDeleteView(PeopleView):
2023-10-17 08:53:03 +00:00
model = Membership
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-17 08:53:03 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
if self.object != self.request.user:
user = self.object.user
self.object.delete()
else:
messages.error(self.request, _('Is not possible delete your account!'))
return redirect('idhub:admin_people_edit', user.id)
2023-10-17 15:42:48 +00:00
2023-11-14 14:55:17 +00:00
class PeopleRolRegisterView(People, FormView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_rol_register.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Add a user role to access a service')
2023-10-17 15:42:48 +00:00
icon = 'bi bi-person'
2023-11-14 14:55:17 +00:00
form_class = UserRolForm
2023-10-17 15:42:48 +00:00
model = UserRol
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().post(request, *args, **kwargs)
def get_form_kwargs(self):
self.object = self.model(user=self.user)
kwargs = super().get_form_kwargs()
2023-11-14 14:55:17 +00:00
kwargs['instance'] = self.object
2023-10-17 15:42:48 +00:00
return kwargs
2023-11-14 14:55:17 +00:00
def form_valid(self, form):
form.save()
messages.success(self.request, _('Membership created successfully'))
return super().form_valid(form)
2023-11-13 17:09:37 +00:00
2023-10-17 15:42:48 +00:00
def get_success_url(self):
self.success_url = reverse_lazy(
'idhub:admin_people_edit',
kwargs={"pk": self.user.id}
)
return self.success_url
2023-11-14 14:55:17 +00:00
class PeopleRolEditView(People, FormView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_rol_register.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Modify a user role to access a service')
2023-10-17 15:42:48 +00:00
icon = 'bi bi-person'
2023-11-14 14:55:17 +00:00
form_class = UserRolForm
2023-10-17 15:42:48 +00:00
model = UserRol
2023-11-14 14:55:17 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
return super().post(request, *args, **kwargs)
2023-10-17 15:42:48 +00:00
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
2023-11-14 14:55:17 +00:00
kwargs['instance'] = self.object
2023-10-17 15:42:48 +00:00
return kwargs
2023-11-14 14:55:17 +00:00
def form_valid(self, form):
form.save()
messages.success(self.request, _('Membership updated successfully'))
return super().form_valid(form)
2023-10-17 15:42:48 +00:00
def get_success_url(self):
self.success_url = reverse_lazy(
'idhub:admin_people_edit',
kwargs={"pk": self.object.user.id}
)
return self.success_url
2023-11-03 15:42:45 +00:00
class PeopleRolDeleteView(PeopleView):
2023-10-17 15:42:48 +00:00
model = UserRol
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-17 15:42:48 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
user = self.object.user
self.object.delete()
return redirect('idhub:admin_people_edit', user.id)
2023-10-09 15:59:15 +00:00
2023-11-30 16:05:31 +00:00
class RolesView(AccessControl, SingleTableView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/roles.html"
2023-11-13 10:12:40 +00:00
subtitle = _('Manage roles')
2023-11-30 16:05:31 +00:00
table_class = RolesTable
2023-10-09 16:12:18 +00:00
icon = ''
2023-11-30 16:05:31 +00:00
model = Rol
2023-10-09 15:59:15 +00:00
2023-10-17 11:40:34 +00:00
def get_context_data(self, **kwargs):
2023-11-30 16:05:31 +00:00
queryset = kwargs.pop('object_list', None)
if queryset is None:
self.object_list = self.model.objects.all()
return super().get_context_data(**kwargs)
2023-10-17 11:40:34 +00:00
2023-11-30 16:05:31 +00:00
2023-11-03 15:42:45 +00:00
class RolRegisterView(AccessControl, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/rol_register.html"
2023-11-21 11:38:12 +00:00
subtitle = _('Add role')
2023-10-17 13:49:56 +00:00
icon = ''
2023-10-17 11:40:34 +00:00
model = Rol
2023-11-14 14:55:17 +00:00
fields = ('name', "description")
2023-10-17 11:40:34 +00:00
success_url = reverse_lazy('idhub:admin_roles')
object = None
2023-11-09 16:58:06 +00:00
def form_valid(self, form):
form.save()
2023-11-13 09:15:52 +00:00
messages.success(self.request, _('Role created successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_ROLE_CREATED_BY_ADMIN()
return super().form_valid(form)
2023-10-17 11:40:34 +00:00
2023-11-14 14:55:17 +00:00
class RolEditView(AccessControl, UpdateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/rol_register.html"
2023-11-21 11:38:12 +00:00
subtitle = _('Edit role')
2023-10-17 13:49:56 +00:00
icon = ''
2023-10-17 11:40:34 +00:00
model = Rol
2023-11-14 14:55:17 +00:00
fields = ('name', "description")
2023-10-17 11:40:34 +00:00
success_url = reverse_lazy('idhub:admin_roles')
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
if pk:
self.object = get_object_or_404(self.model, pk=pk)
kwargs = super().get_form_kwargs()
return kwargs
2023-11-09 16:58:06 +00:00
def form_valid(self, form):
form.save()
2023-11-13 09:15:52 +00:00
messages.success(self.request, _('Role updated successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_ROLE_MODIFIED_BY_ADMIN()
return super().form_valid(form)
2023-10-17 11:40:34 +00:00
2023-11-03 15:42:45 +00:00
class RolDeleteView(AccessControl):
2023-10-17 11:40:34 +00:00
model = Rol
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-17 11:40:34 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete()
2023-11-13 09:15:52 +00:00
messages.success(self.request, _('Role deleted successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_ROLE_DELETED_BY_ADMIN()
2023-10-17 11:40:34 +00:00
return redirect('idhub:admin_roles')
2023-12-01 11:08:22 +00:00
class ServicesView(AccessControl, SingleTableView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/services.html"
2023-12-01 11:08:22 +00:00
table_class = ServicesTable
2023-11-13 10:12:40 +00:00
subtitle = _('Manage services')
2023-10-09 16:12:18 +00:00
icon = ''
2023-12-01 11:08:22 +00:00
model = Service
2023-10-09 15:59:15 +00:00
2023-10-17 13:49:56 +00:00
def get_context_data(self, **kwargs):
2023-12-01 11:08:22 +00:00
queryset = kwargs.pop('object_list', None)
if queryset is None:
self.object_list = self.model.objects.all()
return super().get_context_data(**kwargs)
2023-10-17 13:49:56 +00:00
2023-11-03 15:42:45 +00:00
class ServiceRegisterView(AccessControl, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/service_register.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Add service')
2023-10-17 13:49:56 +00:00
icon = ''
model = Service
fields = ('domain', 'description', 'rol')
success_url = reverse_lazy('idhub:admin_services')
object = None
2023-11-14 16:45:08 +00:00
def get_form(self):
form = super().get_form()
form.fields['rol'].required = False
return form
2023-11-09 16:58:06 +00:00
def form_valid(self, form):
form.save()
messages.success(self.request, _('Service created successfully'))
Event.set_EV_SERVICE_CREATED_BY_ADMIN()
return super().form_valid(form)
2023-10-17 13:49:56 +00:00
2023-11-14 14:55:17 +00:00
class ServiceEditView(AccessControl, UpdateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/service_register.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Modify service')
2023-10-17 13:49:56 +00:00
icon = ''
model = Service
fields = ('domain', 'description', 'rol')
success_url = reverse_lazy('idhub:admin_services')
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
if pk:
self.object = get_object_or_404(self.model, pk=pk)
kwargs = super().get_form_kwargs()
return kwargs
2023-11-14 16:45:08 +00:00
def get_form(self):
form = super().get_form()
form.fields['rol'].required = False
return form
2023-11-09 16:58:06 +00:00
def form_valid(self, form):
form.save()
messages.success(self.request, _('Service updated successfully'))
Event.set_EV_SERVICE_MODIFIED_BY_ADMIN()
return super().form_valid(form)
2023-10-17 13:49:56 +00:00
2023-11-03 15:42:45 +00:00
class ServiceDeleteView(AccessControl):
2023-10-17 13:49:56 +00:00
model = Service
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-17 13:49:56 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete()
2023-11-09 16:58:06 +00:00
messages.success(self.request, _('Service deleted successfully'))
Event.set_EV_SERVICE_DELETED_BY_ADMIN()
2023-10-17 13:49:56 +00:00
return redirect('idhub:admin_services')
2023-10-09 15:59:15 +00:00
class CredentialsView(Credentials, SingleTableView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/credentials.html"
2023-12-01 11:08:22 +00:00
table_class = CredentialTable
2023-11-13 10:12:40 +00:00
subtitle = _('View credentials')
2023-10-09 16:12:18 +00:00
icon = ''
2023-12-01 11:08:22 +00:00
model = VerificableCredential
2023-10-09 15:59:15 +00:00
2023-10-26 11:33:13 +00:00
def get_context_data(self, **kwargs):
queryset = kwargs.pop('object_list', None)
if queryset is None:
self.object_list = self.model.objects.all()
return super().get_context_data(**kwargs)
2023-10-26 11:33:13 +00:00
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class CredentialView(Credentials):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/issue_credentials.html"
2023-11-21 11:38:12 +00:00
subtitle = _('Change credential status')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-31 09:59:19 +00:00
model = VerificableCredential
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.object,
})
return context
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class CredentialJsonView(Credentials):
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-11-03 15:42:45 +00:00
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
)
response = HttpResponse(self.object.get_data(), content_type="application/json")
2023-11-03 15:42:45 +00:00
response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json")
return response
class RevokeCredentialsView(Credentials):
2023-11-03 16:10:09 +00:00
success_url = reverse_lazy('idhub:admin_credentials')
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-11-03 16:10:09 +00:00
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
)
if self.object.status == VerificableCredential.Status.ISSUED:
self.object.status = VerificableCredential.Status.REVOKED
self.object.save()
messages.success(self.request, _('Credential revoked successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_CREDENTIAL_REVOKED_BY_ADMIN(self.object)
Event.set_EV_CREDENTIAL_REVOKED(self.object)
2023-11-03 16:10:09 +00:00
return redirect(self.success_url)
class DeleteCredentialsView(Credentials):
success_url = reverse_lazy('idhub:admin_credentials')
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-11-03 16:10:09 +00:00
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
)
2024-02-21 09:46:33 +00:00
self.object.delete()
messages.success(self.request, _('Credential deleted successfully'))
Event.set_EV_CREDENTIAL_DELETED(self.object)
Event.set_EV_CREDENTIAL_DELETED_BY_ADMIN(self.object)
2023-11-03 16:10:09 +00:00
return redirect(self.success_url)
2023-10-09 15:59:15 +00:00
2023-12-01 11:08:22 +00:00
class DidsView(Credentials, SingleTableView):
2023-10-26 16:06:52 +00:00
template_name = "idhub/admin/dids.html"
2023-12-01 11:08:22 +00:00
table_class = DIDTable
2023-11-21 11:38:12 +00:00
subtitle = _('Manage identities (DID)')
2023-10-09 16:12:18 +00:00
icon = 'bi bi-patch-check-fill'
2023-10-09 16:38:28 +00:00
wallet = True
2023-12-01 11:08:22 +00:00
model = DID
2023-10-09 15:59:15 +00:00
2023-10-26 11:33:13 +00:00
def get_context_data(self, **kwargs):
2023-12-01 11:08:22 +00:00
queryset = kwargs.pop('object_list', None)
2024-02-06 17:13:57 +00:00
dids = DID.objects.filter(user=self.request.user)
2023-12-01 11:08:22 +00:00
if queryset is None:
2024-02-06 17:13:57 +00:00
self.object_list = dids.all()
2023-12-01 11:08:22 +00:00
2023-10-26 11:33:13 +00:00
context = super().get_context_data(**kwargs)
context.update({
2024-02-06 17:13:57 +00:00
'dids': dids
2023-10-26 11:33:13 +00:00
})
return context
2023-12-01 11:08:22 +00:00
2023-11-03 15:42:45 +00:00
class DidRegisterView(Credentials, CreateView):
2023-10-26 16:06:52 +00:00
template_name = "idhub/admin/did_register.html"
2023-11-21 11:38:12 +00:00
subtitle = _('Add a new organizational identity (DID)')
2023-10-26 16:06:52 +00:00
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
2024-01-16 13:00:05 +00:00
fields = ('label', 'type')
2023-10-26 16:06:52 +00:00
success_url = reverse_lazy('idhub:admin_dids')
object = None
def form_valid(self, form):
2023-11-03 15:27:40 +00:00
form.instance.user = self.request.user
form.instance.set_did()
2023-10-27 09:19:10 +00:00
form.save()
2023-10-26 16:06:52 +00:00
messages.success(self.request, _('DID created successfully'))
2023-11-09 16:58:06 +00:00
Event.set_EV_ORG_DID_CREATED_BY_ADMIN(form.instance)
2023-10-26 16:06:52 +00:00
return super().form_valid(form)
2023-11-03 15:42:45 +00:00
class DidEditView(Credentials, UpdateView):
2023-10-26 16:06:52 +00:00
template_name = "idhub/admin/did_register.html"
subtitle = _('Organization Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
2023-11-03 15:27:40 +00:00
fields = ('label',)
2023-10-26 16:06:52 +00:00
success_url = reverse_lazy('idhub:admin_dids')
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
return super().get(request, *args, **kwargs)
def form_valid(self, form):
form.save()
2023-10-27 09:19:10 +00:00
messages.success(self.request, _('DID updated successfully'))
2023-10-26 16:06:52 +00:00
return super().form_valid(form)
2023-11-03 15:42:45 +00:00
class DidDeleteView(Credentials, DeleteView):
2023-10-26 16:06:52 +00:00
subtitle = _('Organization Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
success_url = reverse_lazy('idhub:admin_dids')
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-26 16:06:52 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
2023-11-09 16:58:06 +00:00
Event.set_EV_ORG_DID_DELETED_BY_ADMIN(self.object)
2023-10-26 16:06:52 +00:00
self.object.delete()
2023-10-27 09:19:10 +00:00
messages.success(self.request, _('DID delete successfully'))
2023-10-26 16:06:52 +00:00
return redirect(self.success_url)
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class WalletCredentialsView(Credentials):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/wallet_credentials.html"
2023-11-13 10:12:40 +00:00
subtitle = _('View org. credentials')
2023-10-09 16:12:18 +00:00
icon = 'bi bi-patch-check-fill'
2023-10-09 16:38:28 +00:00
wallet = True
2023-10-09 15:59:15 +00:00
2024-01-10 12:53:43 +00:00
class WalletConfigIssuesView(Credentials, FormView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/wallet_issues.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Configure credential issuance')
2023-10-09 16:12:18 +00:00
icon = 'bi bi-patch-check-fill'
2023-10-09 16:38:28 +00:00
wallet = True
2024-01-10 12:53:43 +00:00
form_class = ImportCertificateForm
success_url = reverse_lazy('idhub:admin_dids')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
cred = form.save()
if cred:
messages.success(self.request, _("The credential was imported successfully!"))
Event.set_EV_ORG_DID_CREATED_BY_ADMIN(cred)
else:
messages.error(self.request, _("Error importing the credential!"))
return super().form_valid(form)
2023-10-09 15:59:15 +00:00
2023-12-15 17:40:56 +00:00
class SchemasView(SchemasMix, SingleTableView):
2023-10-20 14:50:53 +00:00
template_name = "idhub/admin/schemas.html"
2023-12-15 17:40:56 +00:00
table_class = TemplateTable
2023-11-13 09:15:52 +00:00
subtitle = _('View credential templates')
2023-10-09 16:12:18 +00:00
icon = ''
2023-12-15 17:40:56 +00:00
model = Schemas
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
def get_context_data(self, **kwargs):
2023-12-15 17:40:56 +00:00
queryset = kwargs.pop('object_list', None)
if queryset is None:
self.object_list = self.model.objects.all()
return super().get_context_data(**kwargs)
2023-10-20 14:50:53 +00:00
2023-11-03 15:42:45 +00:00
class SchemasDeleteView(SchemasMix):
2023-10-23 17:24:48 +00:00
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-23 17:24:48 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(Schemas, pk=self.pk)
self.object.delete()
return redirect('idhub:admin_schemas')
2023-11-03 15:42:45 +00:00
class SchemasDownloadView(SchemasMix):
2023-10-23 17:24:48 +00:00
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2023-10-23 17:24:48 +00:00
self.pk = kwargs['pk']
self.object = get_object_or_404(Schemas, pk=self.pk)
response = HttpResponse(self.object.data, content_type="application/json")
response['Content-Disposition'] = 'inline; filename={}'.format(self.object.file_schema)
return response
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class SchemasNewView(SchemasMix):
2023-10-22 18:01:41 +00:00
template_name = "idhub/admin/schemas_new.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Upload template')
2023-10-22 18:01:41 +00:00
icon = ''
success_url = reverse_lazy('idhub:admin_schemas')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form': SchemaForm(),
})
return context
def post(self, request, *args, **kwargs):
form = SchemaForm(request.POST, request.FILES)
if form.is_valid():
schema = self.handle_uploaded_file()
if not schema:
messages.error(request, _("There are some errors in the file"))
return super().get(request, *args, **kwargs)
return redirect(self.success_url)
else:
return super().get(request, *args, **kwargs)
return super().post(request, *args, **kwargs)
def handle_uploaded_file(self):
f = self.request.FILES.get('file_template')
if not f:
return
file_name = f.name
if Schemas.objects.filter(file_schema=file_name).exists():
messages.error(self.request, _("This template already exists!"))
return
2023-10-24 12:08:24 +00:00
try:
data = f.read().decode('utf-8')
2023-11-29 16:29:31 +00:00
ldata = json.loads(data)
assert credtools.validate_schema(ldata)
name = ldata.get('name')
assert name
2023-10-24 12:08:24 +00:00
except Exception:
2023-11-21 11:38:12 +00:00
messages.error(self.request, _('This is not a valid schema!'))
2023-10-24 12:08:24 +00:00
return
2023-11-29 16:29:31 +00:00
schema = Schemas.objects.create(file_schema=file_name, data=data, type=name)
2023-10-22 18:01:41 +00:00
schema.save()
return schema
2023-11-03 15:42:45 +00:00
class SchemasImportView(SchemasMix):
2023-10-20 14:50:53 +00:00
template_name = "idhub/admin/schemas_import.html"
2023-11-13 09:15:52 +00:00
subtitle = _('Import template')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'schemas': self.get_schemas(),
})
return context
def get_schemas(self):
schemas_files = os.listdir(settings.SCHEMAS_DIR)
schemas = [x for x in schemas_files
if not Schemas.objects.filter(file_schema=x).exists()]
return schemas
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class SchemasImportAddView(SchemasMix):
2023-10-20 14:50:53 +00:00
def get(self, request, *args, **kwargs):
2024-01-20 11:26:19 +00:00
self.check_valid_user()
2024-02-02 15:16:35 +00:00
self.file_name = kwargs['file_schema']
2023-10-20 14:50:53 +00:00
schemas_files = os.listdir(settings.SCHEMAS_DIR)
2024-02-02 15:16:35 +00:00
if self.file_name not in schemas_files:
file_name = self.file_name
2023-10-20 14:50:53 +00:00
messages.error(self.request, f"The schema {file_name} not exist!")
return redirect('idhub:admin_schemas_import')
2024-02-02 15:16:35 +00:00
schema = self.create_schema()
2023-10-24 12:08:24 +00:00
if schema:
2023-11-13 09:15:52 +00:00
messages.success(self.request, _("The schema was added sucessfully"))
2023-11-22 11:52:40 +00:00
return redirect('idhub:admin_schemas')
2023-10-20 14:50:53 +00:00
2024-02-02 15:16:35 +00:00
def create_schema(self):
data = self.open_file()
2023-10-24 12:08:24 +00:00
try:
2023-11-29 16:29:31 +00:00
ldata = json.loads(data)
assert credtools.validate_schema(ldata)
name = ldata.get('name')
2024-01-21 12:43:29 +00:00
title = ldata.get('title')
2023-11-29 16:29:31 +00:00
assert name
2024-01-21 12:43:29 +00:00
assert title
2023-10-24 12:08:24 +00:00
except Exception:
2023-11-21 11:38:12 +00:00
messages.error(self.request, _('This is not a valid schema!'))
2023-10-24 12:08:24 +00:00
return
2024-01-22 17:13:06 +00:00
_name = json.dumps(ldata.get('name', ''))
_description = json.dumps(ldata.get('description', ''))
2024-01-22 13:04:06 +00:00
schema = Schemas.objects.create(
2024-02-02 15:16:35 +00:00
file_schema=self.file_name,
2024-01-22 13:04:06 +00:00
data=data,
2024-01-22 17:13:06 +00:00
type=title,
_name=_name,
_description=_description,
# template_description=_description
2024-01-22 13:04:06 +00:00
template_description=self.get_description()
)
2023-10-20 14:50:53 +00:00
schema.save()
return schema
2024-02-02 15:16:35 +00:00
def open_file(self):
2023-10-20 14:50:53 +00:00
data = ''
2024-02-02 15:16:35 +00:00
filename = Path(settings.SCHEMAS_DIR).joinpath(self.file_name)
2023-10-20 14:50:53 +00:00
with filename.open() as schema_file:
data = schema_file.read()
return data
def get_template_description(self):
2024-01-22 17:13:06 +00:00
context = {}
template_name = 'credentials/{}'.format(
2024-02-02 15:16:35 +00:00
self.file_name
)
tmpl = get_template(template_name)
return tmpl.render(context)
def get_description(self):
2024-01-22 17:13:06 +00:00
for des in json.loads(self.get_template_description()).get('description', []):
if settings.LANGUAGE_CODE == des.get('lang'):
return des.get('value', '')
return ''
2023-10-20 14:50:53 +00:00
2023-12-06 10:45:04 +00:00
class ImportView(ImportExport, SingleTableView):
2023-10-24 12:08:24 +00:00
template_name = "idhub/admin/import.html"
2023-12-06 10:45:04 +00:00
table_class = DataTable
2024-02-05 11:27:45 +00:00
subtitle = _('Imported data')
2023-10-09 16:12:18 +00:00
icon = ''
2023-12-06 10:45:04 +00:00
model = File_datas
2023-10-09 15:59:15 +00:00
2023-10-24 12:08:24 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
2023-10-25 10:18:42 +00:00
'dates': File_datas.objects,
2023-10-24 12:08:24 +00:00
})
return context
2023-10-09 15:59:15 +00:00
2023-10-24 12:08:24 +00:00
2023-11-08 10:24:37 +00:00
class ImportStep2View(ImportExport, TemplateView):
2023-10-24 12:08:24 +00:00
template_name = "idhub/admin/import_step2.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Import')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-23 17:24:48 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'schemas': Schemas.objects,
})
return context
2023-10-24 12:08:24 +00:00
class ImportAddView(NotifyActivateUserByEmail, ImportExport, FormView):
2023-11-08 10:24:37 +00:00
template_name = "idhub/admin/import_add.html"
2023-10-23 17:24:48 +00:00
subtitle = _('Import')
icon = ''
2023-11-08 10:24:37 +00:00
form_class = ImportForm
2023-10-23 17:24:48 +00:00
success_url = reverse_lazy('idhub:admin_import')
2023-11-08 10:24:37 +00:00
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
2023-10-23 17:24:48 +00:00
2023-11-08 10:24:37 +00:00
def form_valid(self, form):
2023-11-09 16:58:06 +00:00
creds = form.save()
if creds:
2023-11-21 11:38:12 +00:00
messages.success(self.request, _("The file was imported successfully!"))
2023-11-09 16:58:06 +00:00
for cred in creds:
Event.set_EV_CREDENTIAL_ENABLED(cred)
Event.set_EV_CREDENTIAL_CAN_BE_REQUESTED(cred)
2023-10-23 17:24:48 +00:00
else:
2023-11-08 10:24:37 +00:00
messages.error(self.request, _("Error importing the file!"))
for user in form.users:
try:
self.send_email(user)
except SMTPException as e:
messages.error(self.request, e)
2023-11-08 10:24:37 +00:00
return super().form_valid(form)