Skip to content

Instantly share code, notes, and snippets.

@marlonmartins2
Created August 31, 2023 16:38
Show Gist options
  • Save marlonmartins2/f294caa3e68abb6f0f85e913c86545e4 to your computer and use it in GitHub Desktop.
Save marlonmartins2/f294caa3e68abb6f0f85e913c86545e4 to your computer and use it in GitHub Desktop.
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