-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: role #3229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: role #3229
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { RoleTypeEnum } from '@/enums/system' | ||
|
|
||
| interface RoleItem { | ||
| id: string, | ||
| role_name: string, | ||
| type: RoleTypeEnum, | ||
| create_user: string, | ||
| internal: boolean, | ||
| } | ||
|
|
||
| interface ChildrenPermissionItem { | ||
| id: string | ||
| name: string | ||
| enable: boolean | ||
| } | ||
|
|
||
| interface RolePermissionItem { | ||
| id: string, | ||
| name: string, | ||
| children: { | ||
| id: string, | ||
| name: string, | ||
| permission: ChildrenPermissionItem[], | ||
| enable: boolean, | ||
| }[] | ||
| } | ||
|
|
||
| interface RoleTableDataItem { | ||
| module: string | ||
| name: string | ||
| permission: ChildrenPermissionItem[] | ||
| enable: boolean | ||
| perChecked: string[] | ||
| indeterminate: boolean | ||
| } | ||
|
|
||
| interface CreateOrUpdateParams { | ||
| role_id?: string, | ||
| role_name: string, | ||
| role_type?: RoleTypeEnum, | ||
| } | ||
|
|
||
| export type { RoleItem, RolePermissionItem, RoleTableDataItem, CreateOrUpdateParams, ChildrenPermissionItem } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { get, post, del } from '@/request/index' | ||
| import type { Ref } from 'vue' | ||
| import { Result } from '@/request/Result' | ||
| import type { RoleItem, RolePermissionItem, CreateOrUpdateParams } from '@/api/type/role' | ||
| import { RoleTypeEnum } from '@/enums/system' | ||
|
|
||
| const prefix = '/system/role' | ||
| /** | ||
| * 获取角色列表 | ||
| */ | ||
| const getRoleList: (loading?: Ref<boolean>) => Promise<Result<{ internal_role: RoleItem[], custom_role: RoleItem[] }>> = (loading) => { | ||
| return get(`${prefix}`, undefined, loading) | ||
| } | ||
|
|
||
| /** | ||
| * 根据类型获取角色权限模版列表 | ||
| */ | ||
| const getRoleTemplate: (role_type: RoleTypeEnum, loading?: Ref<boolean>) => Promise<Result<RolePermissionItem[]>> = (role_type, loading) => { | ||
| return get(`${prefix}/template/${role_type}`, undefined, loading) | ||
| } | ||
|
|
||
| /** | ||
| * 获取角色权限选中 | ||
| */ | ||
| const getRolePermissionList: (role_id: string, loading?: Ref<boolean>) => Promise<Result<RolePermissionItem[]>> = (role_id, loading) => { | ||
| return get(`${prefix}/${role_id}/permission`, undefined, loading) | ||
| } | ||
|
|
||
| /** | ||
| * 新建或更新角色 | ||
| */ | ||
| const CreateOrUpdateRole: ( | ||
| data: CreateOrUpdateParams, | ||
| loading?: Ref<boolean>, | ||
| ) => Promise<Result<any>> = (data, loading) => { | ||
| return post(`${prefix}`, data, undefined, loading) | ||
| } | ||
|
|
||
| /** | ||
| * 删除角色 | ||
| */ | ||
| const deleteRole: (role_id: string, loading?: Ref<boolean>) => Promise<Result<boolean>> = ( | ||
| role_id, | ||
| loading, | ||
| ) => { | ||
| return del(`${prefix}/${role_id}`, undefined, {}, loading) | ||
| } | ||
|
|
||
| /** | ||
| * 保存角色权限 | ||
| */ | ||
| const saveRolePermission: ( | ||
| role_id: string, | ||
| data: { id: string, enable: boolean }[], | ||
| loading?: Ref<boolean>, | ||
| ) => Promise<Result<any>> = (role_id, data, loading) => { | ||
| return post(`${prefix}/${role_id}/permission`, data, undefined, loading) | ||
| } | ||
|
|
||
| export default { | ||
| getRoleList, | ||
| getRolePermissionList, | ||
| getRoleTemplate, | ||
| CreateOrUpdateRole, | ||
| deleteRole, | ||
| saveRolePermission | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export default { | ||
| title: 'Role management', | ||
| internalRole: 'System built-in roles', | ||
| customRole: 'Custom roles', | ||
| systemAdmin: 'System admin', | ||
| workspaceAdmin: 'Workspace admin', | ||
| user: 'Regular user', | ||
| roleName: 'Role name', | ||
| inheritingRole: 'Inherited role', | ||
| delete: { | ||
| confirmTitle: 'Confirm to delete role:', | ||
| confirmMessage: 'After deletion, all members under this role will be removed. Please proceed with caution.', | ||
| }, | ||
| permission: { | ||
| title: 'Permission configuration', | ||
| operationTarget: 'Operation target', | ||
| moduleName: 'Module name' | ||
| }, | ||
| member: { | ||
| title: 'Members' | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export default { | ||
| title: '角色管理', | ||
| internalRole: '系统内置角色', | ||
| customRole: '自定义角色', | ||
| systemAdmin: '系统管理员', | ||
| workspaceAdmin: '工作空间管理员', | ||
| user: '普通用户', | ||
| roleName: '角色名称', | ||
| inheritingRole: '继承角色', | ||
| delete: { | ||
| confirmTitle: '是否删除角色:', | ||
| confirmMessage: '删除后,该角色下的成员都会被移除,请谨慎操作。', | ||
| }, | ||
| permission: { | ||
| title: '权限配置', | ||
| operationTarget: '操作对象', | ||
| moduleName: '模块名称' | ||
| }, | ||
| member: { | ||
| title: '成员' | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export default { | ||
| title: '角色管理', | ||
| internalRole: '系統內置角色', | ||
| customRole: '自定義角色', | ||
| systemAdmin: '系統管理員', | ||
| workspaceAdmin: '工作空間管理員', | ||
| user: '普通用戶', | ||
| roleName: '角色名稱', | ||
| inheritingRole: '繼承角色', | ||
| delete: { | ||
| confirmTitle: '是否刪除角色:', | ||
| confirmMessage: '刪除後,該角色下的成員都會被移除,請謹慎操作。', | ||
| }, | ||
| permission: { | ||
| title: '權限配置', | ||
| operationTarget: '操作對象', | ||
| moduleName: '模塊名稱' | ||
| }, | ||
| member: { | ||
| title: '成員' | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <template> | ||
| <el-dialog :title="`${!form.role_id ? $t('common.create') : $t('common.rename')}${$t('views.role.customRole')}`" | ||
| v-model="dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true"> | ||
| <el-form label-position="top" ref="formRef" :rules="rules" :model="form" require-asterisk-position="right"> | ||
| <el-form-item :label="$t('views.role.roleName')" prop="role_name"> | ||
| <el-input v-model="form.role_name" maxlength="64" | ||
| :placeholder="`${$t('common.inputPlaceholder')}${$t('views.role.roleName')}`" /> | ||
| </el-form-item> | ||
| <el-form-item v-if="!form.role_id" :label="$t('views.role.inheritingRole')" prop="role_type"> | ||
| <el-select v-model="form.role_type" | ||
| :placeholder="`${$t('common.selectPlaceholder')}${$t('views.role.inheritingRole')}`"> | ||
| <el-option v-for="(label, value) in roleTypeMap" :key="value" :label="label" :value="value" /> | ||
| </el-select> | ||
| </el-form-item> | ||
| </el-form> | ||
| <template #footer> | ||
| <span class="dialog-footer"> | ||
| <el-button @click.prevent="dialogVisible = false"> {{ $t('common.cancel') }} </el-button> | ||
| <el-button type="primary" @click="submit(formRef)" :loading="loading"> | ||
| {{ !form.role_id ? $t('common.create') : $t('common.save') }} | ||
| </el-button> | ||
| </span> | ||
| </template> | ||
| </el-dialog> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { ref, reactive } from 'vue' | ||
| import type { FormInstance } from 'element-plus' | ||
| import { MsgSuccess } from '@/utils/message' | ||
| import { t } from '@/locales' | ||
| import type { RoleItem, CreateOrUpdateParams } from '@/api/type/role' | ||
| import RoleApi from '@/api/user/role' | ||
| import { roleTypeMap } from '../index' | ||
|
|
||
| const emit = defineEmits<{ | ||
| (e: 'refresh', currentRole: RoleItem): void; | ||
| }>(); | ||
|
|
||
| const dialogVisible = ref<boolean>(false) | ||
| const defaultForm = { | ||
| role_name: '' | ||
| } | ||
| const form = ref<CreateOrUpdateParams>({ | ||
| ...defaultForm, | ||
| }) | ||
| function open(item?: RoleItem) { | ||
| if (item) { | ||
| form.value = { | ||
| role_name: item.role_name, | ||
| role_type: item.type, | ||
| role_id: item.id, | ||
| } | ||
| } else { | ||
| form.value = { ...defaultForm } | ||
| } | ||
| dialogVisible.value = true | ||
| } | ||
|
|
||
| const formRef = ref<FormInstance>(); | ||
|
|
||
| const rules = reactive({ | ||
| role_name: [{ required: true, message: `${t('common.inputPlaceholder')}${t('views.role.roleName')}`, trigger: 'blur' }], | ||
| role_type: [{ required: true, message: `${t('common.selectPlaceholder')}${t('views.role.inheritingRole')}`, trigger: 'blur' }] | ||
| }) | ||
|
|
||
| const loading = ref<boolean>(false) | ||
| const submit = async (formEl: FormInstance | undefined) => { | ||
| if (!formEl) return | ||
| await formEl.validate((valid) => { | ||
| if (valid) { | ||
| RoleApi.CreateOrUpdateRole(form.value, loading).then((res: any) => { | ||
| MsgSuccess(!form.value.role_id ? t('common.createSuccess') : t('common.renameSuccess')) | ||
| emit('refresh', res.data) | ||
| dialogVisible.value = false | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| defineExpose({ open }) | ||
| </script> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <template> | ||
| <div></div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"></script> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code you've provided seems to be a Vue.js component using Element Plus. Here are a few points of concern:
Duplicate Import Statements: You have two imports for
MsgSuccesswhich is unnecessary and could potentially lead to conflicts.Variable Naming Consistency: It's good practice to use consistent naming, such as PascalCase for variable names unless they align with existing conventions or specific libraries.
Conditional Check for Role ID: The logic for opening the dialog is inconsistent. When passing an item from parent components, it should handle both creating new roles (no role_id) and renaming them (has role_id).
Loading State Management: Ensure that the loading state (
loading) is properly reset after successful operations, especially if multiple submissions occur.Error Handling: Implement proper error handling for API calls to
RoleApi.CreateOrUpdateRole.Accessibility Considerations: Ensure that all elements have appropriate accessibility attributes like
aria-labelsandalttext, depending on their usage.Type Definitions: Verify that the types defined in
interfacematch those used throughout the code to prevent runtime errors.Here’s an updated version of the script part based on these considerations:
This revision addresses some of the mentioned concerns while maintaining readability and adhering to best practices. Make sure to update other parts of your application accordingly, such as updating any usages or additional styling changes where needed.