Skip to content

Add permission-based navbar items #2416

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mircearoata
Copy link
Contributor

image
image
image

@codecov-commenter
Copy link

codecov-commenter commented Mar 31, 2025

Codecov Report

Attention: Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.

Project coverage is 47.77%. Comparing base (fd7fb05) to head (0724281).
Report is 69 commits behind head on master.

Files with missing lines Patch % Lines
judge/models/interface.py 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2416      +/-   ##
==========================================
+ Coverage   46.76%   47.77%   +1.00%     
==========================================
  Files         251      260       +9     
  Lines       13317    13741     +424     
==========================================
+ Hits         6228     6565     +337     
- Misses       7089     7176      +87     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WallE256 WallE256 requested a review from Copilot March 31, 2025 23:29
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request implements permission-based control for navbar items by filtering navigation bar entries based on the user's permissions.

  • Updates the navbar context to filter items using a permission-based method.
  • Adds a new "permission" field to the NavigationBar model along with an associated migration.
  • Updates the admin interface to include the new permission field with an appropriate widget.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
judge/template_context.py Filters navbar items based on user permissions using for_user method
judge/models/interface.py Adds a permission field to the NavigationBar model and a static filtering method
judge/migrations/0150_navigationbar_permissions.py Adds the new permission field to the NavigationBar model via migration
judge/admin/interface.py Updates the admin form and field list to support the new permission field

Comment on lines +70 to +71
return list(item for item in all_navbar_items if item.permission is None or user.has_perm(item.permission))

Copy link
Preview

Copilot AI Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django's user.has_perm expects a string (in the format 'app_label.codename') rather than a Permission instance. Consider converting the Permission object to its appropriate permission name before passing it to has_perm.

Suggested change
return list(item for item in all_navbar_items if item.permission is None or user.has_perm(item.permission))
return list(item for item in all_navbar_items if item.permission is None or user.has_perm(f"{item.permission.content_type.app_label}.{item.permission.codename}"))

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this code? Is Copilot hallucinating?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot....might actually not be hallucinating....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm the one hallucinating, because I was sure I tested passing the object to has_perm, but I must have been using the admin account, which was skipping the check.

Copilot's suggestion does indeed work, but another way to fix it would be doing the filtering in the query instead of in python, concatenating the permission app label and codename, and checking if this string is in user.get_all_permissions(). This would also result in fewer queries (with copilot's code, there would be 2 queries per has_perm run, one for each field)

@@ -59,7 +59,7 @@ def general_info(request):
path = request.get_full_path()
return {
'nav_tab': FixedSimpleLazyObject(partial(__nav_tab, request.path)),
'nav_bar': NavigationBar.objects.all(),
'nav_bar': NavigationBar.for_user(request.user),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about nav_tab? This is going to break it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this would break if multiple navbar items match the current path, but the first result is the one that gets hidden, then nothing would be highlighted.

I can see a few ways to fix this:

  • passing the path to for_user and moving the regex query there, but if anything else would use the navbar in the future it would run into the same issue again
  • making for_user take a queryset and the function would only apply the permission filter, but this would then result in NavigationBar.for_user(NavigationBar.objects.all()) and NavigationBar.for_user(NavigationBar.objects.extra(where=['%s REGEXP BINARY regex'], params=[path]))
  • adding a custom queryset class for NavigationBar that applies the filter in the query (the one from Add permission-based navbar items #2416 (comment))

@staticmethod
def for_user(user):
all_navbar_items = NavigationBar.objects.all()
return list(item for item in all_navbar_items if item.permission is None or user.has_perm(item.permission))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using list(...) to emulate list comprehensions when you can use the real thing?

@@ -61,6 +64,11 @@ def pattern(self, cache={}):
pattern = cache[self.regex] = re.compile(self.regex, re.VERBOSE)
return pattern

@staticmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use classmethod? This is unpythonic, especially when you refer to the class in the body.

Comment on lines +70 to +71
return list(item for item in all_navbar_items if item.permission is None or user.has_perm(item.permission))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this code? Is Copilot hallucinating?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants