Files
boursecu_admin/src/store/userStore.ts
T

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2026-01-24 17:45:54 +03:30
import { type ComputedRef, ref } from 'vue'
import type { User } from '@/model/user'
import { getMyInformation } from '@/api/user'
import { get } from 'lodash'
2026-06-21 20:27:13 +03:30
import { isDeployAdmin } from '@/utils/deployAdmin'
2026-07-04 14:21:27 +03:30
import { hasSmsPanelAccess, isSmsBillingEnabled } from '@/utils/smsPanelAccess'
2026-10-20 12:11:46 +03:30
import { setAdminAccessState } from '@/utils/adminAccessState'
import { parsePageAccess } from '@/utils/adminPageAccess'
2026-01-24 17:45:54 +03:30
export const useUserStore = defineStore('user', () => {
const userInfo = ref<User | undefined>(undefined)
2026-10-20 12:11:46 +03:30
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)
})
}
2026-01-24 17:45:54 +03:30
async function getMyInfo() {
try {
userInfo.value = await getMyInformation()
2026-10-20 12:11:46 +03:30
syncPageAccess()
2026-01-24 17:45:54 +03:30
return userInfo.value
} catch (e) {
console.log(e)
}
}
2026-07-30 18:30:32 +03:30
function findSettingByKey(key: string) {
return userInfo.value?.settings?.find((setting: any) => setting.key === key)?.value
}
2026-01-24 17:45:54 +03:30
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
})
2026-06-21 20:27:13 +03:30
const isDeployAdminUser = computed(() => isDeployAdmin(userInfo.value?.mobile))
2026-10-20 12:11:46 +03:30
const hasFullPageAccess = computed(() => {
if (isDeployAdminUser.value) return true
return parsePageAccess(userInfo.value?.page_access) == null
})
2026-07-04 14:21:27 +03:30
const smsPanelAccess = computed(() => hasSmsPanelAccess(userInfo.value))
const smsBillingEnabled = computed(() => isSmsBillingEnabled(userInfo.value))
return {
userInfo,
getMyInfo,
2026-07-30 18:30:32 +03:30
findSettingByKey,
2026-07-04 14:21:27 +03:30
permissions,
canAccess,
isAdmin,
isDeployAdminUser,
2026-10-20 12:11:46 +03:30
hasFullPageAccess,
2026-07-04 14:21:27 +03:30
smsPanelAccess,
smsBillingEnabled
}
2026-01-24 17:45:54 +03:30
})