77from django .urls .exceptions import NoReverseMatch
88from django .utils .translation import gettext_lazy as _
99
10+ from netbox .api .authentication import TokenAuthentication
1011from netbox .plugins import PluginConfig
1112from netbox .registry import registry
1213from utilities .relations import get_related_models
1920 'GetRelatedModelsMixin' ,
2021 'GetReturnURLMixin' ,
2122 'ObjectPermissionRequiredMixin' ,
23+ 'TokenConditionalLoginRequiredMixin' ,
2224 'ViewTab' ,
2325 'get_viewname' ,
2426 'register_model_view' ,
@@ -39,6 +41,19 @@ def dispatch(self, request, *args, **kwargs):
3941 return super ().dispatch (request , * args , ** kwargs )
4042
4143
44+ class TokenConditionalLoginRequiredMixin (ConditionalLoginRequiredMixin ):
45+ def dispatch (self , request , * args , ** kwargs ):
46+ # Attempt to authenticate the user using a DRF token, if provided
47+ if settings .LOGIN_REQUIRED and not request .user .is_authenticated :
48+ authenticator = TokenAuthentication ()
49+ auth_info = authenticator .authenticate (request )
50+ if auth_info is not None :
51+ request .user = auth_info [0 ] # User object
52+ request .auth = auth_info [1 ]
53+
54+ return super ().dispatch (request , * args , ** kwargs )
55+
56+
4257class ContentTypePermissionRequiredMixin (ConditionalLoginRequiredMixin ):
4358 """
4459 Similar to Django's built-in PermissionRequiredMixin, but extended to check model-level permission assignments.
0 commit comments