From 2842279165342cfcc0ac4e32802884d546e1d1f6 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Mon, 10 Jun 2024 20:25:42 +0700 Subject: [PATCH] add permission --- .env.example | 3 +- src/interface/request/main/main.ts | 141 +--------- .../components/Users/DialogRoleUser.vue | 9 +- src/modules/03_logs/router.ts | 10 +- src/modules/03_logs/views/lists.vue | 4 +- src/plugins/keycloak.ts | 16 +- src/router/index.ts | 24 +- src/stores/roleUser.ts | 65 ++--- src/views/MainLayout.vue | 262 ++---------------- 9 files changed, 101 insertions(+), 433 deletions(-) diff --git a/.env.example b/.env.example index 112b6b3d..391dea20 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ VITE_REALM_KEYCLOAK: "realms-name" VITE_CLIENTID_KEYCLOAK: "client-id" -VITE_URL_KEYCLOAK: "https://id.your-domain.com/" \ No newline at end of file +VITE_URL_KEYCLOAK: "https://id.your-domain.com/" +VITE_API_URI_CONFIG: "https://app.your-domain.com/api/v1" \ No newline at end of file diff --git a/src/interface/request/main/main.ts b/src/interface/request/main/main.ts index 3468ec9e..17552269 100644 --- a/src/interface/request/main/main.ts +++ b/src/interface/request/main/main.ts @@ -49,7 +49,7 @@ const menuList = readonly([ activeIcon: "mdi-home-variant", label: "หน้าแรก", path: "dashboard", - role: "dashboard", + role: ["SUPER_ADMIN", "ADMIN"], }, { key: 2, @@ -57,7 +57,7 @@ const menuList = readonly([ activeIcon: "person", label: "ข้อมูลหลัก", path: "metadata", - role: "metadata", + role: ["SUPER_ADMIN", "ADMIN"], children: [ { key: 2.0, @@ -91,7 +91,7 @@ const menuList = readonly([ icon: "mdi-account-badge", activeIcon: "groups", label: "จัดการผู้ใช้งานและสิทธิ์", - role: "user_role", + role: ["SUPER_ADMIN", "ADMIN"], children: [ { key: 2.0, @@ -115,141 +115,10 @@ const menuList = readonly([ icon: "history", activeIcon: "groups", label: "ประวัติกิจกรรม (Logs)", - role: "user_role", path: "viewLogs", + role: ["SUPER_ADMIN", "ADMIN"], }, ]); -const tabList = readonly([ - { - key: 1, - label: "ข้อมูลทั่วไป", - tag: "information", - }, - { - key: 19, - label: "ประวัติการเปลี่ยนชื่อ", - tag: "oldName", - }, - { - key: 16, - label: "ข้อมูลราชการ", - tag: "government", - }, - { - key: 17, - label: "ข้อมูลที่อยู่", - tag: "address", - }, - { - key: 18, - label: "ข้อมูลครอบครัว", - tag: "family", - }, - { - key: 15, - label: "ใบอนุญาตประกอบวิชาชีพ", - tag: "certicate", - }, - { - key: 2, - label: "ประวัติการศึกษา", - tag: "education", - }, - { - key: 3, - label: "การฝึกอบรม/ดูงาน", - tag: "training", - }, - { - key: 4, - label: "เครื่องราชอิสริยาภรณ์", - tag: "insignia", - }, - { - key: 5, - label: "ประกาศเกียรติคุณ", - tag: "coined", - }, - { - key: 6, - label: "ผลการประเมินปฏิบัติราชการ", - tag: "assessment", - }, - { - key: 7, - label: "ตำแหน่ง/เงินเดือน/ค่าจ้าง", - tag: "position", - }, - { - key: 8, - label: "วินัย", - tag: "rule", - }, - { - key: 9, - label: "การลา", - tag: "leave", - }, - { - key: 10, - label: "ความสามารถพิเศษ", - tag: "talent", - }, - { - key: 11, - label: "ปฎิบัติราชการพิเศษ", - tag: "work", - }, - { - key: 12, - label: "บันทึกวันที่ไม่ได้รับเงินเดือนฯ", - tag: "record", - }, - { - key: 13, - label: "อื่นๆ", - tag: "other", - }, - { - key: 14, - label: "เอกสารหลักฐาน", - tag: "document", - }, -]); - -const tabListPlacement = readonly([ - { - key: 1, - label: "ข้อมูลทั่วไป", - tag: "information", - }, - { - key: 2, - label: "ใบอนุญาตประกอบอาชีพ", - tag: "certicate", - }, - { - key: 3, - label: "ประวัติการศึกษา", - tag: "education", - }, - { - key: 4, - label: "ผลการสอบ", - tag: "examresult", - }, - { - key: 5, - label: "การคัดกรองคุณสมบัติ", - tag: "qualification", - }, - { - key: 6, - label: "เอกสารหลักฐาน", - tag: "document", - }, -]); - -export { menuList, tabList, tabListPlacement }; +export { menuList }; export type { ScrollType, tabType, menuType, notiType, optionType }; diff --git a/src/modules/02_users/components/Users/DialogRoleUser.vue b/src/modules/02_users/components/Users/DialogRoleUser.vue index 4e5e58ff..aad15417 100644 --- a/src/modules/02_users/components/Users/DialogRoleUser.vue +++ b/src/modules/02_users/components/Users/DialogRoleUser.vue @@ -68,7 +68,14 @@ function fetchlistRole() { .get(config.API.managementRole) .then((res) => { const rolesIds = roles.value?.map((e) => e.id); - rows.value = res.data.filter((v: Roles) => !rolesIds?.includes(v.id)); + rows.value = res.data.filter( + (v: Roles) => + !rolesIds?.includes(v.id) && + (v.name == "USER" || + v.name == "ADMIN" || + v.name == "SUPER_ADMIN" || + v.name == "STAFF") + ); }) .catch((err) => { messageError($q, err); diff --git a/src/modules/03_logs/router.ts b/src/modules/03_logs/router.ts index 18d9b7da..dac4fbc4 100644 --- a/src/modules/03_logs/router.ts +++ b/src/modules/03_logs/router.ts @@ -2,17 +2,13 @@ const ListsPage = () => import("@/modules/03_logs/views/lists.vue"); export default [ { - path: "/users", - name: "manageUsers", + path: "/lists", + name: "viewLogs", component: ListsPage, meta: { Auth: true, Key: [7], - Role: "user_role", + Role: ["SUPER_ADMIN", "ADMIN"], }, }, - { - path: "/users/:id", - name: "masterInsignia", - }, ]; diff --git a/src/modules/03_logs/views/lists.vue b/src/modules/03_logs/views/lists.vue index 8b137891..eb1f9309 100644 --- a/src/modules/03_logs/views/lists.vue +++ b/src/modules/03_logs/views/lists.vue @@ -1 +1,3 @@ - + diff --git a/src/plugins/keycloak.ts b/src/plugins/keycloak.ts index fe636392..83b56c84 100644 --- a/src/plugins/keycloak.ts +++ b/src/plugins/keycloak.ts @@ -11,7 +11,21 @@ const initOptions = { const keycloak = Keycloak(initOptions); -keycloak.onAuthSuccess = () => {}; //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้ +keycloak.onAuthSuccess = () => { + // check role user + const hasCommonValues = (array1: string[], array2: string[]): boolean => { + return array1.some((element) => array2.includes(element)); + }; + + const role = keycloak.tokenParsed?.role; + const roleSys = ["SUPER_ADMIN", "ADMIN"]; + const checkPermission = hasCommonValues(role, roleSys); + if (!checkPermission) { + keycloak.logout({ + redirectUri: `${window.location.protocol}//${window.location.host}/`, + }); + } +}; //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้ await keycloak.init({ onLoad: "check-sso", checkLoginIframe: false }); //ทำการ connect keycloak export default keycloak; diff --git a/src/router/index.ts b/src/router/index.ts index 927bf3eb..525db05a 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -6,6 +6,7 @@ const Error404NotFound = () => import("@/views/Error404NotFound.vue"); import ModuleMetadata from "@/modules/01_metadata/router"; import ModuleUser from "@/modules/02_users/router"; +import ModuleLogs from "@/modules/03_logs/router"; // TODO: ใช้หรือไม่? import keycloak from "@/plugins/keycloak"; @@ -25,11 +26,12 @@ const router = createRouter({ meta: { Auth: true, Key: [7], - Role: "dashboard", + Role: ["SUPER_ADMIN", "ADMIN"], }, }, ...ModuleMetadata, ...ModuleUser, + ...ModuleLogs, ], }, /** @@ -64,17 +66,17 @@ router.beforeEach((to, from, next) => { }); } else { // keycloak.updateToken(60); - const role = keycloak.tokenParsed?.role; + // const role = keycloak.tokenParsed?.role; + // console.log("tokenParsed===>", keycloak.tokenParsed?.role); + // console.log("Role===>", to.meta.Role); + // console.log(hasCommonValues(role, to.meta.Role)); - // ถ้าเป็นคนกรอกข้อมูล - if (role.includes("keyregistry") && to.meta.Role != "registry") { - next({ path: "/registry" }); - } else if (role.includes(to.meta.Role)) { - next(); - } else { - next({ path: "" }); - // next(); - } + // if (role.includes(to.meta.Role)) { + next(); + // } else { + // next({ path: "" }); + // // next(); + // } } } else { next(); diff --git a/src/stores/roleUser.ts b/src/stores/roleUser.ts index 802a7051..4840450d 100644 --- a/src/stores/roleUser.ts +++ b/src/stores/roleUser.ts @@ -2,39 +2,40 @@ import { defineStore } from "pinia"; import { ref } from "vue"; export const useroleUserDataStore = defineStore("roleusers", () => { - const insignia1Role = ref(false) - const insignia2Role = ref(false) - const caregiverRole = ref(false) - const chairmanRole = ref(false) - const commanderRole = ref(false) - const admin2Role = ref(false) - const adminRole = ref(false) - const oligarchRole = ref(false) + const insignia1Role = ref(false); + const insignia2Role = ref(false); + const caregiverRole = ref(false); + const chairmanRole = ref(false); + const commanderRole = ref(false); + const admin2Role = ref(false); + const adminRole = ref(false); + const oligarchRole = ref(false); + // const hasCommonValues = (array1: number[], array2: number[]): boolean => { + // return array1.some((element) => array2.includes(element)); + // }; - - - const fetchroleUser = async (keycloak: any) => { - if (keycloak != null) { - insignia1Role.value = await keycloak.includes("insignia1"); - insignia2Role.value = await keycloak.includes("insignia2"); - caregiverRole.value = await keycloak.includes("caregiver"); - chairmanRole.value = await keycloak.includes("chairman"); - commanderRole.value = await keycloak.includes("commander"); - admin2Role.value = await keycloak.includes("admin2"); - adminRole.value = await keycloak.includes("admin"); - oligarchRole.value = await keycloak.includes("oligarch"); - } + const fetchroleUser = async (keycloak: any) => { + if (keycloak != null) { + insignia1Role.value = await keycloak.includes("insignia1"); + insignia2Role.value = await keycloak.includes("insignia2"); + caregiverRole.value = await keycloak.includes("caregiver"); + chairmanRole.value = await keycloak.includes("chairman"); + commanderRole.value = await keycloak.includes("commander"); + admin2Role.value = await keycloak.includes("admin2"); + adminRole.value = await keycloak.includes("admin"); + oligarchRole.value = await keycloak.includes("oligarch"); } + }; - return { - fetchroleUser, - insignia1Role, - insignia2Role, - caregiverRole, - chairmanRole, - commanderRole, - admin2Role, - adminRole, - }; -}); \ No newline at end of file + return { + fetchroleUser, + insignia1Role, + insignia2Role, + caregiverRole, + chairmanRole, + commanderRole, + admin2Role, + adminRole, + }; +}); diff --git a/src/views/MainLayout.vue b/src/views/MainLayout.vue index 45bfa957..659122ab 100644 --- a/src/views/MainLayout.vue +++ b/src/views/MainLayout.vue @@ -1,7 +1,7 @@