Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

[release-2.2] fix: the issue that role display name #856

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/layouts/BasicLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import sortBy from "lodash.sortby";
import { useRoleStore } from "@/stores/role";
import { hasPermission } from "@/utils/permission";
import { useUserStore } from "@/stores/user";
import { rbacAnnotations } from "@/constants/annotations";

const route = useRoute();
const router = useRouter();
Expand Down Expand Up @@ -56,11 +57,16 @@ const handleLogout = () => {
};

const currentRole = computed(() => {
return JSON.parse(
userStore.currentUser?.metadata.annotations?.[
"rbac.authorization.halo.run/role-names"
] || "[]"
)[0];
const names = JSON.parse(
userStore.currentUser?.metadata.annotations?.[rbacAnnotations.ROLE_NAMES] ||
"[]"
);

if (names.length === 0) {
return;
}

return roleStore.getRoleDisplayName(names[0]);
});

// Global Search
Expand Down Expand Up @@ -228,7 +234,7 @@ onMounted(generateMenus);
<div class="flex text-sm font-medium">
{{ userStore.currentUser?.spec.displayName }}
</div>
<div class="flex">
<div v-if="currentRole" class="flex">
<VTag>
<template #leftIcon>
<IconUserSettings />
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ async function initApp() {

await loadUserPermissions();

const roleStore = useRoleStore();
await roleStore.fetchRoles();

try {
await loadPluginModules();
} catch (e) {
Expand Down
4 changes: 4 additions & 0 deletions src/modules/system/roles/RoleList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import Fuse from "fuse.js";
import { usePermission } from "@/utils/permission";
import { roleLabels } from "@/constants/labels";
import { SUPER_ROLE_NAME } from "@/constants/constants";
import { useRoleStore } from "@/stores/role";

const roleStore = useRoleStore();
const { currentUserHasPermission } = usePermission();

const editingModal = ref<boolean>(false);
Expand Down Expand Up @@ -90,6 +92,7 @@ const handleOpenEditingModal = (role: Role) => {
const onEditingModalClose = () => {
selectedRole.value = undefined;
handleFetchRoles();
roleStore.fetchRoles();
};

const handleCloneRole = async (role: Role) => {
Expand Down Expand Up @@ -139,6 +142,7 @@ const handleDelete = async (role: Role) => {
console.error("Failed to delete role", e);
} finally {
handleFetchRoles();
roleStore.fetchRoles();
}
},
});
Expand Down
9 changes: 8 additions & 1 deletion src/modules/system/users/UserDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { useRouter } from "vue-router";
import type { User } from "@halo-dev/api-client";
import { rbacAnnotations } from "@/constants/annotations";
import { formatDatetime } from "@/utils/date";
import { useRoleStore } from "@/stores/role";

const user = inject<Ref<User>>("user");

const roleStore = useRoleStore();

const roles = computed(() => {
return JSON.parse(
const names = JSON.parse(
user?.value?.metadata?.annotations?.[rbacAnnotations.ROLE_NAMES] || "[]"
);

return names.map((name: string) => {
return roleStore.getRoleDisplayName(name);
});
});

const router = useRouter();
Expand Down
8 changes: 7 additions & 1 deletion src/modules/system/users/UserList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useRouteQuery } from "@vueuse/router";
import Fuse from "fuse.js";
import { usePermission } from "@/utils/permission";
import { useUserStore } from "@/stores/user";
import { useRoleStore } from "@/stores/role";

const { currentUserHasPermission } = usePermission();

Expand All @@ -55,6 +56,7 @@ const selectedUser = ref<User>();
const refreshInterval = ref();

const userStore = useUserStore();
const roleStore = useRoleStore();

let fuse: Fuse<User> | undefined = undefined;

Expand Down Expand Up @@ -208,9 +210,13 @@ const handleOpenGrantPermissionModal = (user: User) => {
};

const getRoles = (user: User) => {
return JSON.parse(
const names = JSON.parse(
user.metadata.annotations?.[rbacAnnotations.ROLE_NAMES] || "[]"
);

return names.map((name: string) => {
return roleStore.getRoleDisplayName(name);
});
};

onMounted(() => {
Expand Down
42 changes: 29 additions & 13 deletions src/stores/role.ts
Original file line number Diff line number Diff line change
@@ -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 };
});