From ce3579e2dfb0fd845f4ac4309e9f71fecc8e7344 Mon Sep 17 00:00:00 2001 From: Adrian McPhee Date: Sun, 17 Nov 2024 21:52:25 +0100 Subject: [PATCH] fix: improve redirect to login logic for anonymous users accessing portal urls --- apps/portal/views.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/apps/portal/views.py b/apps/portal/views.py index 96d30019..00e6839e 100644 --- a/apps/portal/views.py +++ b/apps/portal/views.py @@ -54,12 +54,18 @@ class PortalBaseView(LoginRequiredMixin, TemplateView): """Base view for portal pages.""" - login_url = 'sign_in' + def get_login_url(self): + return reverse('security:sign_in') + portal_service = PortalService() role_service = RoleService() def get_current_organisation(self, request): """Get the current organisation from session or default to first available.""" + # Add safety check for anonymous users + if not request.user.is_authenticated: + return None + current_org_id = request.session.get('current_organisation_id') person = request.user.person @@ -79,6 +85,10 @@ def get_current_organisation(self, request): def dispatch(self, request, *args, **kwargs): """Common permission checking and org context setting.""" + # Add safety check for anonymous users + if not request.user.is_authenticated: + return redirect(self.get_login_url()) + try: # Set current_organisation as an instance variable self.current_organisation = self.get_current_organisation(request)