maquinaweb-shared-auth 0.2.14__py3-none-any.whl → 0.2.16__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of maquinaweb-shared-auth might be problematic. Click here for more details.
- {maquinaweb_shared_auth-0.2.14.dist-info → maquinaweb_shared_auth-0.2.16.dist-info}/METADATA +1 -1
- {maquinaweb_shared_auth-0.2.14.dist-info → maquinaweb_shared_auth-0.2.16.dist-info}/RECORD +10 -8
- shared_auth/exceptions.py +11 -9
- shared_auth/managers.py +0 -3
- shared_auth/models.py +31 -1
- shared_auth/serializers.py +11 -1
- shared_auth/urls.py +8 -0
- shared_auth/views.py +41 -0
- {maquinaweb_shared_auth-0.2.14.dist-info → maquinaweb_shared_auth-0.2.16.dist-info}/WHEEL +0 -0
- {maquinaweb_shared_auth-0.2.14.dist-info → maquinaweb_shared_auth-0.2.16.dist-info}/top_level.txt +0 -0
|
@@ -3,16 +3,18 @@ shared_auth/app.py,sha256=EHoLKlpW41o6ZxcH184aMhnWQxkVp9fH2_-89RjMz-4,215
|
|
|
3
3
|
shared_auth/authentication.py,sha256=2dcmq2ATYxvbPXgXd2duNNfiSmwIubg_5yrYa90STsI,1432
|
|
4
4
|
shared_auth/conf.py,sha256=-jdnCokMvWvVKllfsxNYCsPk1Vo3MDRq4Y1MsO16oeA,411
|
|
5
5
|
shared_auth/decorators.py,sha256=RT-Qfi7oGBo6PvWJRR1dqJUQdU6ZOf9p-8mV3rZmqQ0,3237
|
|
6
|
-
shared_auth/exceptions.py,sha256=
|
|
6
|
+
shared_auth/exceptions.py,sha256=VKamHjBl2yjXG2RsMrLrXru1_Q9IJXmy4xmDcXlpWsU,627
|
|
7
7
|
shared_auth/fields.py,sha256=RAcmFh1D_nkbai_7t_OrPZhfhAipesy5kKnEj4LUvvM,1254
|
|
8
|
-
shared_auth/managers.py,sha256=
|
|
8
|
+
shared_auth/managers.py,sha256=IF12LmQslupcQhpazmxksrWBhy3N4bZrgU8hP6bHqII,6781
|
|
9
9
|
shared_auth/middleware.py,sha256=8pi5UEj3TLLrjrVBp2Jh8F015urncUSewJTjTj5UsY8,6106
|
|
10
10
|
shared_auth/mixins.py,sha256=BSYNTWYjLRGuxfy7x-uAaNZmrTMsm67ogAQzjrU2DoQ,7388
|
|
11
|
-
shared_auth/models.py,sha256=
|
|
11
|
+
shared_auth/models.py,sha256=ow38lE2oDAVz2OplyTnGbGR7TeAmoDn3qUiyWn6Z1WE,6178
|
|
12
12
|
shared_auth/permissions.py,sha256=CTsd0xE_Z-LYNVwU4jOJLWvlcbdRG8mLkLMEnbmkhJA,2665
|
|
13
13
|
shared_auth/router.py,sha256=zYidJ7j40lQLrhkCtAQAp-rQLhua_UF0X7SDzYRcV5w,668
|
|
14
|
-
shared_auth/serializers.py,sha256=
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
maquinaweb_shared_auth-0.2.
|
|
18
|
-
maquinaweb_shared_auth-0.2.
|
|
14
|
+
shared_auth/serializers.py,sha256=uhSrcmFgBUnIWUiKymQrsSNpFDTj3j1Eged2lPG2M0s,4588
|
|
15
|
+
shared_auth/urls.py,sha256=Bj3EtpLDyNBEgtfVN0gVf11STOPJV8hzAnOb_cF2xi4,298
|
|
16
|
+
shared_auth/views.py,sha256=r4UU97TDvhJdqPr4ifIYSflRaUBeQ9DMaDAxby7l3k0,1572
|
|
17
|
+
maquinaweb_shared_auth-0.2.16.dist-info/METADATA,sha256=8D6D6yeKcHIbHZ_pDRYgCNRQoe6mBSPM2tbVZZbgkMw,27151
|
|
18
|
+
maquinaweb_shared_auth-0.2.16.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
19
|
+
maquinaweb_shared_auth-0.2.16.dist-info/top_level.txt,sha256=msyYRy02ZV7zz7GR1raUI5LXGFIFn2TIkgkeKZqKufE,12
|
|
20
|
+
maquinaweb_shared_auth-0.2.16.dist-info/RECORD,,
|
shared_auth/exceptions.py
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
+
from rest_framework.exceptions import APIException
|
|
1
2
|
"""
|
|
2
3
|
Exceções customizadas
|
|
3
4
|
"""
|
|
4
|
-
class SharedAuthError(
|
|
5
|
+
class SharedAuthError(APIException):
|
|
5
6
|
"""Erro base da biblioteca"""
|
|
6
7
|
pass
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
class OrganizationNotFoundError(SharedAuthError):
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
status_code = 404
|
|
12
|
+
default_detail = 'Organização não encontrada.'
|
|
13
|
+
default_code = 'organization_not_found'
|
|
13
14
|
|
|
14
15
|
class UserNotFoundError(SharedAuthError):
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
status_code = 404
|
|
17
|
+
default_detail = 'Usuário não encontrado.'
|
|
18
|
+
default_code = 'user_not_found'
|
|
18
19
|
|
|
19
20
|
class DatabaseConnectionError(SharedAuthError):
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
status_code = 500
|
|
22
|
+
default_detail = 'Erro interno'
|
|
23
|
+
default_code = 'internal_error'
|
shared_auth/managers.py
CHANGED
shared_auth/models.py
CHANGED
|
@@ -5,7 +5,7 @@ ATENÇÃO: Estes models NÃO devem ser usados para criar migrations
|
|
|
5
5
|
|
|
6
6
|
from django.contrib.auth.models import AbstractUser
|
|
7
7
|
from django.db import models
|
|
8
|
-
|
|
8
|
+
from .exceptions import OrganizationNotFoundError
|
|
9
9
|
from .conf import MEMBER_TABLE, ORGANIZATION_TABLE, USER_TABLE
|
|
10
10
|
from .managers import (
|
|
11
11
|
SharedMemberManager,
|
|
@@ -149,6 +149,36 @@ class User(AbstractUser):
|
|
|
149
149
|
managed = False
|
|
150
150
|
db_table = USER_TABLE
|
|
151
151
|
|
|
152
|
+
@property
|
|
153
|
+
def organizations(self):
|
|
154
|
+
"""
|
|
155
|
+
Retorna todas as organizações associadas ao usuário.
|
|
156
|
+
"""
|
|
157
|
+
return SharedOrganization.objects.filter(
|
|
158
|
+
id__in=SharedMember.objects.filter(user_id=self.id).values_list(
|
|
159
|
+
"organization_id", flat=True
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
def get_org(self, organization_id):
|
|
164
|
+
"""
|
|
165
|
+
Retorna a organização especificada pelo ID, se o usuário for membro.
|
|
166
|
+
"""
|
|
167
|
+
try:
|
|
168
|
+
organization = SharedOrganization.objects.get(id=organization_id)
|
|
169
|
+
except SharedOrganization.DoesNotExist:
|
|
170
|
+
raise OrganizationNotFoundError(
|
|
171
|
+
f"Organização com ID {organization_id} não encontrada."
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
if not SharedMember.objects.filter(
|
|
175
|
+
user_id=self.id, organization_id=organization.id
|
|
176
|
+
).exists():
|
|
177
|
+
raise OrganizationNotFoundError("Usuário não é membro desta organização.")
|
|
178
|
+
|
|
179
|
+
return organization
|
|
180
|
+
|
|
181
|
+
|
|
152
182
|
|
|
153
183
|
class SharedMember(models.Model):
|
|
154
184
|
"""
|
shared_auth/serializers.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Serializers compartilhados para DRF
|
|
3
3
|
"""
|
|
4
|
-
|
|
5
4
|
from rest_framework import serializers
|
|
5
|
+
from .models import SharedOrganization, User
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class OrganizationSerializerMixin:
|
|
@@ -49,6 +49,16 @@ class OrganizationSerializerMixin:
|
|
|
49
49
|
except Exception as e:
|
|
50
50
|
return None
|
|
51
51
|
|
|
52
|
+
class OrganizationSerializer(serializers.ModelSerializer):
|
|
53
|
+
class Meta:
|
|
54
|
+
model = SharedOrganization
|
|
55
|
+
fields = "__all__"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class UserSerializer(serializers.ModelSerializer):
|
|
59
|
+
class Meta:
|
|
60
|
+
model = User
|
|
61
|
+
fields = "__all__"
|
|
52
62
|
|
|
53
63
|
class UserSerializerMixin:
|
|
54
64
|
"""
|
shared_auth/urls.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from rest_framework.routers import DefaultRouter
|
|
2
|
+
from .views import OrganizationViewSet, UserViewSet
|
|
3
|
+
|
|
4
|
+
router = DefaultRouter()
|
|
5
|
+
router.register(r'api/organizations', OrganizationViewSet, basename='organization')
|
|
6
|
+
router.register(r'auth/user', UserViewSet, basename='user')
|
|
7
|
+
|
|
8
|
+
urlpatterns = router.urls
|
shared_auth/views.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from rest_framework import viewsets
|
|
2
|
+
from rest_framework.decorators import action
|
|
3
|
+
from rest_framework.response import Response
|
|
4
|
+
from rest_framework.permissions import IsAuthenticated
|
|
5
|
+
from .middleware import get_member
|
|
6
|
+
from .models import SharedOrganization, SharedMember
|
|
7
|
+
from .serializers import OrganizationSerializer, UserSerializer
|
|
8
|
+
|
|
9
|
+
class OrganizationViewSet(viewsets.ReadOnlyModelViewSet):
|
|
10
|
+
"""
|
|
11
|
+
ViewSet para organizações do usuário + action `me` para retornar
|
|
12
|
+
a organização atual via header.
|
|
13
|
+
"""
|
|
14
|
+
serializer_class = OrganizationSerializer
|
|
15
|
+
permission_classes = [IsAuthenticated]
|
|
16
|
+
|
|
17
|
+
def get_queryset(self):
|
|
18
|
+
organizations = self.request.user.organizations
|
|
19
|
+
return organizations
|
|
20
|
+
|
|
21
|
+
@action(detail=False, methods=['get'])
|
|
22
|
+
def me(self, request):
|
|
23
|
+
org = request.user.get_org(request.organization_id)
|
|
24
|
+
if not org:
|
|
25
|
+
return Response({"detail": "Organization not specified or not found."}, status=400)
|
|
26
|
+
|
|
27
|
+
if not get_member(request.user.id, org.pk):
|
|
28
|
+
return Response({"detail": "Você não pertence a essa organização."}, status=403)
|
|
29
|
+
serializer = self.get_serializer(org)
|
|
30
|
+
return Response(serializer.data)
|
|
31
|
+
|
|
32
|
+
class UserViewSet(viewsets.ReadOnlyModelViewSet):
|
|
33
|
+
serializer_class = UserSerializer
|
|
34
|
+
permission_classes = [IsAuthenticated]
|
|
35
|
+
|
|
36
|
+
# def get_queryset(self):
|
|
37
|
+
# return User.objects.filter(pk=self.request.user.pk)
|
|
38
|
+
|
|
39
|
+
def list(self, request, *args, **kwargs):
|
|
40
|
+
serializer = self.get_serializer(request.user)
|
|
41
|
+
return Response(serializer.data)
|
|
File without changes
|
{maquinaweb_shared_auth-0.2.14.dist-info → maquinaweb_shared_auth-0.2.16.dist-info}/top_level.txt
RENAMED
|
File without changes
|