Skip to content

Commit

Permalink
feat: first step of condition
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed Dec 6, 2024
1 parent e5196b7 commit 4ebe0c0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
72 changes: 35 additions & 37 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type KNOWN_ROLES =

interface WhoAmIResponse {
GeorchestraUser: {
roles: KNOWN_ROLES[]
roles: string[]
username: string
firstName: string
lastName: string
Expand All @@ -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<User> {
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": "[email protected]",
"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) {
Expand All @@ -57,42 +76,21 @@ export async function getUserDetails(): Promise<User> {
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
Expand Down
30 changes: 23 additions & 7 deletions src/header.ce.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(() => {
Expand Down Expand Up @@ -102,7 +105,7 @@ onMounted(() => {
></GeorchestraLogo>
</a>
<nav class="flex justify-center items-center font-semibold">
<template v-for="item in menu.menu" v-if="checkCondition">
<template v-for="(item, index) in menu.menu" :key="index">
<template v-if="!item.type && checkCondition(item.condition)">
<a
:href="item.url"
Expand All @@ -111,12 +114,22 @@ onMounted(() => {
>{{ item.i18n ? t(item.i18n) : item.label }}</a
>
</template>
<template v-else-if="item.type === 'separator' && checkCondition(item.condition)">
<template
v-else-if="
item.type === 'separator' && checkCondition(item.condition)
"
>
<span class="text-gray-400">|</span>
</template>
<template v-else-if="item.type === 'dropdown' && checkCondition(item.condition)">
<template
v-else-if="
item.type === 'dropdown' && checkCondition(item.condition)
"
>
<div class="group inline-block relative">
<button class="nav-item after:hover:scale-x-0 flex items-center">
<button
class="nav-item after:hover:scale-x-0 flex items-center"
>
<span class="mr-2 first-letter:capitalize">{{
item.i18n ? t(item.i18n) : item.label
}}</span>
Expand All @@ -129,8 +142,11 @@ onMounted(() => {
class="absolute hidden group-hover:block border rounded w-full admin-dropdown z-[1002] bg-white"
>
<template v-for="subitem in item.items">
<li v-if="checkCondition(subitem.condition)"
:class="{ active: props.activeApp === subitem['active-app'] }"
<li
v-if="checkCondition(subitem.condition)"
:class="{
active: props.activeApp === subitem['active-app'],
}"
>
<a :href="subitem.url" class="capitalize">
{{ subitem.i18n ? t(subitem.i18n) : subitem.label }}</a
Expand Down
11 changes: 6 additions & 5 deletions src/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,38 @@
"i18n": "datafeeder",
"url": "/datafeeder",
"active-app" : "datafeeder",
"condition": "import"
"condition": "ROLE_SUPERUSER,ROLE_IMPORT"
},
{
"condition": "isAdmin",
"condition": "ROLE_SUPERUSER",
"type": "separator"
},
{
"type": "dropdown",
"label": "Administration",
"i18n": "admin",
"condition": "ROLE_SUPERUSER",
"items": [
{
"label": "Geonetwork",
"i18n": "catalogue",
"url": "/georchestra/ldap/users",
"active-app": "geonetwork",
"condition": "catalogAdmin"
"condition": "ROLE_SUPERUSER,ROLE_GN_EDITOR,ROLE_GN_ADMIN"
},
{
"label": "Viewer",
"i18n": "viewer",
"url": "/mapstore/#/admin",
"active-app": "msadmin",
"condition": "viewer"
"condition": "ROLE_SUPERUSER,ROLE_MAPSTORE_ADMIN"
},
{
"label": "Console",
"i18n": "users",
"url": "/console/manager/home",
"active-app": "console",
"condition": "console"
"condition": "ROLE_SUPERUSER"
}
]
}
Expand Down

0 comments on commit 4ebe0c0

Please sign in to comment.