Skip to content

Commit

Permalink
Fixes: netbox-community#16973 - Resolve $user token to User.id for us…
Browse files Browse the repository at this point in the history
…e in permissions based on custom fields (netbox-community#17268)

* Resolve $user token to User.id for use in permissions based on custom fields

* Cleaner type check

* Simplify User object check by updating tokens instead of resolved values
  • Loading branch information
bctiemann authored Aug 26, 2024
1 parent ee0af15 commit a7f83de
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions netbox/utilities/permissions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.conf import settings
from django.apps import apps
from django.db.models import Q
from django.utils.translation import gettext_lazy as _

from users.constants import CONSTRAINT_TOKEN_USER

__all__ = (
'get_permission_for_model',
'permission_is_exempt',
Expand Down Expand Up @@ -90,6 +93,11 @@ def qs_filter_from_constraints(constraints, tokens=None):
if tokens is None:
tokens = {}

User = apps.get_model('users.User')
for token, value in tokens.items():
if token == CONSTRAINT_TOKEN_USER and isinstance(value, User):
tokens[token] = value.id

def _replace_tokens(value, tokens):
if type(value) is list:
return list(map(lambda v: tokens.get(v, v), value))
Expand Down

0 comments on commit a7f83de

Please sign in to comment.