From 087d478094ed745b7fa631d6603b352ec46eadce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B0=8F=E6=B6=9B?= <1537080775@qq.com> Date: Fri, 28 Jun 2024 15:19:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(role=5Fper=5F20240628):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=9D=83=E9=99=90=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、修复首次打开权限配置,按钮小齿轮数据权限不显示bug 2、优化自定义数据权限:在input里面显示当前配置用户已有的权限 3、优化自定义数据权限:在树形选择器中,禁用掉当前登录用户没有权限的部门 --- .../views/role_menu_button_permission.py | 142 ++++++------------ .../views/system/login/component/account.vue | 4 +- .../role/components/PermissionComNew/api.ts | 5 +- .../components/PermissionComNew/index.vue | 22 ++- 4 files changed, 70 insertions(+), 103 deletions(-) diff --git a/backend/dvadmin/system/views/role_menu_button_permission.py b/backend/dvadmin/system/views/role_menu_button_permission.py index d44fab2b..4c933f4b 100644 --- a/backend/dvadmin/system/views/role_menu_button_permission.py +++ b/backend/dvadmin/system/views/role_menu_button_permission.py @@ -256,86 +256,45 @@ class RoleMenuButtonPermissionViewSet(CustomModelViewSet): is_superuser = request.user.is_superuser if is_superuser: data = [ - { - "value": 0, - "label": '仅本人数据权限' - }, - { - "value": 1, - "label": '本部门及以下数据权限' - }, - { - "value": 2, - "label": '本部门数据权限' - }, - { - "value": 3, - "label": '全部数据权限' - }, - { - "value": 4, - "label": '自定义数据权限' - } + {"value": 0, "label": '仅本人数据权限'}, + {"value": 1, "label": '本部门及以下数据权限'}, + {"value": 2, "label": '本部门数据权限'}, + {"value": 3, "label": '全部数据权限'}, + {"value": 4, "label": '自定义数据权限'} ] return DetailResponse(data=data) else: - data = [] + params = request.query_params + data = [{"value": 0, "label": '仅本人数据权限'}] role_list = request.user.role.values_list('id', flat=True) - if params := request.query_params: - if menu_button_id := params.get('menu_button', None): - role_queryset = RoleMenuButtonPermission.objects.filter( - role__in=role_list, menu_button__id=menu_button_id - ).values_list('data_range', flat=True) - data_range_list = list(set(role_queryset)) - for item in data_range_list: - if item == 0: - data = [{ - "value": 0, - "label": '仅本人数据权限' - }] - elif item == 1: - data = [{ - "value": 0, - "label": '仅本人数据权限' - }, { - "value": 1, - "label": '本部门及以下数据权限' - }, - { - "value": 2, - "label": '本部门数据权限' - }] - elif item == 2: - data = [{ - "value": 0, - "label": '仅本人数据权限' - }, - { - "value": 2, - "label": '本部门数据权限' - }] - elif item == 3: - data = [{ - "value": 0, - "label": '仅本人数据权限' - }, - { - "value": 3, - "label": '全部数据权限' - }, ] - elif item == 4: - data = [{ - "value": 0, - "label": '仅本人数据权限' - }, - { - "value": 4, - "label": '自定义数据权限' - }] - else: - data = [] - return DetailResponse(data=data) - return ErrorResponse(msg="参数错误") + # 权限页面进入初始化获取所有的数据权限范围 + role_queryset = RoleMenuButtonPermission.objects.filter( + role__in=role_list + ).values_list('data_range', flat=True) + # 通过按钮小齿轮获取指定按钮的权限 + if menu_button_id := params.get('menu_button', None): + role_queryset = RoleMenuButtonPermission.objects.filter( + role__in=role_list, menu_button__id=menu_button_id + ).values_list('data_range', flat=True) + + data_range_list = list(set(role_queryset)) + for item in data_range_list: + if item == 0: + data = data + elif item == 1: + data.extend([ + {"value": 1, "label": '本部门及以下数据权限'}, + {"value": 2, "label": '本部门数据权限'} + ]) + elif item == 2: + data.extend([{"value": 2, "label": '本部门数据权限'}]) + elif item == 3: + data.extend([{"value": 3, "label": '全部数据权限'}]) + elif item == 4: + data.extend([{"value": 4, "label": '自定义数据权限'}]) + else: + data = [] + return DetailResponse(data=data) @action(methods=['get'], detail=False, permission_classes=[IsAuthenticated]) def role_to_dept_all(self, request): @@ -344,23 +303,22 @@ class RoleMenuButtonPermissionViewSet(CustomModelViewSet): :param request: :return: """ - params = request.query_params is_superuser = request.user.is_superuser - if is_superuser: - queryset = Dept.objects.values('id', 'name', 'parent') - else: - if not params: - return ErrorResponse(msg="参数错误") - menu_button = params.get('menu_button') - if menu_button is None: - return ErrorResponse(msg="参数错误") - role_list = request.user.role.values_list('id', flat=True) - queryset = RoleMenuButtonPermission.objects.filter(role__in=role_list, menu_button=None).values( - dept_id=F('dept__id'), - name=F('dept__name'), - parent=F('dept__parent') - ) - return DetailResponse(data=queryset) + params = request.query_params + role_id = params.get('role') + menu_button_id = params.get('menu_button') + dept_checked = RoleMenuButtonPermission.objects.filter( + role_id=role_id, menu_button_id=menu_button_id + ).values_list('dept', flat=True) + dept_list = Dept.objects.values('id', 'name', 'parent') + data = { + 'depts': [], + 'dept_checked': dept_checked + } + for dept in dept_list: + dept["disabled"] = not (is_superuser | dept["id"] in dept_checked) + data['depts'].append(dept) + return DetailResponse(data=data) @action(methods=['get'], detail=False, permission_classes=[IsAuthenticated]) def menu_to_button(self, request): diff --git a/web/src/views/system/login/component/account.vue b/web/src/views/system/login/component/account.vue index ee4e64fa..def80eb7 100644 --- a/web/src/views/system/login/component/account.vue +++ b/web/src/views/system/login/component/account.vue @@ -80,8 +80,8 @@ export default defineComponent({ const state = reactive({ isShowPassword: false, ruleForm: { - username: '', - password: '', + username: 'superadmin', + password: 'admin123456', captcha: '', captchaKey: '', captchaImgBase: '', diff --git a/web/src/views/system/role/components/PermissionComNew/api.ts b/web/src/views/system/role/components/PermissionComNew/api.ts index eee6d2a1..68793c06 100644 --- a/web/src/views/system/role/components/PermissionComNew/api.ts +++ b/web/src/views/system/role/components/PermissionComNew/api.ts @@ -40,10 +40,11 @@ export function getDataPermissionRangeAll() { method: 'get', }) } -export function getDataPermissionDept() { +export function getDataPermissionDept(query:object) { return request({ url: '/api/system/role_menu_button_permission/role_to_dept_all/', - method: 'get' + method: 'get', + params:query }) } diff --git a/web/src/views/system/role/components/PermissionComNew/index.vue b/web/src/views/system/role/components/PermissionComNew/index.vue index 7770b05b..3babb8e4 100644 --- a/web/src/views/system/role/components/PermissionComNew/index.vue +++ b/web/src/views/system/role/components/PermissionComNew/index.vue @@ -124,6 +124,7 @@ watch( (val) => { drawerVisible.value = val; getMenuBtnPermission() + getDataPermissionRangeLable() } ); @@ -144,9 +145,10 @@ let menuCurrent = ref>({}); let menuBtnCurrent = ref(-1); let dialogVisible = ref(false); let dataPermissionRange = ref([]); +let dataPermissionRangeLabel = ref([]); const formatDataRange = computed(() => { return function (datarange: number) { - const findItem = dataPermissionRange.value.find((i) => i.value === datarange); + const findItem = dataPermissionRangeLabel.value.find((i) => i.value === datarange); return findItem?.label || '' } }) @@ -158,6 +160,11 @@ const getMenuBtnPermission = async () => { const resMenu = await getRolePremission({ role: props.roleId }) menuData.value = resMenu.data } +// 获取按钮的数据权限下拉选项 +const getDataPermissionRangeLable = async () => { + const resRange = await getDataPermissionRange({ role: props.roleId }) + dataPermissionRangeLabel.value = resRange.data; +} const fetchData = async (btnId) => { try { @@ -170,9 +177,9 @@ const fetchData = async (btnId) => { } }; -const handleCollapseChange = (val: number) => { - collapseCurrent.value = [val]; -}; +// const handleCollapseChange = (val: number) => { +// collapseCurrent.value = [val]; +// }; /** * 设置按钮数据权限 @@ -194,9 +201,10 @@ const handleColumnChange = (val: boolean, record: MenusType, btnType: string) => const handlePermissionRangeChange = async (val: number) => { if (val === 4) { - const res = await getDataPermissionDept(); - const data = XEUtils.toArrayTree(res.data, { parentKey: 'parent', strict: false }); - deptData.value = data; + const res = await getDataPermissionDept({ role: props.roleId,menu_button:menuBtnCurrent.value }); + const depts = XEUtils.toArrayTree(res.data.depts, { parentKey: 'parent', strict: false }); + deptData.value = depts; + customDataPermission.value = res.data.dept_checked; } }; -- Gitee From 1b8a502d6695fc121f07c89c491b6786e195ea4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B0=8F=E6=B6=9B?= <1537080775@qq.com> Date: Fri, 28 Jun 2024 15:36:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(role=5Fper=5F20240628):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=9D=83=E9=99=90=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、修复第一次选择自定义数据权限无法设置的bug --- backend/dvadmin/system/views/role_menu_button_permission.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/dvadmin/system/views/role_menu_button_permission.py b/backend/dvadmin/system/views/role_menu_button_permission.py index 4c933f4b..a3e85001 100644 --- a/backend/dvadmin/system/views/role_menu_button_permission.py +++ b/backend/dvadmin/system/views/role_menu_button_permission.py @@ -313,10 +313,11 @@ class RoleMenuButtonPermissionViewSet(CustomModelViewSet): dept_list = Dept.objects.values('id', 'name', 'parent') data = { 'depts': [], - 'dept_checked': dept_checked + 'dept_checked': [] if dept_checked.exists() else dept_checked } + for dept in dept_list: - dept["disabled"] = not (is_superuser | dept["id"] in dept_checked) + dept["disabled"] = False if is_superuser else dept["id"] not in dept_checked data['depts'].append(dept) return DetailResponse(data=data) -- Gitee From 0779cc1a842a071736996f74a356bfe2bec09a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B0=8F=E6=B6=9B?= <1537080775@qq.com> Date: Fri, 28 Jun 2024 15:46:44 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(role=5Fper=5F20240628):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=9D=83=E9=99=90=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、修复第一次选择自定义数据权限无法设置的bug --- .../dvadmin/system/views/role_menu_button_permission.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/backend/dvadmin/system/views/role_menu_button_permission.py b/backend/dvadmin/system/views/role_menu_button_permission.py index 4c933f4b..b53c31df 100644 --- a/backend/dvadmin/system/views/role_menu_button_permission.py +++ b/backend/dvadmin/system/views/role_menu_button_permission.py @@ -307,16 +307,17 @@ class RoleMenuButtonPermissionViewSet(CustomModelViewSet): params = request.query_params role_id = params.get('role') menu_button_id = params.get('menu_button') - dept_checked = RoleMenuButtonPermission.objects.filter( + dept_checked = RoleMenuButtonPermission.objects.get( role_id=role_id, menu_button_id=menu_button_id - ).values_list('dept', flat=True) + ).dept.all().values_list('id', flat=True) dept_list = Dept.objects.values('id', 'name', 'parent') data = { 'depts': [], - 'dept_checked': dept_checked + 'dept_checked': dept_checked if dept_checked.exists() else [] } + for dept in dept_list: - dept["disabled"] = not (is_superuser | dept["id"] in dept_checked) + dept["disabled"] = False if is_superuser else dept["id"] not in dept_checked data['depts'].append(dept) return DetailResponse(data=data) -- Gitee