mirror of https://github.com/halo-dev/halo-admin
[release-2.2] fix: the issue that role display name (#856)
This is an automated cherry-pick of #847 /assign ruibaby ```release-note 优化 Console 端用户角色标识的显示名称。 ```pull/858/head
parent
11e08762a0
commit
cb055d3fb8
@ -1,18 +1,34 @@
|
||||
import { defineStore } from "pinia";
|
||||
import type { Role, UserPermission } from "@halo-dev/api-client";
|
||||
import { ref } from "vue";
|
||||
import { apiClient } from "@/utils/api-client";
|
||||
import { roleLabels } from "@/constants/labels";
|
||||
import { rbacAnnotations } from "@/constants/annotations";
|
||||
|
||||
interface RoleStoreState {
|
||||
roles: Role[]; // all roles
|
||||
permissions: UserPermission; // current user's permissions
|
||||
}
|
||||
|
||||
export const useRoleStore = defineStore({
|
||||
id: "role",
|
||||
state: (): RoleStoreState => ({
|
||||
export const useRoleStore = defineStore("role", () => {
|
||||
const roles = ref<Role[]>([]);
|
||||
const permissions = ref<UserPermission>({
|
||||
roles: [],
|
||||
permissions: {
|
||||
roles: [],
|
||||
uiPermissions: [],
|
||||
},
|
||||
}),
|
||||
uiPermissions: [],
|
||||
});
|
||||
|
||||
async function fetchRoles() {
|
||||
try {
|
||||
const { data } = await apiClient.extension.role.listv1alpha1Role({
|
||||
page: 0,
|
||||
size: 0,
|
||||
labelSelector: [`!${roleLabels.TEMPLATE}`],
|
||||
});
|
||||
roles.value = data.items;
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch roles", error);
|
||||
}
|
||||
}
|
||||
|
||||
function getRoleDisplayName(name: string) {
|
||||
const role = roles.value.find((role) => role.metadata.name === name);
|
||||
return role?.metadata.annotations?.[rbacAnnotations.DISPLAY_NAME] || name;
|
||||
}
|
||||
|
||||
return { roles, permissions, fetchRoles, getRoleDisplayName };
|
||||
});
|
||||
|
Loading…
Reference in new issue