From 198d3adde07e9d3c58df4e703fc866564a48ce77 Mon Sep 17 00:00:00 2001
From: Ryan Wang
Date: Wed, 7 Dec 2022 12:40:53 +0800
Subject: [PATCH] feat: refining the logic of user roles (halo-dev/console#749)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
#### What type of PR is this?
/kind improvement
/milestone 2.0.1
#### What this PR does / why we need it:
完善用户角色的相关逻辑。适配 https://github.com/halo-dev/halo/pull/2865
1. 支持标识是否是系统保留角色。
2. 根据是否是系统保留角色,禁用修改和删除的操作。
3. 支持判断是否是超级管理员,如果是,默认勾选所有权限。
4. 优化 `包含 N 个权限` 文案的逻辑,超级管理员为 `包含所有权限`。
5. 优化 `基于此角色创建` 的逻辑,判断是否为超级管理员,如果是,需要设置所有角色模板到创建表单。
#### Which issue(s) this PR fixes:
Fixes https://github.com/halo-dev/halo/issues/2844
#### Screenshots:
#### Special notes for your reviewer:
测试方式:
1. Halo 需要切换到 https://github.com/halo-dev/halo/pull/2865 分支。
2. 测试角色相关的所有功能。
#### Does this PR introduce a user-facing change?
```release-note
完善 Console 端用户角色的相关逻辑
```
---
src/constants/constants.ts | 1 +
src/constants/labels.ts | 1 +
src/modules/system/roles/RoleDetail.vue | 64 ++++++++++++++-----
src/modules/system/roles/RoleList.vue | 59 +++++++++++++----
.../system/roles/composables/use-role.ts | 8 ++-
5 files changed, 105 insertions(+), 28 deletions(-)
create mode 100644 src/constants/constants.ts
diff --git a/src/constants/constants.ts b/src/constants/constants.ts
new file mode 100644
index 000000000..1a9befd1e
--- /dev/null
+++ b/src/constants/constants.ts
@@ -0,0 +1 @@
+export const SUPER_ROLE_NAME = "super-role";
diff --git a/src/constants/labels.ts b/src/constants/labels.ts
index c5df31e24..1cb0ac071 100644
--- a/src/constants/labels.ts
+++ b/src/constants/labels.ts
@@ -6,6 +6,7 @@ export enum pluginLabels {
// role
export enum roleLabels {
TEMPLATE = "halo.run/role-template",
+ SYSTEM_RESERVED = "rbac.authorization.halo.run/system-reserved",
}
// post
diff --git a/src/modules/system/roles/RoleDetail.vue b/src/modules/system/roles/RoleDetail.vue
index a4263a0ba..76ce43e5a 100644
--- a/src/modules/system/roles/RoleDetail.vue
+++ b/src/modules/system/roles/RoleDetail.vue
@@ -8,17 +8,19 @@ import {
VTabbar,
VTag,
VAvatar,
+ VAlert,
} from "@halo-dev/components";
import { useRoute } from "vue-router";
-import { onMounted, ref, watch } from "vue";
+import { computed, onMounted, ref, watch } from "vue";
import { apiClient } from "@/utils/api-client";
-import { pluginLabels } from "@/constants/labels";
+import { pluginLabels, roleLabels } from "@/constants/labels";
import { rbacAnnotations } from "@/constants/annotations";
import {
useRoleForm,
useRoleTemplateSelection,
} from "@/modules/system/roles/composables/use-role";
import { useUserFetch } from "@/modules/system/users/composables/use-user";
+import { SUPER_ROLE_NAME } from "@/constants/constants";
const route = useRoute();
@@ -31,6 +33,28 @@ const { formState, saving, handleCreateOrUpdate } = useRoleForm();
const { users } = useUserFetch({ fetchOnMounted: false });
+const isSystemReserved = computed(() => {
+ return (
+ formState.value.metadata.labels?.[roleLabels.SYSTEM_RESERVED] === "true"
+ );
+});
+
+const isSuperRole = computed(() => {
+ return formState.value.metadata.name === SUPER_ROLE_NAME;
+});
+
+const getRoleCountText = computed(() => {
+ if (formState.value.metadata.name === SUPER_ROLE_NAME) {
+ return `包含所有权限`;
+ }
+
+ const dependenciesCount = JSON.parse(
+ formState.value.metadata.annotations?.[rbacAnnotations.DEPENDENCIES] || "[]"
+ ).length;
+
+ return `包含 ${dependenciesCount} 个权限`;
+});
+
watch(
() => selectedRoleTemplates.value,
(newValue) => {
@@ -97,17 +121,7 @@ onMounted(() => {
- 包含 - {{ - JSON.parse( - formState.metadata.annotations?.[ - rbacAnnotations.DEPENDENCIES - ] || "[]" - ).length - }} - 个权限 + {{ getRoleCountText }}