[docs]classSecurityLevelMixin:"""A mixin that provides security feature checks for Django views."""def__init__(self,security_feature:SecurityFeature=None,*args,**kwargs)->None:"""Initializes the SecurityLevelMixin with the specified security feature and redirect URL. Parameters: ----------- security_feature : SecurityFeatures, optional The feature to check against the current security level (default is None). no_permisson_url : str, optional The URL to which the user is redirected if the feature is not allowed (default is None). *args, **kwargs: Additional arguments passed to the superclass initializer. """super().__init__(*args,**kwargs)
[docs]defget_security_level(self):"""Returns the security mode of the current security level instance. Returns: -------- str The security mode of the current security level instance. """returnself.sec.get_security_level()
[docs]classSecurityLevelMixinRedirect(SecurityLevelMixin):"""A mixin that provides security feature checks for Django views with redirect feature."""def__init__(self,disabled_by_security_level_url=None,*args,**kwargs)->None:"""Initializes the SecurityLevelMixin with the specified security feature and redirect URL. Parameters: ----------- security_feature : SecurityFeatures, optional The feature to check against the current security level (default is None). no_permisson_url : str, optional The URL to which the user is redirected if the feature is not allowed (default is None). *args, **kwargs: Additional arguments passed to the superclass initializer. """super().__init__(*args,**kwargs)
[docs]defdispatch(self,request,*args,**kwargs):"""If the feature is not allowed, the user is redirected to the disabled_by_security_level_url with an error message. Parameters: ----------- request : HttpRequest The HTTP request object. *args, **kwargs: Additional arguments passed to the dispatch method. Returns: -------- HttpResponse or HttpResponseRedirect The HTTP response object, either continuing to the requested view or redirecting. """ifnotself.sec.is_feature_allowed(self.security_feature):msg=_('Your security setting %s does not allow the feature: %s'%(self.get_security_level(),self.security_feature.value))messages.error(request,msg)returnredirect(self.disabled_by_security_level_url)returnsuper().dispatch(request,*args,**kwargs)