Skip to content

Instantly share code, notes, and snippets.

@rg3915
Forked from marlonmartins2/middleware.py
Last active July 8, 2024 03:04
Show Gist options
  • Save rg3915/fcbfb091e0fd9138b6f8bda89549f9bd to your computer and use it in GitHub Desktop.
Save rg3915/fcbfb091e0fd9138b6f8bda89549f9bd to your computer and use it in GitHub Desktop.
middleware PermissionRequiredMixin redirect
from django.utils.deprecation import MiddlewareMixin #verificar se já saiu na versão que utiliza do django ou como ficou no update.
class RequestMiddleware(MiddlewareMixin):
"""
The middleware that intercept a request.
:param MiddlewareMixin: the mixin.
:type MiddlewareMixin: MiddlewareMixin
"""
def process_request(self, request):
"""
Process the request.
:param request: the sent request.
:type request: HttpRequest
"""
if not isinstance(request.user, User):
if "Authorization" in request.headers:
token = request.headers["Authorization"].replace("Bearer ", "")
token_access_obj = AccessToken.objects.filter(token=token).first()
if token_access_obj is not None:
token_access_obj.user_id = token_access_obj.application.user.id
token_access_obj.save()
else:
if request.path and request.method == "POST": # aqui você vai fazendo a validação por cada metodo de request.
perm = # find pelas permissões
"""" apartir daqui você faz as validações com o user logado apartir do request.user """
# se ele n tiver a permissão da um return com a function do redirect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment