maquinaweb-shared-auth 0.2.14__py3-none-any.whl → 0.2.15__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maquinaweb-shared-auth
3
- Version: 0.2.14
3
+ Version: 0.2.15
4
4
  Summary: Models read-only para autenticação compartilhada entre projetos Django.
5
5
  Author-email: Seu Nome <seuemail@dominio.com>
6
6
  License: MIT
@@ -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=eiII-REupK6GeFinisteYO3FsGUDAN5zAajXPhTREm8,404
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=Oskz863DGG7nFp6ijZfYVWGCG-Inp6PAP0NakX-x71g,6812
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=a3NZKcGHCP_dHBDPeabxrpryDjk9silKVh2TwOALwrs,5135
11
+ shared_auth/models.py,sha256=PHVLpJUk3gzmiQYHPkk_G1KyUo8qXGolCioK8vg2xu0,6201
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=TDpuZVsOL-6igINSOOOyELWbTUeet4XWRoBkvcMGjW4,4290
15
- maquinaweb_shared_auth-0.2.14.dist-info/METADATA,sha256=F6kSUVyGeqYrpK66wyh4ijlXxYtu2V4T5uCFJHHyTyE,27151
16
- maquinaweb_shared_auth-0.2.14.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
17
- maquinaweb_shared_auth-0.2.14.dist-info/top_level.txt,sha256=msyYRy02ZV7zz7GR1raUI5LXGFIFn2TIkgkeKZqKufE,12
18
- maquinaweb_shared_auth-0.2.14.dist-info/RECORD,,
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.15.dist-info/METADATA,sha256=00iVBSYhh26aw4TA_z8yMRgwKmglsq5JIXM6o1z5pio,27151
18
+ maquinaweb_shared_auth-0.2.15.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
+ maquinaweb_shared_auth-0.2.15.dist-info/top_level.txt,sha256=msyYRy02ZV7zz7GR1raUI5LXGFIFn2TIkgkeKZqKufE,12
20
+ maquinaweb_shared_auth-0.2.15.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(Exception):
5
+ class SharedAuthError(APIException):
5
6
  """Erro base da biblioteca"""
6
7
  pass
7
8
 
8
9
 
9
10
  class OrganizationNotFoundError(SharedAuthError):
10
- """Organização não encontrada"""
11
- pass
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
- """Usuário não encontrado"""
16
- pass
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
- """Erro de conexão com o banco de dados"""
21
- pass
21
+ status_code = 500
22
+ default_detail = 'Erro interno'
23
+ default_code = 'internal_error'
shared_auth/managers.py CHANGED
@@ -220,6 +220,3 @@ class BaseAuthManager(models.Manager):
220
220
  return super().get_queryset()
221
221
 
222
222
  return qs_class(self.model, using=self._db)
223
-
224
-
225
- # shared_auth/serializers.py
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, PermissionDeniedError
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
  """
@@ -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)