Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add role API key (BE) #3346

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion api/api_keys/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.db import models
from django_lifecycle import BEFORE_UPDATE, LifecycleModelMixin, hook
from rest_framework_api_key.models import AbstractAPIKey, APIKeyManager
from softdelete.models import SoftDeleteManager, SoftDeleteObject

Expand All @@ -9,7 +11,7 @@ class MasterAPIKeyManager(APIKeyManager, SoftDeleteManager):
pass


class MasterAPIKey(AbstractAPIKey, SoftDeleteObject):
class MasterAPIKey(AbstractAPIKey, LifecycleModelMixin, SoftDeleteObject):
organisation = models.ForeignKey(
Organisation,
on_delete=models.CASCADE,
Expand All @@ -18,3 +20,12 @@ class MasterAPIKey(AbstractAPIKey, SoftDeleteObject):

objects = MasterAPIKeyManager()
is_admin = models.BooleanField(default=True)

@hook(BEFORE_UPDATE, when="is_admin", was=False, is_now=True)
def delete_role_api_keys(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@matthewelwell matthewelwell Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to RBAC

self,
):
if settings.IS_RBAC_INSTALLED:
from rbac.models import MasterAPIKeyRole

MasterAPIKeyRole.objects.filter(master_api_key=self.id).delete()
1 change: 1 addition & 0 deletions api/api_keys/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MasterAPIKeySerializer(serializers.ModelSerializer):
class Meta:
model = MasterAPIKey
fields = (
"id",
"prefix",
"created",
"name",
Expand Down
Loading