From e31570e93d742842a8665499b4fea12d9b913b67 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 7 Jun 2019 13:13:06 +0100 Subject: [PATCH] Refactor to use `.belongs_to` methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The view code shouldn’t need to know the internals of a service’s data structure; the idea of having a service model is to abstract this kind of thing. --- app/main/views/choose_account.py | 4 ++-- app/models/user.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/main/views/choose_account.py b/app/main/views/choose_account.py index 9bbaf06dc..69a713aba 100644 --- a/app/main/views/choose_account.py +++ b/app/main/views/choose_account.py @@ -31,11 +31,11 @@ def show_accounts_or_dashboard(): return redirect(url_for('.index')) service_id = session.get('service_id') - if service_id and (service_id in current_user.service_ids or current_user.platform_admin): + if service_id and (current_user.belongs_to_service(service_id) or current_user.platform_admin): return redirect(url_for('.service_dashboard', service_id=service_id)) organisation_id = session.get('organisation_id') - if organisation_id and (organisation_id in current_user.organisation_ids or current_user.platform_admin): + if organisation_id and (current_user.belongs_to_organisation(organisation_id) or current_user.platform_admin): return redirect(url_for('.organisation_dashboard', org_id=organisation_id)) if len(current_user.service_ids) == 1 and not current_user.organisation_ids: diff --git a/app/models/user.py b/app/models/user.py index 3bbaa7e1b..9c03ceab6 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -228,6 +228,9 @@ class User(JSONModel, UserMixin): if not self.belongs_to_service(service_id): abort(403) + def belongs_to_organisation(self, organisation_id): + return str(organisation_id) in self.organisation_ids + @property def locked(self): return self.failed_login_count >= self.max_failed_login_count