-
-
Save rg3915/fcbfb091e0fd9138b6f8bda89549f9bd to your computer and use it in GitHub Desktop.
middleware PermissionRequiredMixin redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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