|
1 | 1 | import logging |
| 2 | +from django.views import View |
2 | 3 | from django.views.generic import TemplateView |
3 | 4 | from django.views.generic.edit import FormView, DeleteView |
4 | 5 | from django.views.generic.edit import UpdateView, CreateView |
|
10 | 11 | from django.urls import reverse_lazy |
11 | 12 | from django.contrib import messages |
12 | 13 | from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect |
13 | | -from django.shortcuts import redirect |
| 14 | +from django.shortcuts import get_object_or_404, redirect |
| 15 | +from django.template.loader import render_to_string |
14 | 16 | from django.contrib.auth import update_session_auth_hash |
15 | 17 |
|
| 18 | +from rest_framework.authtoken.models import Token |
| 19 | + |
| 20 | +from tom_common.models import UserSession |
16 | 21 | from tom_common.forms import ChangeUserPasswordForm, CustomUserCreationForm, GroupForm |
17 | 22 | from tom_common.mixins import SuperuserRequiredMixin |
18 | 23 |
|
@@ -82,6 +87,54 @@ def dispatch(self, *args, **kwargs): |
82 | 87 | return super().dispatch(*args, **kwargs) |
83 | 88 |
|
84 | 89 |
|
| 90 | +class RegenerateAPITokenView(LoginRequiredMixin, View): |
| 91 | + """View that handles regeneration of a User's DRF API token. Requires login. |
| 92 | +
|
| 93 | + Deletes the existing token (if any) and creates a new one. For HTMX requests, |
| 94 | + returns the api_token partial with the new token. For non-HTMX requests, |
| 95 | + redirects to the user update page with a success message. |
| 96 | + """ |
| 97 | + # this is the partial template to render the API token |
| 98 | + partial_template_name = 'tom_common/partials/api_token.html' |
| 99 | + |
| 100 | + def dispatch(self, *args, **kwargs): |
| 101 | + """Ensure non-superusers can only regenerate their own token. |
| 102 | +
|
| 103 | + Checks authentication first (via LoginRequiredMixin), then checks |
| 104 | + that non-superusers are only operating on their own token. |
| 105 | + """ |
| 106 | + # the User must be authenticated |
| 107 | + if not self.request.user.is_authenticated: |
| 108 | + return self.handle_no_permission() |
| 109 | + |
| 110 | + # don't let a non-super-user regenerate someone else's API Token, |
| 111 | + # instead, redirect them to their own user-update view. |
| 112 | + if not self.request.user.is_superuser and self.request.user.id != int(self.kwargs['pk']): |
| 113 | + return redirect('user-update', pk=self.request.user.id) |
| 114 | + return super().dispatch(*args, **kwargs) |
| 115 | + |
| 116 | + def post(self, request, pk: int) -> HttpResponse: |
| 117 | + target_user = get_object_or_404(User, pk=pk) |
| 118 | + |
| 119 | + # Delete existing token (safe even if none exists) and create a new one |
| 120 | + Token.objects.filter(user=target_user).delete() |
| 121 | + new_token = Token.objects.create(user=target_user) |
| 122 | + |
| 123 | + # handle HTMX requests here |
| 124 | + if request.htmx: |
| 125 | + # Return just the partial for in-place replacement (avoid full page reload) |
| 126 | + html = render_to_string( |
| 127 | + self.partial_template_name, |
| 128 | + {'drf_api_token': new_token, 'user_pk': target_user.pk}, |
| 129 | + request=request, |
| 130 | + ) |
| 131 | + return HttpResponse(html) |
| 132 | + |
| 133 | + # Non-HTMX fallback: redirect with a success message |
| 134 | + messages.success(request, 'API token regenerated.') |
| 135 | + return redirect('user-update', pk=target_user.pk) |
| 136 | + |
| 137 | + |
85 | 138 | class UserProfileView(LoginRequiredMixin, TemplateView): |
86 | 139 | """ |
87 | 140 | View to handle creating a user profile page. Requires a login. |
@@ -193,9 +246,19 @@ def get_success_url(self): |
193 | 246 | return reverse_lazy('user-update', kwargs={'pk': self.request.user.id}) |
194 | 247 |
|
195 | 248 | def get_context_data(self, **kwargs): |
196 | | - """Add current user to the context for all templates.""" |
| 249 | + """Add current user and API token to the context for all templates.""" |
197 | 250 | context = super().get_context_data(**kwargs) |
| 251 | + |
| 252 | + # this is the User doing the updating. (could be super-user) |
198 | 253 | context['current_user'] = self.request.user |
| 254 | + |
| 255 | + # this is the User being updated (usually the same as the requesting User, |
| 256 | + # but not if a super-user is updating a different User). |
| 257 | + user_being_updated = self.object |
| 258 | + |
| 259 | + # add context required to Regenerate the user's DRF API token |
| 260 | + context['drf_api_token'] = getattr(user_being_updated, 'auth_token', None) |
| 261 | + context['user_pk'] = user_being_updated.pk |
199 | 262 | return context |
200 | 263 |
|
201 | 264 | def get_form(self, form_class=None): |
|
0 commit comments