usePermission-Mro0TvQy.mjs
1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { z as createSharedComposable, d as useRoute$1, b as useAuth, aq as getAccessiblePagesForRoles, ar as normalizePagePath, as as isProtectedPage, at as APP_PAGE_PATHS, au as toPagePath, av as canUseButtonByRoles, aw as canAccessPageByRoles } from './server.mjs';
import { computed } from 'vue';
function toRoles(user) {
if (!user || !Array.isArray(user.roles)) {
return [];
}
return user.roles.filter((role) => typeof role === "string" && role.trim().length > 0);
}
const _usePermission = () => {
const route = useRoute$1();
const { user } = useAuth();
const roles = computed(() => toRoles(user.value));
const allowedPages = computed(() => getAccessiblePagesForRoles(roles.value));
const canAccessPage = (pagePath) => {
return canAccessPageByRoles(roles.value, pagePath);
};
const can = (pagePath, buttonKey) => {
return canUseButtonByRoles(roles.value, pagePath, buttonKey);
};
const canInCurrentPage = (buttonKey) => {
return can(route.path, buttonKey);
};
const isAllowedPage = (pagePath) => {
const pageKey = toPagePath(pagePath);
if (!pageKey) {
return false;
}
return allowedPages.value.has(pageKey);
};
const getAllowedPages = () => {
return APP_PAGE_PATHS.filter((pagePath) => allowedPages.value.has(pagePath));
};
const getDeniedReason = (pagePath, buttonKey) => {
if (!isProtectedPage(pagePath)) {
return "permission.pageNotManaged";
}
if (!canAccessPage(pagePath)) {
return "permission.pageDenied";
}
if (buttonKey && !can(pagePath, buttonKey)) {
return "permission.buttonDenied";
}
return "permission.denied";
};
const normalize = (pagePath) => normalizePagePath(pagePath);
return {
roles,
allowedPages,
canAccessPage,
can,
canInCurrentPage,
isAllowedPage,
getAllowedPages,
getDeniedReason,
normalize
};
};
const usePermission = createSharedComposable(_usePermission);
export { usePermission as u };
//# sourceMappingURL=usePermission-Mro0TvQy.mjs.map