2022-05-30 14:38:57 +00:00
|
|
|
<script lang="ts" setup>
|
2022-08-23 07:27:42 +00:00
|
|
|
// core libs
|
2024-01-24 02:34:11 +00:00
|
|
|
import { computed, ref } from "vue";
|
|
|
|
import type { Role, RoleList } from "@halo-dev/api-client";
|
2022-08-23 07:27:42 +00:00
|
|
|
|
|
|
|
// components
|
2022-05-30 14:38:57 +00:00
|
|
|
import {
|
2024-05-24 07:18:51 +00:00
|
|
|
Dialog,
|
2022-05-30 14:38:57 +00:00
|
|
|
IconAddCircle,
|
|
|
|
IconShieldUser,
|
2024-05-24 07:18:51 +00:00
|
|
|
Toast,
|
2022-06-14 07:56:55 +00:00
|
|
|
VButton,
|
|
|
|
VCard,
|
2024-05-24 07:18:51 +00:00
|
|
|
VDropdownItem,
|
2022-09-14 03:17:15 +00:00
|
|
|
VEntity,
|
|
|
|
VEntityField,
|
2022-11-24 03:46:10 +00:00
|
|
|
VLoading,
|
2024-05-24 07:18:51 +00:00
|
|
|
VPageHeader,
|
|
|
|
VStatusDot,
|
|
|
|
VTag,
|
2022-06-14 07:56:55 +00:00
|
|
|
} from "@halo-dev/components";
|
2022-07-26 10:04:51 +00:00
|
|
|
import RoleEditingModal from "./components/RoleEditingModal.vue";
|
2022-08-23 07:27:42 +00:00
|
|
|
|
|
|
|
// constants
|
2022-07-26 12:21:11 +00:00
|
|
|
import { rbacAnnotations } from "@/constants/annotations";
|
2022-08-23 06:33:53 +00:00
|
|
|
import { formatDatetime } from "@/utils/date";
|
2022-05-31 03:34:48 +00:00
|
|
|
|
2022-08-23 07:59:58 +00:00
|
|
|
// libs
|
2024-06-25 04:31:44 +00:00
|
|
|
import { coreApiClient } from "@halo-dev/api-client";
|
2022-09-28 10:37:01 +00:00
|
|
|
import Fuse from "fuse.js";
|
2022-09-30 09:48:19 +00:00
|
|
|
import { usePermission } from "@/utils/permission";
|
2022-12-07 04:40:53 +00:00
|
|
|
import { roleLabels } from "@/constants/labels";
|
|
|
|
import { SUPER_ROLE_NAME } from "@/constants/constants";
|
2023-03-23 08:54:33 +00:00
|
|
|
import { useI18n } from "vue-i18n";
|
2024-01-24 02:34:11 +00:00
|
|
|
import { useQuery } from "@tanstack/vue-query";
|
|
|
|
import { resolveDeepDependencies } from "@/utils/role";
|
2022-09-30 09:48:19 +00:00
|
|
|
|
|
|
|
const { currentUserHasPermission } = usePermission();
|
2023-03-23 08:54:33 +00:00
|
|
|
const { t } = useI18n();
|
2022-08-23 07:59:58 +00:00
|
|
|
|
2022-07-26 12:21:11 +00:00
|
|
|
const editingModal = ref<boolean>(false);
|
2022-09-28 10:37:01 +00:00
|
|
|
const selectedRole = ref<Role>();
|
2022-05-30 14:38:57 +00:00
|
|
|
|
2022-09-28 10:37:01 +00:00
|
|
|
let fuse: Fuse<Role> | undefined = undefined;
|
|
|
|
|
2024-01-24 02:34:11 +00:00
|
|
|
const { data: roleTemplates } = useQuery({
|
|
|
|
queryKey: ["role-templates"],
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await coreApiClient.role.listRole({
|
2024-01-24 02:34:11 +00:00
|
|
|
page: 0,
|
|
|
|
size: 0,
|
|
|
|
labelSelector: [`${roleLabels.TEMPLATE}=true`, "!halo.run/hidden"],
|
|
|
|
});
|
|
|
|
return data.items;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
data: roles,
|
|
|
|
isLoading,
|
|
|
|
refetch,
|
|
|
|
} = useQuery<RoleList>({
|
|
|
|
queryKey: ["roles"],
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await coreApiClient.role.listRole({
|
2024-01-24 02:34:11 +00:00
|
|
|
page: 0,
|
|
|
|
size: 0,
|
|
|
|
labelSelector: [`!${roleLabels.TEMPLATE}`],
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
refetchInterval(data) {
|
|
|
|
const hasDeletingRole = data?.items.some(
|
|
|
|
(item) => !!item.metadata.deletionTimestamp
|
|
|
|
);
|
|
|
|
return hasDeletingRole ? 1000 : false;
|
|
|
|
},
|
|
|
|
onSuccess(data) {
|
|
|
|
fuse = new Fuse(data.items, {
|
|
|
|
keys: [
|
|
|
|
{
|
|
|
|
name: "displayName",
|
|
|
|
getFn: (role) => {
|
|
|
|
return (
|
|
|
|
role.metadata.annotations?.[rbacAnnotations.DISPLAY_NAME] || ""
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"metadata.name",
|
|
|
|
],
|
2022-09-28 10:37:01 +00:00
|
|
|
useExtendedSearch: true,
|
2023-02-13 03:46:22 +00:00
|
|
|
threshold: 0.2,
|
2022-09-28 10:37:01 +00:00
|
|
|
});
|
2024-01-24 02:34:11 +00:00
|
|
|
},
|
|
|
|
enabled: computed(() => !!roleTemplates.value),
|
|
|
|
});
|
2022-09-28 10:37:01 +00:00
|
|
|
|
|
|
|
const keyword = ref("");
|
|
|
|
|
|
|
|
const searchResults = computed(() => {
|
|
|
|
if (!fuse || !keyword.value) {
|
2024-01-24 02:34:11 +00:00
|
|
|
return roles.value?.items || [];
|
2022-09-28 10:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fuse?.search(keyword.value).map((item) => item.item);
|
|
|
|
});
|
|
|
|
|
2022-12-07 04:40:53 +00:00
|
|
|
const isSystemReserved = (role: Role) => {
|
|
|
|
return role.metadata.labels?.[roleLabels.SYSTEM_RESERVED] === "true";
|
|
|
|
};
|
|
|
|
|
|
|
|
const getRoleCountText = (role: Role) => {
|
|
|
|
if (role.metadata.name === SUPER_ROLE_NAME) {
|
2023-03-23 08:54:33 +00:00
|
|
|
return t("core.role.common.text.contains_all_permissions");
|
2022-12-07 04:40:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 02:34:11 +00:00
|
|
|
const dependencies = new Set<string>(
|
|
|
|
resolveDeepDependencies(role, roleTemplates.value || [])
|
|
|
|
);
|
2022-12-07 04:40:53 +00:00
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
return t("core.role.common.text.contains_n_permissions", {
|
2024-01-24 02:34:11 +00:00
|
|
|
count: dependencies.size || 0,
|
2023-03-23 08:54:33 +00:00
|
|
|
});
|
2022-12-07 04:40:53 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 12:21:11 +00:00
|
|
|
const handleOpenEditingModal = (role: Role) => {
|
|
|
|
selectedRole.value = role;
|
|
|
|
editingModal.value = true;
|
2022-06-28 05:54:45 +00:00
|
|
|
};
|
|
|
|
|
2022-08-23 07:27:42 +00:00
|
|
|
const onEditingModalClose = () => {
|
2022-09-28 10:37:01 +00:00
|
|
|
selectedRole.value = undefined;
|
2024-05-24 07:18:51 +00:00
|
|
|
editingModal.value = false;
|
2024-01-24 02:34:11 +00:00
|
|
|
refetch();
|
2022-08-23 07:27:42 +00:00
|
|
|
};
|
2022-08-23 07:59:58 +00:00
|
|
|
|
2022-12-07 04:40:53 +00:00
|
|
|
const handleCloneRole = async (role: Role) => {
|
2023-08-01 02:46:20 +00:00
|
|
|
const roleToCreate: Role = {
|
|
|
|
apiVersion: "v1alpha1",
|
|
|
|
kind: "Role",
|
|
|
|
metadata: {
|
|
|
|
name: "",
|
|
|
|
generateName: "role-",
|
|
|
|
labels: {},
|
|
|
|
annotations: {
|
|
|
|
[rbacAnnotations.DEPENDENCIES]:
|
|
|
|
role.metadata.annotations?.[rbacAnnotations.DEPENDENCIES] || "",
|
|
|
|
[rbacAnnotations.DISPLAY_NAME]:
|
|
|
|
role.metadata.annotations?.[rbacAnnotations.DISPLAY_NAME] || "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: [],
|
|
|
|
};
|
2022-12-07 04:40:53 +00:00
|
|
|
|
|
|
|
// 如果是超级管理员角色,那么需要获取到所有角色模板并填充到表单
|
|
|
|
if (role.metadata.name === SUPER_ROLE_NAME) {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await coreApiClient.role.listRole({
|
2022-12-07 04:40:53 +00:00
|
|
|
page: 0,
|
|
|
|
size: 0,
|
|
|
|
labelSelector: [`${roleLabels.TEMPLATE}=true`, "!halo.run/hidden"],
|
|
|
|
});
|
|
|
|
const roleTemplateNames = data.items.map((item) => item.metadata.name);
|
|
|
|
if (roleToCreate.metadata.annotations) {
|
|
|
|
roleToCreate.metadata.annotations[rbacAnnotations.DEPENDENCIES] =
|
|
|
|
JSON.stringify(roleTemplateNames);
|
|
|
|
} else {
|
|
|
|
roleToCreate.metadata.annotations = {
|
|
|
|
[rbacAnnotations.DEPENDENCIES]: JSON.stringify(roleTemplateNames),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 07:59:58 +00:00
|
|
|
selectedRole.value = roleToCreate;
|
|
|
|
editingModal.value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDelete = async (role: Role) => {
|
2022-10-18 01:58:09 +00:00
|
|
|
Dialog.warning({
|
2023-03-23 08:54:33 +00:00
|
|
|
title: t("core.role.operations.delete.title"),
|
|
|
|
description: t("core.role.operations.delete.description"),
|
2022-08-23 07:59:58 +00:00
|
|
|
confirmType: "danger",
|
2023-03-23 08:54:33 +00:00
|
|
|
confirmText: t("core.common.buttons.confirm"),
|
|
|
|
cancelText: t("core.common.buttons.cancel"),
|
2022-08-23 07:59:58 +00:00
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2024-06-25 04:31:44 +00:00
|
|
|
await coreApiClient.role.deleteRole({
|
refactor: method parameters of api client (halo-dev/console#605)
<!-- Thanks for sending a pull request! Here are some tips for you:
1. 如果这是你的第一次,请阅读我们的贡献指南:<https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>。
1. If this is your first time, please read our contributor guidelines: <https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>.
2. 请根据你解决问题的类型为 Pull Request 添加合适的标签。
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request.
3. 请确保你已经添加并运行了适当的测试。
3. Ensure you have added or ran the appropriate tests for your PR.
-->
#### What type of PR is this?
/kind improvement
/milestone 2.0
<!--
添加其中一个类别:
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind optimization
适当添加其中一个或多个类别(可选):
Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->
#### What this PR does / why we need it:
修改 api-client 的请求参数结构,改为所有参数由一个对象包裹,而不是将各个参数作为方法的参数,防止因为后端参数结构发生改变,或者生成 api-client 时参数顺序发生改变导致请求异常。如:
```diff
await apiClient.extension.storage.group.updatestorageHaloRunV1alpha1Group(
- formState.value.metadata.name,
- formState.value
+ {
+ name: formState.value.metadata.name,
+ group: formState.value,
+ }
);
```
#### Which issue(s) this PR fixes:
<!--
PR 合并时自动关闭 issue。
Automatically closes linked issue when PR is merged.
用法:`Fixes #<issue 号>`,或者 `Fixes (粘贴 issue 完整链接)`
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
None
#### Screenshots:
<!--
如果此 PR 有 UI 的改动,最好截图说明这个 PR 的改动。
If there are UI changes to this PR, it is best to take a screenshot to illustrate the changes to this PR.
eg.
Before:

After:

-->
None
#### Special notes for your reviewer:
/cc @halo-dev/sig-halo-admin
#### Does this PR introduce a user-facing change?
<!--
如果当前 Pull Request 的修改不会造成用户侧的任何变更,在 `release-note` 代码块儿中填写 `NONE`。
否则请填写用户侧能够理解的 Release Note。如果当前 Pull Request 包含破坏性更新(Break Change),
Release Note 需要以 `action required` 开头。
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note
None
```
2022-09-06 02:26:11 +00:00
|
|
|
name: role.metadata.name,
|
|
|
|
});
|
2022-12-20 11:04:29 +00:00
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
Toast.success(t("core.common.toast.delete_success"));
|
2022-08-23 07:59:58 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to delete role", e);
|
2022-12-25 05:04:31 +00:00
|
|
|
} finally {
|
2024-01-24 02:34:11 +00:00
|
|
|
refetch();
|
2022-08-23 07:59:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2022-05-30 14:38:57 +00:00
|
|
|
</script>
|
|
|
|
<template>
|
2022-07-26 12:21:11 +00:00
|
|
|
<RoleEditingModal
|
2024-05-24 07:18:51 +00:00
|
|
|
v-if="editingModal"
|
2022-07-26 12:21:11 +00:00
|
|
|
:role="selectedRole"
|
2022-08-23 07:27:42 +00:00
|
|
|
@close="onEditingModalClose"
|
2022-07-26 12:21:11 +00:00
|
|
|
/>
|
2022-05-31 03:34:48 +00:00
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
<VPageHeader :title="$t('core.role.title')">
|
2022-05-30 14:38:57 +00:00
|
|
|
<template #icon>
|
|
|
|
<IconShieldUser class="mr-2 self-center" />
|
|
|
|
</template>
|
|
|
|
<template #actions>
|
2022-07-19 06:07:28 +00:00
|
|
|
<VButton
|
|
|
|
v-permission="['system:roles:manage']"
|
|
|
|
type="secondary"
|
2022-07-26 12:21:11 +00:00
|
|
|
@click="editingModal = true"
|
2022-07-19 06:07:28 +00:00
|
|
|
>
|
2022-05-30 14:38:57 +00:00
|
|
|
<template #icon>
|
|
|
|
<IconAddCircle class="h-full w-full" />
|
|
|
|
</template>
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.new") }}
|
2022-05-30 14:38:57 +00:00
|
|
|
</VButton>
|
|
|
|
</template>
|
|
|
|
</VPageHeader>
|
|
|
|
|
|
|
|
<div class="m-0 md:m-4">
|
|
|
|
<VCard :body-class="['!p-0']">
|
|
|
|
<template #header>
|
|
|
|
<div class="block w-full bg-gray-50 px-4 py-3">
|
|
|
|
<div
|
|
|
|
class="relative flex flex-col items-start sm:flex-row sm:items-center"
|
|
|
|
>
|
|
|
|
<div class="flex w-full flex-1 sm:w-auto">
|
2022-09-28 10:37:01 +00:00
|
|
|
<FormKit
|
|
|
|
v-model="keyword"
|
2023-03-23 08:54:33 +00:00
|
|
|
:placeholder="$t('core.common.placeholder.search')"
|
2022-09-28 10:37:01 +00:00
|
|
|
type="text"
|
|
|
|
></FormKit>
|
2022-05-30 14:38:57 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
2024-01-24 02:34:11 +00:00
|
|
|
<VLoading v-if="isLoading" />
|
2022-11-24 03:46:10 +00:00
|
|
|
<Transition v-else appear name="fade">
|
|
|
|
<ul
|
|
|
|
class="box-border h-full w-full divide-y divide-gray-100"
|
|
|
|
role="list"
|
|
|
|
>
|
|
|
|
<li v-for="(role, index) in searchResults" :key="index">
|
|
|
|
<VEntity>
|
|
|
|
<template #start>
|
|
|
|
<VEntityField
|
|
|
|
:title="
|
|
|
|
role.metadata.annotations?.[rbacAnnotations.DISPLAY_NAME] ||
|
|
|
|
role.metadata.name
|
|
|
|
"
|
2022-12-07 04:40:53 +00:00
|
|
|
:description="getRoleCountText(role)"
|
2022-11-24 03:46:10 +00:00
|
|
|
:route="{
|
|
|
|
name: 'RoleDetail',
|
|
|
|
params: {
|
|
|
|
name: role.metadata.name,
|
|
|
|
},
|
|
|
|
}"
|
|
|
|
></VEntityField>
|
|
|
|
</template>
|
|
|
|
<template #end>
|
2022-11-28 11:28:19 +00:00
|
|
|
<!-- TODO: 支持显示用户数量 -->
|
|
|
|
<VEntityField v-if="false" description="0 个用户" />
|
2022-11-24 03:46:10 +00:00
|
|
|
<VEntityField>
|
|
|
|
<template #description>
|
2022-12-07 04:40:53 +00:00
|
|
|
<VTag>
|
2023-03-23 08:54:33 +00:00
|
|
|
{{
|
|
|
|
isSystemReserved(role)
|
|
|
|
? t("core.role.common.text.system_reserved")
|
|
|
|
: t("core.role.common.text.custom")
|
|
|
|
}}
|
2022-12-07 04:40:53 +00:00
|
|
|
</VTag>
|
2022-11-24 03:46:10 +00:00
|
|
|
</template>
|
|
|
|
</VEntityField>
|
2022-12-25 05:04:31 +00:00
|
|
|
<VEntityField v-if="role.metadata.deletionTimestamp">
|
|
|
|
<template #description>
|
2023-03-23 08:54:33 +00:00
|
|
|
<VStatusDot
|
|
|
|
v-tooltip="$t('core.common.status.deleting')"
|
|
|
|
state="warning"
|
|
|
|
animate
|
|
|
|
/>
|
2022-12-25 05:04:31 +00:00
|
|
|
</template>
|
|
|
|
</VEntityField>
|
2022-11-24 03:46:10 +00:00
|
|
|
<VEntityField>
|
|
|
|
<template #description>
|
|
|
|
<span class="truncate text-xs tabular-nums text-gray-500">
|
|
|
|
{{ formatDatetime(role.metadata.creationTimestamp) }}
|
|
|
|
</span>
|
|
|
|
</template>
|
|
|
|
</VEntityField>
|
|
|
|
</template>
|
|
|
|
<template
|
|
|
|
v-if="currentUserHasPermission(['system:roles:manage'])"
|
|
|
|
#dropdownItems
|
2022-09-13 04:08:12 +00:00
|
|
|
>
|
2023-03-27 08:06:13 +00:00
|
|
|
<VDropdownItem
|
2022-12-07 04:40:53 +00:00
|
|
|
v-if="!isSystemReserved(role)"
|
2022-11-24 03:46:10 +00:00
|
|
|
@click="handleOpenEditingModal(role)"
|
|
|
|
>
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.edit") }}
|
2023-03-27 08:06:13 +00:00
|
|
|
</VDropdownItem>
|
|
|
|
<VDropdownItem
|
2022-12-07 04:40:53 +00:00
|
|
|
v-if="!isSystemReserved(role)"
|
2022-11-24 03:46:10 +00:00
|
|
|
type="danger"
|
|
|
|
@click="handleDelete(role)"
|
|
|
|
>
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.delete") }}
|
2023-03-27 08:06:13 +00:00
|
|
|
</VDropdownItem>
|
|
|
|
<VDropdownItem @click="handleCloneRole(role)">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{
|
|
|
|
$t("core.role.operations.create_based_on_this_role.button")
|
|
|
|
}}
|
2023-03-27 08:06:13 +00:00
|
|
|
</VDropdownItem>
|
2022-11-24 03:46:10 +00:00
|
|
|
</template>
|
|
|
|
</VEntity>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</Transition>
|
2022-05-30 14:38:57 +00:00
|
|
|
</VCard>
|
|
|
|
</div>
|
|
|
|
</template>
|