diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index c6f90bc..74046df 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -21,3 +21,5 @@ class RolForm(forms.ModelForm): class ServiceForm(forms.ModelForm): MANDATORY_FIELDS = ['domain', 'rol'] +class UserRolForm(forms.ModelForm): + MANDATORY_FIELDS = ['service'] diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 780902d..0e6f122 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -7,13 +7,19 @@ from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.contrib import messages -from idhub.models import Membership, Rol, Service +from idhub.models import Membership, Rol, Service, UserRol from idhub.mixins import AdminView -from idhub.admin.forms import ProfileForm, MembershipForm, RolForm, ServiceForm +from idhub.admin.forms import ( + ProfileForm, + MembershipForm, + RolForm, + ServiceForm, + UserRolForm +) class AdminDashboardView(AdminView, TemplateView): - template_name = "idhub/admin_dashboard.html" + template_name = "idhub/admin/dashboard.html" title = _('Dashboard') subtitle = _('Success') icon = 'bi bi-bell' @@ -45,7 +51,7 @@ class ImportExport(AdminView, TemplateView): class AdminPeopleListView(People, TemplateView): - template_name = "idhub/admin_people.html" + template_name = "idhub/admin/people.html" subtitle = _('People list') icon = 'bi bi-person' @@ -58,7 +64,7 @@ class AdminPeopleListView(People, TemplateView): class AdminPeopleView(People, TemplateView): - template_name = "idhub/admin_user.html" + template_name = "idhub/admin/user.html" subtitle = _('User Profile') icon = 'bi bi-person' model = User @@ -109,14 +115,14 @@ class AdminPeopleDeleteView(AdminPeopleView): return redirect('idhub:admin_people_list') class AdminPeopleEditView(AdminPeopleView, UpdateView): - template_name = "idhub/admin_user_edit.html" + template_name = "idhub/admin/user_edit.html" from_class = ProfileForm fields = ('first_name', 'last_name', 'email', 'username') success_url = reverse_lazy('idhub:admin_people_list') class AdminPeopleRegisterView(People, CreateView): - template_name = "idhub/admin_people_register.html" + template_name = "idhub/admin/people_register.html" subtitle = _('People Register') icon = 'bi bi-person' model = User @@ -133,7 +139,7 @@ class AdminPeopleRegisterView(People, CreateView): class AdminPeopleMembershipRegisterView(People, CreateView): - template_name = "idhub/admin_people_membership_register.html" + template_name = "idhub/admin/people_membership_register.html" subtitle = _('People add membership') icon = 'bi bi-person' model = Membership @@ -164,14 +170,14 @@ class AdminPeopleMembershipRegisterView(People, CreateView): def get_success_url(self): self.success_url = reverse_lazy( - 'idhub:admin_people_edit', + 'idhub:admin_people_rol_new', kwargs={"pk": self.user.id} ) return self.success_url class AdminPeopleMembershipEditView(People, CreateView): - template_name = "idhub/admin_people_membership_register.html" + template_name = "idhub/admin/people_membership_register.html" subtitle = _('People add membership') icon = 'bi bi-person' model = Membership @@ -208,9 +214,76 @@ class AdminPeopleMembershipDeleteView(AdminPeopleView): return redirect('idhub:admin_people_edit', user.id) + +class AdminPeopleRolRegisterView(People, CreateView): + template_name = "idhub/admin/people_rol_register.html" + subtitle = _('Add Rol to User') + icon = 'bi bi-person' + model = UserRol + from_class = UserRolForm + fields = ('service',) + + 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() + return kwargs + + def get_success_url(self): + self.success_url = reverse_lazy( + 'idhub:admin_people_edit', + kwargs={"pk": self.user.id} + ) + return self.success_url + + +class AdminPeopleRolEditView(People, CreateView): + template_name = "idhub/admin/people_rol_register.html" + subtitle = _('Edit Rol to User') + icon = 'bi bi-person' + model = UserRol + from_class = UserRolForm + fields = ('service',) + + 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 + + def get_success_url(self): + self.success_url = reverse_lazy( + 'idhub:admin_people_edit', + kwargs={"pk": self.object.user.id} + ) + return self.success_url + + +class AdminPeopleRolDeleteView(AdminPeopleView): + model = UserRol + + def get(self, request, *args, **kwargs): + 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) + class AdminRolesView(AccessControl): - template_name = "idhub/admin_roles.html" + template_name = "idhub/admin/roles.html" subtitle = _('Roles Management') icon = '' @@ -222,7 +295,7 @@ class AdminRolesView(AccessControl): return context class AdminRolRegisterView(AccessControl, CreateView): - template_name = "idhub/admin_rol_register.html" + template_name = "idhub/admin/rol_register.html" subtitle = _('Add Rol') icon = '' model = Rol @@ -233,7 +306,7 @@ class AdminRolRegisterView(AccessControl, CreateView): class AdminRolEditView(AccessControl, CreateView): - template_name = "idhub/admin_rol_register.html" + template_name = "idhub/admin/rol_register.html" subtitle = _('Edit Rol') icon = '' model = Rol @@ -261,7 +334,7 @@ class AdminRolDeleteView(AccessControl): class AdminServicesView(AccessControl): - template_name = "idhub/admin_services.html" + template_name = "idhub/admin/services.html" subtitle = _('Service Management') icon = '' @@ -273,7 +346,7 @@ class AdminServicesView(AccessControl): return context class AdminServiceRegisterView(AccessControl, CreateView): - template_name = "idhub/admin_service_register.html" + template_name = "idhub/admin/service_register.html" subtitle = _('Add Service') icon = '' model = Service @@ -284,7 +357,7 @@ class AdminServiceRegisterView(AccessControl, CreateView): class AdminServiceEditView(AccessControl, CreateView): - template_name = "idhub/admin_service_register.html" + template_name = "idhub/admin/service_register.html" subtitle = _('Edit Service') icon = '' model = Service @@ -312,69 +385,69 @@ class AdminServiceDeleteView(AccessControl): class AdminCredentialsView(Credentials): - template_name = "idhub/admin_credentials.html" + template_name = "idhub/admin/credentials.html" subtitle = _('Credentials list') icon = '' class AdminIssueCredentialsView(Credentials): - template_name = "idhub/admin_issue_credentials.html" + template_name = "idhub/admin/issue_credentials.html" subtitle = _('Issuance of Credentials') icon = '' class AdminRevokeCredentialsView(Credentials): - template_name = "idhub/admin_revoke_credentials.html" + template_name = "idhub/admin/revoke_credentials.html" subtitle = _('Revoke Credentials') icon = '' class AdminWalletIdentitiesView(Credentials): - template_name = "idhub/admin_wallet_identities.html" + template_name = "idhub/admin/wallet_identities.html" subtitle = _('Organization Identities (DID)') icon = 'bi bi-patch-check-fill' wallet = True class AdminWalletCredentialsView(Credentials): - template_name = "idhub/admin_wallet_credentials.html" + template_name = "idhub/admin/wallet_credentials.html" subtitle = _('Credentials') icon = 'bi bi-patch-check-fill' wallet = True class AdminWalletConfigIssuesView(Credentials): - template_name = "idhub/admin_wallet_issues.html" + template_name = "idhub/admin/wallet_issues.html" subtitle = _('Configure Issues') icon = 'bi bi-patch-check-fill' wallet = True class AdminSchemesView(Schemes): - template_name = "idhub/admin_schemes.html" + template_name = "idhub/admin/schemes.html" subtitle = _('Schemes List') icon = '' class AdminSchemesImportView(Schemes): - template_name = "idhub/admin_schemes_import.html" + template_name = "idhub/admin/schemes_import.html" subtitle = _('Import Schemes') icon = '' class AdminSchemesExportView(Schemes): - template_name = "idhub/admin_schemes_export.html" + template_name = "idhub/admin/schemes_export.html" subtitle = _('Export Schemes') icon = '' class AdminImportView(ImportExport): - template_name = "idhub/admin_import.html" + template_name = "idhub/admin/import.html" subtitle = _('Import') icon = '' class AdminExportView(ImportExport): - template_name = "idhub/admin_export.html" + template_name = "idhub/admin/export.html" subtitle = _('Export') icon = '' diff --git a/idhub/migrations/0004_userrol.py b/idhub/migrations/0004_userrol.py new file mode 100644 index 0000000..5955a70 --- /dev/null +++ b/idhub/migrations/0004_userrol.py @@ -0,0 +1,45 @@ +# Generated by Django 4.2.5 on 2023-10-17 14:24 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("idhub", "0003_service"), + ] + + operations = [ + migrations.CreateModel( + name="UserRol", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "service", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="users", + to="idhub.service", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="roles", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 54c1953..6c143b7 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -97,3 +97,19 @@ class Service(models.Model): on_delete=models.CASCADE, related_name='services', ) + + def __str__(self): + return "{} -> {}".format(self.domain, self.rol.name) + + +class UserRol(models.Model): + user = models.ForeignKey( + User, + on_delete=models.CASCADE, + related_name='roles', + ) + service = models.ForeignKey( + Service, + on_delete=models.CASCADE, + related_name='users', + ) diff --git a/idhub/templates/idhub/admin_credentials.html b/idhub/templates/idhub/admin/credentials.html similarity index 100% rename from idhub/templates/idhub/admin_credentials.html rename to idhub/templates/idhub/admin/credentials.html diff --git a/idhub/templates/idhub/admin_dashboard.html b/idhub/templates/idhub/admin/dashboard.html similarity index 100% rename from idhub/templates/idhub/admin_dashboard.html rename to idhub/templates/idhub/admin/dashboard.html diff --git a/idhub/templates/idhub/admin_export.html b/idhub/templates/idhub/admin/export.html similarity index 100% rename from idhub/templates/idhub/admin_export.html rename to idhub/templates/idhub/admin/export.html diff --git a/idhub/templates/idhub/admin_import.html b/idhub/templates/idhub/admin/import.html similarity index 100% rename from idhub/templates/idhub/admin_import.html rename to idhub/templates/idhub/admin/import.html diff --git a/idhub/templates/idhub/admin_issue_credentials.html b/idhub/templates/idhub/admin/issue_credentials.html similarity index 100% rename from idhub/templates/idhub/admin_issue_credentials.html rename to idhub/templates/idhub/admin/issue_credentials.html diff --git a/idhub/templates/idhub/admin_people.html b/idhub/templates/idhub/admin/people.html similarity index 83% rename from idhub/templates/idhub/admin_people.html rename to idhub/templates/idhub/admin/people.html index 8f5d6fc..69cd758 100644 --- a/idhub/templates/idhub/admin_people.html +++ b/idhub/templates/idhub/admin/people.html @@ -13,8 +13,8 @@
+ | + | + |
---|---|---|
{{ rol.service.rol.name }} | +{{ rol.service.description }} | +{{ rol.service.domain }} | +