change structure of permissions output
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import typing
|
import typing
|
||||||
from rest_framework.permissions import AllowAny
|
from rest_framework.permissions import AllowAny
|
||||||
from apps.authentication.api.v1.serializers.jwt import CustomizedTokenObtainPairSerializer
|
from apps.authentication.api.v1.serializers.jwt import CustomizedTokenObtainPairSerializer
|
||||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
|
||||||
from rest_framework.decorators import action, permission_classes
|
from rest_framework.decorators import action, permission_classes
|
||||||
from apps.authentication import permissions as auth_permissions
|
from apps.authentication import permissions as auth_permissions
|
||||||
from apps.authentication.api.v1.serializers.serializer import (
|
from apps.authentication.api.v1.serializers.serializer import (
|
||||||
@@ -14,6 +13,8 @@ from apps.authentication.api.v1.serializers.serializer import (
|
|||||||
)
|
)
|
||||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||||
from apps.authorization.api.v1 import api as authorize_view
|
from apps.authorization.api.v1 import api as authorize_view
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from apps.authentication.tools import get_token_jti
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
from apps.authentication.models import (
|
from apps.authentication.models import (
|
||||||
User,
|
User,
|
||||||
@@ -24,16 +25,14 @@ from apps.authentication.models import (
|
|||||||
BankAccountInformation,
|
BankAccountInformation,
|
||||||
BlacklistedAccessToken
|
BlacklistedAccessToken
|
||||||
)
|
)
|
||||||
from django.db import transaction
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from common.tools import CustomOperations
|
from common.tools import CustomOperations
|
||||||
|
from rest_framework.views import APIView
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from django.db import transaction
|
||||||
from common.sms import send_sms
|
from common.sms import send_sms
|
||||||
import random
|
import random
|
||||||
from rest_framework.views import APIView
|
|
||||||
from rest_framework.permissions import IsAuthenticated
|
|
||||||
from apps.authentication.tools import get_token_jti
|
|
||||||
|
|
||||||
|
|
||||||
class CustomizedTokenObtainPairView(TokenObtainPairView):
|
class CustomizedTokenObtainPairView(TokenObtainPairView):
|
||||||
@@ -41,24 +40,6 @@ class CustomizedTokenObtainPairView(TokenObtainPairView):
|
|||||||
serializer_class = CustomizedTokenObtainPairSerializer
|
serializer_class = CustomizedTokenObtainPairSerializer
|
||||||
|
|
||||||
|
|
||||||
class LogoutView(APIView):
|
|
||||||
permission_classes = [IsAuthenticated]
|
|
||||||
|
|
||||||
def post(self, request):
|
|
||||||
token_str = request.auth # access token from header
|
|
||||||
jti, user_id = get_token_jti(str(token_str))
|
|
||||||
|
|
||||||
if not jti:
|
|
||||||
return Response({'detail': 'Invalid token'}, status=status.HTTP_400_BAD_REQUEST)
|
|
||||||
|
|
||||||
BlacklistedAccessToken.objects.get_or_create(jti=jti, defaults={
|
|
||||||
'token': token_str,
|
|
||||||
'user_id': user_id,
|
|
||||||
})
|
|
||||||
|
|
||||||
return Response({'detail': 'Access token blacklisted.'}, status=status.HTTP_200_OK)
|
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(ModelViewSet):
|
class UserViewSet(ModelViewSet):
|
||||||
""" Crud operations for user model """
|
""" Crud operations for user model """
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
@@ -307,3 +288,23 @@ class GeneralOTPViewSet(ModelViewSet):
|
|||||||
if entered_code == cached_code:
|
if entered_code == cached_code:
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
return Response(status=status.HTTP_403_FORBIDDEN)
|
return Response(status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|
||||||
|
class LogoutView(APIView):
|
||||||
|
""" logout user """
|
||||||
|
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def post(self, request): # noqa
|
||||||
|
token_str = request.auth # access token from header
|
||||||
|
jti, user_id = get_token_jti(str(token_str))
|
||||||
|
|
||||||
|
if not jti:
|
||||||
|
return Response({'detail': 'Invalid token'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
BlacklistedAccessToken.objects.get_or_create(jti=jti, defaults={
|
||||||
|
'token': token_str,
|
||||||
|
'user_id': user_id,
|
||||||
|
})
|
||||||
|
|
||||||
|
return Response({'detail': 'Access token blacklisted.'}, status=status.HTTP_200_OK)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ from rest_framework import status
|
|||||||
|
|
||||||
|
|
||||||
class TokenBlackListedException(APIException):
|
class TokenBlackListedException(APIException):
|
||||||
|
""" exception for blocked access tokens """
|
||||||
|
|
||||||
status_code = status.HTTP_401_UNAUTHORIZED
|
status_code = status.HTTP_401_UNAUTHORIZED
|
||||||
default_detail = _('unauthorized')
|
default_detail = _('unauthorized')
|
||||||
default_code = 'unauthorized'
|
default_code = 'unauthorized'
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
from django.utils.deprecation import MiddlewareMixin
|
|
||||||
from .models import BlacklistedAccessToken
|
from .models import BlacklistedAccessToken
|
||||||
from apps.authentication.tools import get_token_jti
|
from apps.authentication.tools import get_token_jti
|
||||||
from rest_framework.exceptions import AuthenticationFailed
|
|
||||||
from apps.authentication.exceptions import TokenBlackListedException
|
|
||||||
from rest_framework.response import Response
|
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from rest_framework import status
|
|
||||||
|
|
||||||
|
|
||||||
class BlockedTokenMiddleware:
|
class BlockedTokenMiddleware:
|
||||||
|
""" Check blocked access token authentication """
|
||||||
|
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ from rest_framework_simplejwt.tokens import AccessToken
|
|||||||
|
|
||||||
|
|
||||||
def get_token_jti(token_str):
|
def get_token_jti(token_str):
|
||||||
|
""" get generated jwt id (jti) for every token """
|
||||||
|
|
||||||
try:
|
try:
|
||||||
token = AccessToken(token_str)
|
token = AccessToken(token_str)
|
||||||
return token['jti'], token['user_id']
|
return token['jti'], token['user_id']
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ import itertools
|
|||||||
|
|
||||||
|
|
||||||
class PageSerializer(serializers.ModelSerializer):
|
class PageSerializer(serializers.ModelSerializer):
|
||||||
|
""" Serialize every front-end page """
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Page
|
model = Page
|
||||||
fields = [
|
fields = [
|
||||||
@@ -22,6 +24,8 @@ class PageSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
|
|
||||||
class PermissionSerializer(serializers.ModelSerializer):
|
class PermissionSerializer(serializers.ModelSerializer):
|
||||||
|
""" Serialize permissions """
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Permissions
|
model = Permissions
|
||||||
fields = [
|
fields = [
|
||||||
@@ -34,17 +38,19 @@ class PermissionSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
representation['name'] = 'Hello'
|
representation['page'] = instance.page.name
|
||||||
|
|
||||||
return representation
|
return representation
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def permissions_structure_output(cls, permissions: list) -> typing.Any:
|
def permissions_structure_output(cls, permissions: list) -> typing.Any:
|
||||||
""" set a structure for permissions """
|
""" set a structure for permissions """
|
||||||
structure = {}
|
structure = []
|
||||||
for permission in permissions:
|
pages_list = []
|
||||||
if permission.page.name not in structure.keys():
|
for counter, permission in enumerate(permissions):
|
||||||
structure.update(
|
if permission.page.name not in pages_list:
|
||||||
|
pages_list.append(permission.page.name)
|
||||||
|
structure.append(
|
||||||
{f'{permission.page.name}': itertools.chain(*list(
|
{f'{permission.page.name}': itertools.chain(*list(
|
||||||
permission.page.permission_page.all().values_list('name')))
|
permission.page.permission_page.all().values_list('name')))
|
||||||
})
|
})
|
||||||
@@ -52,6 +58,8 @@ class PermissionSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
|
|
||||||
class RoleSerializer(serializers.ModelSerializer):
|
class RoleSerializer(serializers.ModelSerializer):
|
||||||
|
""" Serialize roles of user """
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Role
|
model = Role
|
||||||
fields = [
|
fields = [
|
||||||
@@ -78,6 +86,8 @@ class RoleSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
|
|
||||||
class UserRelationSerializer(serializers.ModelSerializer):
|
class UserRelationSerializer(serializers.ModelSerializer):
|
||||||
|
""" Serialize relations of user like: organizations, roles, permissions """
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UserRelations
|
model = UserRelations
|
||||||
fields = [
|
fields = [
|
||||||
@@ -108,11 +118,9 @@ class UserRelationSerializer(serializers.ModelSerializer):
|
|||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
""" update user relation object """
|
""" update user relation object """
|
||||||
if validated_data.get('role'):
|
if validated_data.get('role'):
|
||||||
instance.role = Role.objects.get(id=validated_data.get('role', instance.role))
|
instance.role = validated_data.get('role', instance.role.id)
|
||||||
if validated_data.get('organization'):
|
if validated_data.get('organization'):
|
||||||
instance.organization = Organization.objects.get(
|
instance.organization = validated_data.get('organization', instance.organization.id)
|
||||||
id=validated_data.get('organization', instance.organization)
|
|
||||||
)
|
|
||||||
instance.save()
|
instance.save()
|
||||||
instance.permissions.clear()
|
instance.permissions.clear()
|
||||||
instance.permissions.add(*(validated_data.get('permissions', instance.permissions)))
|
instance.permissions.add(*(validated_data.get('permissions', instance.permissions)))
|
||||||
|
|||||||
Reference in New Issue
Block a user