From bef827821705825cbd11f82a4a995f7a577294f3 Mon Sep 17 00:00:00 2001 From: Maximilian Oertel Date: Tue, 20 Aug 2024 22:42:49 +0200 Subject: [PATCH 1/3] Refactor user type determination --- src/composables/useUserType.js | 47 ++++++++++++++++++++++++++++++++++ src/constants/auth.js | 10 ++++++++ src/pages/HomeSelector.vue | 29 ++++++++++----------- 3 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 src/composables/useUserType.js diff --git a/src/composables/useUserType.js b/src/composables/useUserType.js new file mode 100644 index 000000000..de37aae55 --- /dev/null +++ b/src/composables/useUserType.js @@ -0,0 +1,47 @@ +import { computed } from 'vue'; +import { AUTH_USER_TYPE } from '@/constants/auth'; +import _isEmpty from 'lodash/isEmpty'; + +/** + * Get user type + * + * Composable function to determine the user type based on the user claims. The user type can be either an admin or a + * participant. The user type is determined based on the user claims, where a user is considered an admin if they have + * the corresponding super_admin or miniamlAdminOrgs claims. + * + * @param {Object} userClaims - The user claims object. + * @returns {Object} The user type and related computed properties. + */ +export default function useUserType(userClaims) { + const userType = computed(() => { + // Abort the user type determination if the user claims are not available yet. + if (!userClaims.value) return; + + const claims = userClaims.value.claims; + + // Check if the user is a super admin. + if (claims?.super_admin) { + return AUTH_USER_TYPE.ADMIN; + } + + // Check if the user has any minimal admin organizations. + const minimalAdminOrgs = claims.minimalAdminOrgs || {}; + const hasMinimalAdminOrgs = Object.values(minimalAdminOrgs).some((org) => !_isEmpty(org)); + + if (hasMinimalAdminOrgs) { + return AUTH_USER_TYPE.ADMIN; + } + + // Otherwise, default to participant user type. + return AUTH_USER_TYPE.PARTICIPANT; + }); + + const isAdmin = computed(() => userType.value === AUTH_USER_TYPE.ADMIN); + const isParticipant = computed(() => userType.value === AUTH_USER_TYPE.PARTICIPANT); + + return { + userType, + isAdmin, + isParticipant, + }; +} diff --git a/src/constants/auth.js b/src/constants/auth.js index 1c1424827..288556e69 100644 --- a/src/constants/auth.js +++ b/src/constants/auth.js @@ -10,3 +10,13 @@ export const AUTH_SESSION_TIMEOUT_IDLE_THRESHOLD = parseInt(import.meta.env.VITE_AUTH_SESSION_TIMEOUT_IDLE_THRESHOLD, 10) || 15 * oneMinuteInMs; export const AUTH_SESSION_TIMEOUT_COUNTDOWN_DURATION = parseInt(import.meta.env.VITE_AUTH_SESSION_TIMEOUT_COUNTDOWN_DURATION, 10) || 60 * oneSecondInMs; + +/** + * Auth User Type + * + * @constant {Object} AUTH_USER_TYPE - User type, admin or participant. + */ +export const AUTH_USER_TYPE = { + ADMIN: 'admin', + PARTICIPANT: 'participant', +}; diff --git a/src/pages/HomeSelector.vue b/src/pages/HomeSelector.vue index afb50e491..55401ba6a 100644 --- a/src/pages/HomeSelector.vue +++ b/src/pages/HomeSelector.vue @@ -5,12 +5,14 @@

{{ $t('homeSelector.loading') }}

+
- +
+