diff --git a/.eslintrc.cjs b/.eslintrc.cjs index f67fea8..a704a84 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -12,6 +12,7 @@ module.exports = { rules: { 'vue/multi-word-component-names': 'off', 'no-undef': 'off', + 'vue/require-v-for-key': 'warn', }, parserOptions: { ecmaVersion: 'latest', diff --git a/src/auth.ts b/src/auth.ts index ab6f63b..98d1e0f 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -17,7 +17,7 @@ type KNOWN_ROLES = interface WhoAmIResponse { GeorchestraUser: { - roles: KNOWN_ROLES[] + roles: string[] username: string firstName: string lastName: string @@ -33,22 +33,41 @@ export interface User { anonymous: boolean warned: boolean remainingDays: string - adminRoles: AdminRoles | null -} - -export interface AdminRoles { - superUser: boolean - admin: boolean - console: boolean - catalog: boolean - catalogAdmin: boolean - viewer: boolean - import: boolean + roles: string[] } export async function getUserDetails(): Promise { + const jsonFake = { + "GeorchestraUser": { + "username": "jdoe", + "roles": [ + "ROLE_MAPSTORE_ADMIN", + "ROLE_DSP_ILEVIA", + "ROLE_GN_EDITOR", + "ROLE_USER", + "ROLE_ADMINISTRATOR", + "ROLE_SUPERUSER" + ], + "organization": "georchestra", + "id": "961b3749-d5c13b2df7", + "lastUpdated": "37ad990ece9c0b0e6d42b52fdce45849292cd9468", + "firstName": "John", + "lastName": "Doe", + "email": "john.doe@yopmail.com", + "postalAddress": null, + "telephoneNumber": null, + "title": null, + "notes": null, + "ldapWarn": false, + "ldapRemainingDays": null, + "isExternalAuth": false, + "oauth2Provider": null, + "oauth2Uid": null + } + } + return fetch(AUTH_API_URL) - .then(response => response.json()) + .then(response => jsonFake) .then((json: WhoAmIResponse) => { const user = json.GeorchestraUser if (!user) { @@ -57,42 +76,21 @@ export async function getUserDetails(): Promise { warned: false, remainingDays: '0', anonymous: true, - adminRoles: null, + roles: ['ROLE_ANONYMOUS'], } } - const roles = user.roles return { username: user.username, firstname: user.firstName, lastname: user.lastName, warned: user.ldapWarn, remainingDays: user.ldapRemainingDays, - anonymous: roles.indexOf('ROLE_ANONYMOUS') > -1, - adminRoles: getAdminRoles(roles), + anonymous: user.roles.indexOf('ROLE_ANONYMOUS') > -1, + roles: user.roles, } }) } -export function getAdminRoles(roles: KNOWN_ROLES[]): AdminRoles | null { - const superUser = roles.indexOf('ROLE_SUPERUSER') > -1 - const console = superUser || roles.indexOf('ROLE_ORGADMIN') > -1 - const catalogAdmin = superUser || roles.indexOf('ROLE_GN_ADMIN') > -1 - const catalog = !catalogAdmin && (roles.indexOf('ROLE_GN_EDITOR') > -1 || roles.indexOf('ROLE_GN_REVIEWER') > -1) - const viewer = superUser || roles.indexOf('ROLE_MAPSTORE_ADMIN') > -1 - const admin = - superUser || console || catalog || viewer || catalogAdmin - if (!admin && roles.indexOf('ROLE_IMPORT') === -1) return null - return { - superUser, - admin, - console, - catalog, - catalogAdmin, - viewer, - import: superUser || roles.indexOf('ROLE_IMPORT') > -1, - } -} - export interface PlatformInfos { analyticsEnabled: boolean extractorappEnabled: boolean diff --git a/src/header.ce.vue b/src/header.ce.vue index 95790a0..c208780 100644 --- a/src/header.ce.vue +++ b/src/header.ce.vue @@ -36,6 +36,7 @@ const isAdmin = computed(() => state.user?.adminRoles?.admin) const isWarned = computed(() => state.user?.warned) const remainingDays = computed(() => state.user?.remainingDays) const adminRoles = computed(() => state.user?.adminRoles) +const cond = computed(condition => state.user?.roles?.[condition]) const loginUrl = computed(() => { const current = new URL(window.location.href) @@ -49,7 +50,9 @@ function toggleMenu(): void { } function checkCondition(condition: string): boolean { - return !condition || (condition && state.user?.adminRoles?.[condition]) + if (!state.user) return false + if (!condition) return true + return condition.split(',').some(c => state.user?.roles?.indexOf(c) !== -1) } onMounted(() => { @@ -102,7 +105,7 @@ onMounted(() => { >