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

Delete OutstandingToken via django-admin #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion rest_framework_simplejwt/token_blacklist/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from rest_framework_simplejwt.exceptions import TokenError
from rest_framework_simplejwt.tokens import RefreshToken
from .models import BlacklistedToken, OutstandingToken


Expand Down Expand Up @@ -33,7 +35,9 @@ def get_readonly_fields(self, *args, **kwargs):
def has_add_permission(self, *args, **kwargs):
return False

def has_delete_permission(self, *args, **kwargs):
def has_delete_permission(self, request, *args, **kwargs):
if request.user.is_superuser:
Copy link
Member

Choose a reason for hiding this comment

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

And is_staff?

return True
return False

def has_change_permission(self, request, obj=None):
Expand All @@ -42,6 +46,15 @@ def has_change_permission(self, request, obj=None):
super().has_change_permission(request, obj)
)

def delete_queryset(self, request, queryset):
for outstanding_token in queryset:
try:
token = RefreshToken(outstanding_token.token)
token.blacklist()
except TokenError:
pass
queryset.delete()


admin.site.register(OutstandingToken, OutstandingTokenAdmin)

Expand Down
2 changes: 1 addition & 1 deletion rest_framework_simplejwt/token_blacklist/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class OutstandingToken(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True)
Copy link
Member

Choose a reason for hiding this comment

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

If this happens then you should change the SET_NULL in the migrations (deliberately without creating a new file).

Copy link
Member

Choose a reason for hiding this comment

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

Also, I'm not entirely sure if you should change it to be SET_NULL...


jti = models.CharField(unique=True, max_length=255)
token = models.TextField()
Expand Down