feat(core): Filter every form field if associated with an organization to users organizations only
!35 fixes #119
This commit is contained in:
@ -15,9 +15,3 @@ class SoftwareAdd(CommonModelForm):
|
||||
'software',
|
||||
'action'
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
organizations = kwargs.pop('organizations')
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.fields['software'].queryset = Software.objects.filter(Q(organization_id__in=organizations) | Q(is_global = True))
|
||||
|
@ -258,20 +258,11 @@ class GroupHostAdd(AddView):
|
||||
|
||||
exsting_group_hosts = ConfigGroupHosts.objects.filter(group=group)
|
||||
|
||||
form_class.fields["host"].queryset = None
|
||||
form_class.fields["host"].queryset = form_class.fields["host"].queryset.filter(
|
||||
).exclude(
|
||||
id__in=exsting_group_hosts.values_list('host', flat=True)
|
||||
)
|
||||
|
||||
if group.is_global:
|
||||
|
||||
form_class.fields["host"].queryset = Device.objects.filter(
|
||||
).exclude(
|
||||
id__in=exsting_group_hosts.values_list('host', flat=True)
|
||||
)
|
||||
|
||||
if form_class.fields["host"].queryset is None:
|
||||
|
||||
form_class.fields["host"].queryset = Device.objects.filter(
|
||||
organization=group.organization.id,
|
||||
).exclude(id__in=exsting_group_hosts.values_list('host', flat=True))
|
||||
|
||||
return form_class
|
||||
|
||||
|
@ -51,13 +51,6 @@ class GroupSoftwareAdd(AddView):
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super().get_form_kwargs()
|
||||
obj = ConfigGroups.objects.get(pk=self.kwargs['pk'])
|
||||
kwargs['organizations'] = [ obj.organization.id ]
|
||||
return kwargs
|
||||
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return reverse('Config Management:_group_view', args=(self.kwargs['pk'],))
|
||||
|
@ -7,17 +7,38 @@ from access.models import Organization, TeamUsers
|
||||
|
||||
|
||||
class CommonModelForm(forms.ModelForm):
|
||||
""" Abstract Form class for form inclusion
|
||||
|
||||
_organizations = None
|
||||
This class exists so that common functions can be conducted against forms as they are loaded.
|
||||
"""
|
||||
|
||||
organization_field: str = 'organization'
|
||||
""" Organization Field
|
||||
|
||||
Name of the field that contains Organizations.
|
||||
|
||||
This field will be filtered to those that the user is part of.
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Form initialization.
|
||||
|
||||
Initialize the form using the super classes first then continue to initialize the form using logic
|
||||
contained within this method.
|
||||
|
||||
|
||||
## Tenancy Objects
|
||||
|
||||
Fields that contain an attribute called `organization` will have the objects filtered to
|
||||
the organizations the user is part of. If the object has `is_global=True`, that object will not be
|
||||
filtered out.
|
||||
"""
|
||||
|
||||
user = kwargs.pop('user', None)
|
||||
|
||||
user_organizations: list([str]) = []
|
||||
user_organizations_id: list(int()) = []
|
||||
|
||||
for team_user in TeamUsers.objects.filter(user=user):
|
||||
|
||||
@ -28,14 +49,7 @@ class CommonModelForm(forms.ModelForm):
|
||||
self.user_organizations = []
|
||||
|
||||
user_organizations += [ team_user.team.organization.name ]
|
||||
|
||||
if user_organizations:
|
||||
|
||||
self._organizations = Organization.objects.filter(
|
||||
Q(name__in=user_organizations)
|
||||
|
|
||||
Q(manager=user)
|
||||
)
|
||||
user_organizations_id += [ team_user.team.organization.id ]
|
||||
|
||||
new_kwargs: dict = {}
|
||||
|
||||
@ -47,8 +61,40 @@ class CommonModelForm(forms.ModelForm):
|
||||
|
||||
super().__init__(*args, **new_kwargs)
|
||||
|
||||
|
||||
if len(user_organizations_id) > 0:
|
||||
|
||||
for field_name in self.fields:
|
||||
|
||||
field = self.fields[field_name]
|
||||
|
||||
if hasattr(field, 'queryset'):
|
||||
|
||||
if hasattr(field.queryset.model, 'organization'):
|
||||
|
||||
if hasattr(field.queryset.model, 'is_global'):
|
||||
|
||||
self.fields[field_name].queryset = field.queryset.filter(
|
||||
Q(organization__in=user_organizations_id)
|
||||
|
|
||||
Q(is_global = True)
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
self.fields[field_name].queryset = field.queryset.filter(
|
||||
Q(organization__in=user_organizations_id)
|
||||
)
|
||||
|
||||
|
||||
if self.Meta.fields:
|
||||
|
||||
if self.organization_field in self.Meta.fields:
|
||||
|
||||
self.fields[self.organization_field].queryset = self._organizations
|
||||
self.fields[self.organization_field].queryset = self.fields[self.organization_field].queryset.filter(
|
||||
Q(name__in=user_organizations)
|
||||
|
|
||||
Q(manager=user)
|
||||
)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user