Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix permissions of views based on rbac, fixes #863 #864

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/863.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updated the queryset altering to be after permissions restriction for config compliance list view.
Updated the queryset before rendering the compliance reporting to be after permissions restriction.
2 changes: 1 addition & 1 deletion nautobot_golden_config/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def test_alter_queryset(self):
reverse("plugins:nautobot_golden_config:configcompliance_list")
)
request.user = self.user
queryset = views.ConfigComplianceUIViewSet(request=request).alter_queryset(request)
queryset = views.ConfigComplianceUIViewSet(request=request, action="list").alter_queryset(request)
features = (
models.ComplianceFeature.objects.filter(feature__rule__isnull=False)
.values_list("slug", flat=True)
Expand Down
7 changes: 5 additions & 2 deletions nautobot_golden_config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ def get_extra_context(self, request, instance=None, **kwargs):

def alter_queryset(self, request):
"""Build actual runtime queryset as the build time queryset of table `pivoted`."""
# Super because alter_queryset() calls get_queryset(), which is what calls queryset.restrict()
self.queryset = super().alter_queryset(request)
return pivot(
self.queryset,
["device", "device__name"],
Expand Down Expand Up @@ -375,9 +377,10 @@ def setup(self, request, *args, **kwargs):
"""Using request object to perform filtering based on query params."""
super().setup(request, *args, **kwargs)
filter_params = self.get_filter_params(request)
main_qs = models.ConfigCompliance.objects
# Add .restrict() to the queryset to restrict the view based on user permissions.
main_qs = models.ConfigCompliance.objects.restrict(request.user, "view")
device_aggr, feature_aggr = get_global_aggr(main_qs, self.filterset, filter_params)
feature_qs = self.filterset(request.GET, self.queryset).qs
feature_qs = self.filterset(request.GET, self.queryset.restrict(request.user, "view")).qs
self.extra_content = {
"bar_chart": plot_barchart_visual(feature_qs),
"device_aggr": device_aggr,
Expand Down