Files
boursecu_admin/src/store/userStore.ts
T

79 lines
2.3 KiB
TypeScript

import { type ComputedRef, ref } from 'vue'
import type { User } from '@/model/user'
import { getMyInformation } from '@/api/user'
import { get } from 'lodash'
import { isDeployAdmin } from '@/utils/deployAdmin'
import { hasSmsPanelAccess, isSmsBillingEnabled } from '@/utils/smsPanelAccess'
import { setAdminAccessState } from '@/utils/adminAccessState'
import { parsePageAccess } from '@/utils/adminPageAccess'
export const useUserStore = defineStore('user', () => {
const userInfo = ref<User | undefined>(undefined)
function syncPageAccess() {
const info = userInfo.value
const deployAdmin = isDeployAdmin(info?.mobile)
const pageAccess = parsePageAccess(info?.page_access)
const unrestricted = deployAdmin || pageAccess == null
setAdminAccessState({
ready: true,
unrestricted,
pageAccess: unrestricted ? null : pageAccess ?? null,
deployAdmin,
smsPanel: hasSmsPanelAccess(info) && isSmsBillingEnabled(info)
})
}
async function getMyInfo() {
try {
userInfo.value = await getMyInformation()
syncPageAccess()
return userInfo.value
} catch (e) {
console.log(e)
}
}
function findSettingByKey(key: string) {
return userInfo.value?.settings?.find((setting: any) => setting.key === key)?.value
}
const permissions: ComputedRef<string[]> = computed(() => {
const permissions: string[] = get(userInfo.value, 'permission', [])
const excludes: string[] = get(userInfo.value, 'exclude', [])
return permissions.filter((item) => !excludes.includes(item))
})
function canAccess(permissionKey: string) {
if (isAdmin.value) return true
return permissions.value.includes(permissionKey)
}
const isAdmin = computed(() => {
return userInfo.value?.is_admin
})
const isDeployAdminUser = computed(() => isDeployAdmin(userInfo.value?.mobile))
const hasFullPageAccess = computed(() => {
if (isDeployAdminUser.value) return true
return parsePageAccess(userInfo.value?.page_access) == null
})
const smsPanelAccess = computed(() => hasSmsPanelAccess(userInfo.value))
const smsBillingEnabled = computed(() => isSmsBillingEnabled(userInfo.value))
return {
userInfo,
getMyInfo,
findSettingByKey,
permissions,
canAccess,
isAdmin,
isDeployAdminUser,
hasFullPageAccess,
smsPanelAccess,
smsBillingEnabled
}
})