Skip to content

Commit 9b9dbfa

Browse files
committed
fix: component to handle new auth list format
1 parent 3a2d74d commit 9b9dbfa

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/runtime/components/NUsersUserCard.vue

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { computed, onMounted, ref } from 'vue'
33
import { useAuthentication } from '../composables/useAuthentication'
44
import { useRuntimeConfig } from '#imports'
5-
import { defaultDisplayFields, defaultFieldLabels, type User } from 'nuxt-users/utils'
5+
import { defaultDisplayFields, defaultFieldLabels, type User, type Permission } from 'nuxt-users/utils'
66
import { useNuxtUsersLocale } from '../composables/useNuxtUsersLocale'
77
88
const { t } = useNuxtUsersLocale()
@@ -45,15 +45,25 @@ const canEdit = computed(() => {
4545
4646
// Check custom permissions if configured
4747
if (nuxtUsers.auth?.permissions) {
48-
const userPermissions = nuxtUsers.auth.permissions[currentUser.value.role] || []
48+
const userPermissions = (nuxtUsers.auth.permissions[currentUser.value.role as keyof typeof nuxtUsers.auth.permissions] || []) as (string | Permission)[]
4949
const apiBasePath = nuxtUsers.apiBasePath
5050
51-
return userPermissions.some((permission: string) =>
52-
permission === '*'
53-
|| permission === `${apiBasePath}/*`
54-
|| permission === `${apiBasePath}/[id].put`
55-
|| permission === `${apiBasePath}/[id].patch`
56-
)
51+
return userPermissions.some((permission: Permission) => {
52+
if (typeof permission === 'string') {
53+
return permission === '*'
54+
|| permission === `${apiBasePath}/*`
55+
|| permission === `${apiBasePath}/[id].put`
56+
|| permission === `${apiBasePath}/[id].patch`
57+
}
58+
const pathMatches = permission.path === '*'
59+
|| permission.path === `${apiBasePath}/*`
60+
|| permission.path === `${apiBasePath}/[id].put`
61+
|| permission.path === `${apiBasePath}/[id].patch`
62+
if (!pathMatches) {
63+
return false
64+
}
65+
return permission.methods.some(method => method.toUpperCase() === 'PUT' || method.toUpperCase() === 'PATCH')
66+
})
5767
}
5868
return false
5969
})
@@ -62,14 +72,23 @@ const canDelete = computed(() => {
6272
6373
// Check custom permissions if configured
6474
if (nuxtUsers.auth?.permissions) {
65-
const userPermissions = nuxtUsers.auth.permissions[currentUser.value.role] || []
75+
const userPermissions = (nuxtUsers.auth.permissions[currentUser.value.role as keyof typeof nuxtUsers.auth.permissions] || []) as (string | Permission)[]
6676
const apiBasePath = nuxtUsers.apiBasePath
6777
68-
return userPermissions.some((permission: string) =>
69-
permission === '*'
70-
|| permission === `${apiBasePath}/*`
71-
|| permission === `${apiBasePath}/[id].delete`
72-
)
78+
return userPermissions.some((permission: Permission) => {
79+
if (typeof permission === 'string') {
80+
return permission === '*'
81+
|| permission === `${apiBasePath}/*`
82+
|| permission === `${apiBasePath}/[id].delete`
83+
}
84+
const pathMatches = permission.path === '*'
85+
|| permission.path === `${apiBasePath}/*`
86+
|| permission.path === `${apiBasePath}/[id].delete`
87+
if (!pathMatches) {
88+
return false
89+
}
90+
return permission.methods.some(method => method.toUpperCase() === 'DELETE')
91+
})
7392
}
7493
return false
7594
})

0 commit comments

Comments
 (0)