fix restrictions
This commit is contained in:
parent
b178f3460a
commit
4cb5c638a3
|
@ -11,6 +11,8 @@ from idhub.models import (
|
|||
File_datas,
|
||||
Membership,
|
||||
Schemas,
|
||||
Service,
|
||||
UserRol,
|
||||
VerificableCredential,
|
||||
)
|
||||
from idhub_auth.models import User
|
||||
|
@ -139,19 +141,53 @@ class MembershipForm(forms.ModelForm):
|
|||
data = super().clean()
|
||||
start_date = data['start_date']
|
||||
end_date = data.get('end_date')
|
||||
members = Membership.objects.filter(
|
||||
type=data['type'],
|
||||
user=self.instance.user
|
||||
)
|
||||
if self.instance.id:
|
||||
members = members.exclude(id=self.instance.id)
|
||||
|
||||
if (start_date and end_date):
|
||||
if start_date > end_date:
|
||||
msg = _("The end date is less than the start date")
|
||||
raise forms.ValidationError(msg)
|
||||
|
||||
members = Membership.objects.filter(
|
||||
type=data['type'],
|
||||
members = members.filter(
|
||||
start_date__lte=end_date,
|
||||
end_date__gte=start_date,
|
||||
user=self.instance.user
|
||||
)
|
||||
if members.exists() and not self.instance.id:
|
||||
|
||||
if members.exists():
|
||||
msg = _("This membership already exists!")
|
||||
raise forms.ValidationError(msg)
|
||||
|
||||
return end_date
|
||||
|
||||
|
||||
class UserRolForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = UserRol
|
||||
fields = ['service']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if not self.instance.id:
|
||||
user = self.instance.user
|
||||
choices = self.fields['service'].choices
|
||||
choices.queryset = choices.queryset.exclude(users__user=user)
|
||||
self.fields['service'].choices = choices
|
||||
|
||||
def clean_service(self):
|
||||
data = super().clean()
|
||||
service = UserRol.objects.filter(
|
||||
service=data['service'],
|
||||
user=self.instance.user
|
||||
)
|
||||
|
||||
if service.exists():
|
||||
msg = _("Is not possible to have a duplicate role")
|
||||
raise forms.ValidationError(msg)
|
||||
|
||||
return data['service']
|
||||
|
|
|
@ -25,7 +25,12 @@ from idhub_auth.models import User
|
|||
from idhub_auth.forms import ProfileForm
|
||||
from idhub.mixins import AdminView
|
||||
from idhub.email.views import NotifyActivateUserByEmail
|
||||
from idhub.admin.forms import ImportForm, SchemaForm, MembershipForm
|
||||
from idhub.admin.forms import (
|
||||
ImportForm,
|
||||
MembershipForm,
|
||||
SchemaForm,
|
||||
UserRolForm,
|
||||
)
|
||||
from idhub.models import (
|
||||
DID,
|
||||
Event,
|
||||
|
@ -311,12 +316,12 @@ class PeopleMembershipDeleteView(PeopleView):
|
|||
return redirect('idhub:admin_people_edit', user.id)
|
||||
|
||||
|
||||
class PeopleRolRegisterView(People, CreateView):
|
||||
class PeopleRolRegisterView(People, FormView):
|
||||
template_name = "idhub/admin/people_rol_register.html"
|
||||
subtitle = _('Add a user role to access a service')
|
||||
icon = 'bi bi-person'
|
||||
form_class = UserRolForm
|
||||
model = UserRol
|
||||
fields = ('service',)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.pk = kwargs['pk']
|
||||
|
@ -331,14 +336,13 @@ class PeopleRolRegisterView(People, CreateView):
|
|||
def get_form_kwargs(self):
|
||||
self.object = self.model(user=self.user)
|
||||
kwargs = super().get_form_kwargs()
|
||||
kwargs['instance'] = self.object
|
||||
return kwargs
|
||||
|
||||
def get_form(self):
|
||||
form = super().get_form()
|
||||
choices = form.fields['service'].choices
|
||||
choices.queryset = choices.queryset.exclude(users__user=self.user)
|
||||
form.fields['service'].choices = choices
|
||||
return form
|
||||
def form_valid(self, form):
|
||||
form.save()
|
||||
messages.success(self.request, _('Membership created successfully'))
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
self.success_url = reverse_lazy(
|
||||
|
@ -348,20 +352,33 @@ class PeopleRolRegisterView(People, CreateView):
|
|||
return self.success_url
|
||||
|
||||
|
||||
class PeopleRolEditView(People, CreateView):
|
||||
class PeopleRolEditView(People, FormView):
|
||||
template_name = "idhub/admin/people_rol_register.html"
|
||||
subtitle = _('Modify a user role to access a service')
|
||||
icon = 'bi bi-person'
|
||||
form_class = UserRolForm
|
||||
model = UserRol
|
||||
fields = ('service',)
|
||||
|
||||
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)
|
||||
|
||||
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()
|
||||
kwargs['instance'] = self.object
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form):
|
||||
form.save()
|
||||
messages.success(self.request, _('Membership updated successfully'))
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
self.success_url = reverse_lazy(
|
||||
'idhub:admin_people_edit',
|
||||
|
@ -400,7 +417,7 @@ class RolRegisterView(AccessControl, CreateView):
|
|||
subtitle = _('Add Role')
|
||||
icon = ''
|
||||
model = Rol
|
||||
fields = ('name',)
|
||||
fields = ('name', "description")
|
||||
success_url = reverse_lazy('idhub:admin_roles')
|
||||
object = None
|
||||
|
||||
|
@ -411,12 +428,12 @@ class RolRegisterView(AccessControl, CreateView):
|
|||
return super().form_valid(form)
|
||||
|
||||
|
||||
class RolEditView(AccessControl, CreateView):
|
||||
class RolEditView(AccessControl, UpdateView):
|
||||
template_name = "idhub/admin/rol_register.html"
|
||||
subtitle = _('Edit Role')
|
||||
icon = ''
|
||||
model = Rol
|
||||
fields = ('name',)
|
||||
fields = ('name', "description")
|
||||
success_url = reverse_lazy('idhub:admin_roles')
|
||||
|
||||
def get_form_kwargs(self):
|
||||
|
@ -458,7 +475,7 @@ class ServicesView(AccessControl):
|
|||
})
|
||||
return context
|
||||
|
||||
class ServiceRegisterView(AccessControl, CreateView):
|
||||
class ServiceRegisterView(AccessControl, UpdateView):
|
||||
template_name = "idhub/admin/service_register.html"
|
||||
subtitle = _('Add service')
|
||||
icon = ''
|
||||
|
@ -474,7 +491,7 @@ class ServiceRegisterView(AccessControl, CreateView):
|
|||
return super().form_valid(form)
|
||||
|
||||
|
||||
class ServiceEditView(AccessControl, CreateView):
|
||||
class ServiceEditView(AccessControl, UpdateView):
|
||||
template_name = "idhub/admin/service_register.html"
|
||||
subtitle = _('Modify service')
|
||||
icon = ''
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
# Generated by Django 4.2.5 on 2023-11-14 09:19
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('idhub', '0002_event'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='rol',
|
||||
name='description',
|
||||
field=models.CharField(
|
||||
max_length=250, null=True, verbose_name='Description'
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='membership',
|
||||
name='end_date',
|
||||
field=models.DateField(
|
||||
blank=True,
|
||||
help_text='What date will the membership end?',
|
||||
null=True,
|
||||
verbose_name='End date',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='membership',
|
||||
name='type',
|
||||
field=models.PositiveSmallIntegerField(
|
||||
choices=[(1, 'Beneficiary'), (2, 'Employee'), (3, 'Member')],
|
||||
verbose_name='Type of membership',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='rol',
|
||||
name='name',
|
||||
field=models.CharField(max_length=250, verbose_name='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='service',
|
||||
name='description',
|
||||
field=models.CharField(max_length=250, verbose_name='Description'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='service',
|
||||
name='domain',
|
||||
field=models.CharField(max_length=250, verbose_name='Domain'),
|
||||
),
|
||||
]
|
18
idhub/migrations/0004_alter_userrol_unique_together.py
Normal file
18
idhub/migrations/0004_alter_userrol_unique_together.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.5 on 2023-11-14 09:49
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('idhub', '0003_rol_description_alter_membership_end_date_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name='userrol',
|
||||
unique_together={('user', 'service')},
|
||||
),
|
||||
]
|
|
@ -525,7 +525,8 @@ class Membership(models.Model):
|
|||
|
||||
|
||||
class Rol(models.Model):
|
||||
name = models.CharField(max_length=250)
|
||||
name = models.CharField(_("name"), max_length=250)
|
||||
description = models.CharField(_("Description"), max_length=250, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
@ -557,6 +558,9 @@ class UserRol(models.Model):
|
|||
related_name='users',
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = ('user', 'service',)
|
||||
|
||||
|
||||
class Organization(models.Model):
|
||||
name = models.CharField(max_length=250)
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th>
|
||||
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Description' %}</button></th>
|
||||
<th scope="col"></th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
|
@ -21,6 +22,7 @@
|
|||
{% for rol in roles.all %}
|
||||
<tr>
|
||||
<td>{{ rol.name }}</td>
|
||||
<td>{{ rol.description|default:""}}</td>
|
||||
<td><a href="{% url 'idhub:admin_rol_edit' rol.id %}" title="{% trans 'Edit' %}"><i class="bi bi-pencil-square"></i></a></td>
|
||||
<td><a href="{% url 'idhub:admin_rol_del' rol.id %}" title="{% trans 'Delete' %}"><i class="bi bi-trash"></i></a></td>
|
||||
</tr>
|
||||
|
|
Loading…
Reference in a new issue