diff --git a/apiregiter.py b/apiregiter.py new file mode 100644 index 0000000..e794181 --- /dev/null +++ b/apiregiter.py @@ -0,0 +1,17 @@ +import uuid +import hashlib + + +class Iota: + """ + Framework for simulate the comunication with IOTA DLT + """ + + def issue_did(self): + u = str(uuid.uuid4()).encode() + d = hashlib.sha3_256(u).hexdigest() + did = "did:iota:{}".format(d) + return did + + +iota = Iota() diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py deleted file mode 100644 index dd531a4..0000000 --- a/idhub/admin/forms.py +++ /dev/null @@ -1,34 +0,0 @@ -from django import forms -from idhub_auth.models import User -from idhub.models import Rol - - -class ProfileForm(forms.ModelForm): - MANDATORY_FIELDS = ['first_name', 'last_name', 'email'] - - class Meta: - model = User - fields = ('first_name', 'last_name', 'email') - - -class MembershipForm(forms.ModelForm): - MANDATORY_FIELDS = ['type'] - - -class RolForm(forms.ModelForm): - MANDATORY_FIELDS = ['name'] - - -class ServiceForm(forms.ModelForm): - MANDATORY_FIELDS = ['domain', 'rol'] - - -class UserRolForm(forms.ModelForm): - MANDATORY_FIELDS = ['service'] - - -class SchemaForm(forms.Form): - file_template = forms.FileField() - -class ImportForm(forms.Form): - file_import = forms.FileField() diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 480895d..35c7f9c 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -11,11 +11,12 @@ from smtplib import SMTPException from django.conf import settings from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView -from django.views.generic.edit import UpdateView, CreateView +from django.views.generic.edit import UpdateView, CreateView, DeleteView from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages +from apiregiter import iota from idhub_auth.models import User from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail @@ -29,15 +30,6 @@ from idhub.models import ( UserRol, VerifiableCredential, ) -from idhub.admin.forms import ( - ProfileForm, - MembershipForm, - RolForm, - ServiceForm, - UserRolForm, - SchemaForm, - ImportForm, -) class AdminDashboardView(AdminView, TemplateView): @@ -138,7 +130,6 @@ class AdminPeopleDeleteView(AdminPeopleView): class AdminPeopleEditView(AdminPeopleView, UpdateView): template_name = "idhub/admin/user_edit.html" - from_class = ProfileForm fields = ('first_name', 'last_name', 'email') success_url = reverse_lazy('idhub:admin_people_list') @@ -148,7 +139,6 @@ class AdminPeopleRegisterView(NotifyActivateUserByEmail, People, CreateView): subtitle = _('People Register') icon = 'bi bi-person' model = User - from_class = ProfileForm fields = ('first_name', 'last_name', 'email') success_url = reverse_lazy('idhub:admin_people_list') @@ -175,7 +165,6 @@ class AdminPeopleMembershipRegisterView(People, CreateView): subtitle = _('People add membership') icon = 'bi bi-person' model = Membership - from_class = MembershipForm fields = ('type', 'start_date', 'end_date') success_url = reverse_lazy('idhub:admin_people_list') @@ -213,7 +202,6 @@ class AdminPeopleMembershipEditView(People, CreateView): subtitle = _('People add membership') icon = 'bi bi-person' model = Membership - from_class = MembershipForm fields = ('type', 'start_date', 'end_date') success_url = reverse_lazy('idhub:admin_people_list') @@ -252,7 +240,6 @@ class AdminPeopleRolRegisterView(People, CreateView): subtitle = _('Add Rol to User') icon = 'bi bi-person' model = UserRol - from_class = UserRolForm fields = ('service',) def get(self, request, *args, **kwargs): @@ -283,7 +270,6 @@ class AdminPeopleRolEditView(People, CreateView): subtitle = _('Edit Rol to User') icon = 'bi bi-person' model = UserRol - from_class = UserRolForm fields = ('service',) def get_form_kwargs(self): @@ -331,7 +317,6 @@ class AdminRolRegisterView(AccessControl, CreateView): subtitle = _('Add Rol') icon = '' model = Rol - from_class = RolForm fields = ('name',) success_url = reverse_lazy('idhub:admin_roles') object = None @@ -342,7 +327,6 @@ class AdminRolEditView(AccessControl, CreateView): subtitle = _('Edit Rol') icon = '' model = Rol - from_class = RolForm fields = ('name',) success_url = reverse_lazy('idhub:admin_roles') @@ -382,7 +366,6 @@ class AdminServiceRegisterView(AccessControl, CreateView): subtitle = _('Add Service') icon = '' model = Service - from_class = ServiceForm fields = ('domain', 'description', 'rol') success_url = reverse_lazy('idhub:admin_services') object = None @@ -393,7 +376,6 @@ class AdminServiceEditView(AccessControl, CreateView): subtitle = _('Edit Service') icon = '' model = Service - from_class = ServiceForm fields = ('domain', 'description', 'rol') success_url = reverse_lazy('idhub:admin_services') @@ -441,8 +423,8 @@ class AdminRevokeCredentialsView(Credentials): icon = '' -class AdminWalletIdentitiesView(Credentials): - template_name = "idhub/admin/wallet_identities.html" +class AdminDidsView(Credentials): + template_name = "idhub/admin/dids.html" subtitle = _('Organization Identities (DID)') icon = 'bi bi-patch-check-fill' wallet = True @@ -454,6 +436,76 @@ class AdminWalletIdentitiesView(Credentials): }) return context +class AdminDidRegisterView(Credentials, CreateView): + template_name = "idhub/admin/did_register.html" + subtitle = _('Add a new Organization Identities (DID)') + icon = 'bi bi-patch-check-fill' + wallet = True + model = DID + fields = ('did', 'label') + success_url = reverse_lazy('idhub:admin_dids') + object = None + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs['initial'] = { + 'did': iota.issue_did() + } + return kwargs + + def get_form(self): + form = super().get_form() + form.fields['did'].required = False + form.fields['did'].disabled = True + return form + + def form_valid(self, form): + user = form.save() + messages.success(self.request, _('DID created successfully')) + return super().form_valid(form) + + +class AdminDidEditView(Credentials, UpdateView): + template_name = "idhub/admin/did_register.html" + subtitle = _('Organization Identities (DID)') + icon = 'bi bi-patch-check-fill' + wallet = True + model = DID + fields = ('did', 'label') + 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 get_form(self): + form = super().get_form() + form.fields['did'].required = False + form.fields['did'].disabled = True + return form + + def form_valid(self, form): + user = form.save() + messages.success(self.request, _('DID created successfully')) + return super().form_valid(form) + + +class AdminDidDeleteView(Credentials, DeleteView): + 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): + # import pdb; pdb.set_trace() + self.pk = kwargs['pk'] + self.object = get_object_or_404(self.model, pk=self.pk) + self.object.delete() + + return redirect(self.success_url) + class AdminWalletCredentialsView(Credentials): template_name = "idhub/admin/wallet_credentials.html" diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index bb9874b..26780eb 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-26 11:29 +# Generated by Django 4.2.5 on 2023-10-26 13:46 from django.conf import settings from django.db import migrations, models @@ -225,7 +225,8 @@ class Migration(migrations.Migration): verbose_name='ID', ), ), - ('did_string', models.CharField(max_length=250)), + ('created_at', models.DateTimeField(auto_now=True)), + ('did', models.CharField(max_length=250, unique=True)), ('label', models.CharField(max_length=50)), ( 'user', diff --git a/idhub/models.py b/idhub/models.py index 774426b..2570d34 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -4,15 +4,6 @@ from django.utils.translation import gettext_lazy as _ from idhub_auth.models import User -# class AppUser(models.Model): - # Ya incluye "first_name", "last_name", "email", y "date_joined" heredando de la clase User de django. - # Falta ver que más información hay que añadir a nuestros usuarios, como los roles etc. - # django_user = models.OneToOneField(DjangoUser, on_delete=models.CASCADE) - - # Extra data, segun entidad/organizacion - # pass - - # class Event(models.Model): # Para los "audit logs" que se requieren en las pantallas. # timestamp = models.DateTimeField() @@ -21,7 +12,8 @@ from idhub_auth.models import User class DID(models.Model): - did_string = models.CharField(max_length=250) + created_at = models.DateTimeField(auto_now=True) + did = models.CharField(max_length=250, unique=True) label = models.CharField(max_length=50) user = models.ForeignKey( User, diff --git a/idhub/templates/idhub/admin/did_register.html b/idhub/templates/idhub/admin/did_register.html new file mode 100644 index 0000000..a4fe719 --- /dev/null +++ b/idhub/templates/idhub/admin/did_register.html @@ -0,0 +1,34 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +
+
+ {% bootstrap_form form %} +
+
+
+ {% translate "Cancel" %} + +
+ +
+{% endblock %} diff --git a/idhub/templates/idhub/admin/wallet_identities.html b/idhub/templates/idhub/admin/dids.html similarity index 84% rename from idhub/templates/idhub/admin/wallet_identities.html rename to idhub/templates/idhub/admin/dids.html index 0a1295b..b7156c0 100644 --- a/idhub/templates/idhub/admin/wallet_identities.html +++ b/idhub/templates/idhub/admin/dids.html @@ -24,15 +24,15 @@ {{ d.created_at }} {{ d.label }} - {{ d.id }} - + {{ d.did }} + {% endfor %}
- {% translate "Add Identity" %} + {% translate "Add Identity" %}
@@ -51,7 +51,7 @@ diff --git a/idhub/templates/idhub/base_admin.html b/idhub/templates/idhub/base_admin.html index bece139..91715e2 100644 --- a/idhub/templates/idhub/base_admin.html +++ b/idhub/templates/idhub/base_admin.html @@ -123,7 +123,7 @@