From 0b7f0640c9cc80eaa6592d4ba41034e781eb5b3a Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Mon, 6 Feb 2023 20:22:08 +0530 Subject: [PATCH 1/3] feat: segregate api token workspace --- apiserver/plane/api/views/api_token.py | 10 +++++++--- apiserver/plane/db/models/api_token.py | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apiserver/plane/api/views/api_token.py b/apiserver/plane/api/views/api_token.py index 4ed3d9de0..209fa2401 100644 --- a/apiserver/plane/api/views/api_token.py +++ b/apiserver/plane/api/views/api_token.py @@ -15,12 +15,16 @@ from plane.api.serializers import APITokenSerializer class ApiTokenEndpoint(BaseAPIView): def post(self, request): try: - label = request.data.get("label", str(uuid4().hex)) + workspace = request.data.get("workspace", False) + + if not workspace: + return Response( + {"error": "Workspace is required"}, status=status.HTTP_200_OK + ) api_token = APIToken.objects.create( - label=label, - user=request.user, + label=label, user=request.user, workspace=workspace ) serializer = APITokenSerializer(api_token) diff --git a/apiserver/plane/db/models/api_token.py b/apiserver/plane/db/models/api_token.py index 32ba013bc..1b0dda55e 100644 --- a/apiserver/plane/db/models/api_token.py +++ b/apiserver/plane/db/models/api_token.py @@ -17,7 +17,6 @@ def generate_token(): class APIToken(BaseModel): - token = models.CharField(max_length=255, unique=True, default=generate_token) label = models.CharField(max_length=255, default=generate_label_token) user = models.ForeignKey( @@ -28,6 +27,9 @@ class APIToken(BaseModel): user_type = models.PositiveSmallIntegerField( choices=((0, "Human"), (1, "Bot")), default=0 ) + workspace = models.ForeignKey( + "db.workspace", related_name="api_tokens", on_delete=models.SET_NULL, null=True + ) class Meta: verbose_name = "API Token" From f69b76c77dfcd7ffdd1e672cb93c108354c610dc Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Mon, 6 Feb 2023 22:52:20 +0530 Subject: [PATCH 2/3] fix: workpsace id during token creation --- apiserver/plane/api/views/api_token.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apiserver/plane/api/views/api_token.py b/apiserver/plane/api/views/api_token.py index 209fa2401..2508b06ac 100644 --- a/apiserver/plane/api/views/api_token.py +++ b/apiserver/plane/api/views/api_token.py @@ -24,7 +24,7 @@ class ApiTokenEndpoint(BaseAPIView): ) api_token = APIToken.objects.create( - label=label, user=request.user, workspace=workspace + label=label, user=request.user, workspace_id=workspace ) serializer = APITokenSerializer(api_token) From cd41a050228df74321a87f2f63ddaabc909a03b1 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Mon, 6 Feb 2023 23:20:21 +0530 Subject: [PATCH 3/3] refactor: update model association to cascade on delete --- apiserver/plane/db/models/api_token.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apiserver/plane/db/models/api_token.py b/apiserver/plane/db/models/api_token.py index 1b0dda55e..b4009e6eb 100644 --- a/apiserver/plane/db/models/api_token.py +++ b/apiserver/plane/db/models/api_token.py @@ -28,7 +28,7 @@ class APIToken(BaseModel): choices=((0, "Human"), (1, "Bot")), default=0 ) workspace = models.ForeignKey( - "db.workspace", related_name="api_tokens", on_delete=models.SET_NULL, null=True + "db.Workspace", related_name="api_tokens", on_delete=models.CASCADE, null=True ) class Meta: