79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { ref } from "vue";
|
|
import { useAuthStore } from "./authStore";
|
|
import { useStorage } from "@vueuse/core";
|
|
|
|
export const getUserPermission = defineStore("permissionStore", () => {
|
|
const permissions = ref<any[]>([]);
|
|
const loading = ref(false);
|
|
|
|
const hasPagePermission = (to_fullPath: string) => {
|
|
let path = to_fullPath;
|
|
const index = path.indexOf("?");
|
|
if (index !== -1) path = path.substring(0, index);
|
|
|
|
const items = path.split("/");
|
|
const page_key = (items[1] || "") + "_" + (items[2] || "");
|
|
|
|
return permissions.value.some((p) => p.section_tag === page_key);
|
|
};
|
|
|
|
const hasActionPermission = (to_fullPath: string, action_tag: string) => {
|
|
let path = to_fullPath;
|
|
const index = path.indexOf("?");
|
|
if (index !== -1) path = path.substring(0, index);
|
|
|
|
const items = path.split("/");
|
|
const page_key = (items[1] || "") + "_" + (items[2] || "");
|
|
|
|
return permissions.value.some(
|
|
(p) => p.section_tag === page_key && p.action_tag === action_tag
|
|
);
|
|
};
|
|
|
|
const fetchUserPermissions = async (projectId?: number) => {
|
|
const { $http } = useNuxtApp();
|
|
const authStore = useAuthStore();
|
|
|
|
if (!authStore.isAuthenticated && import.meta.client && !localStorage.getItem("user")) {
|
|
return;
|
|
}
|
|
|
|
loading.value = true;
|
|
|
|
try {
|
|
const data = {
|
|
project_id: projectId || 50,
|
|
project_only: 1,
|
|
};
|
|
|
|
const response = await $http.postRequest("/permissions/user-permissions", data);
|
|
permissions.value = response?.data || response || [];
|
|
|
|
if (import.meta.client) {
|
|
const permitStorage = useStorage("permit", []);
|
|
permitStorage.value = permissions.value;
|
|
}
|
|
} catch (error) {
|
|
console.warn("Could not fetch user permissions:", error);
|
|
permissions.value = [];
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
|
|
const reset = () => {
|
|
permissions.value = [];
|
|
loading.value = false;
|
|
};
|
|
|
|
return {
|
|
permissions,
|
|
loading,
|
|
hasPagePermission,
|
|
hasActionPermission,
|
|
fetchUserPermissions,
|
|
reset,
|
|
};
|
|
});
|