mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-05 23:11:24 +08:00
Optimized the selection of permissions
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.cfive.pinnacle.service.permission.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.cfive.pinnacle.entity.permission.Power;
|
||||
import com.cfive.pinnacle.entity.permission.Role;
|
||||
import com.cfive.pinnacle.entity.permission.PowerRole;
|
||||
import com.cfive.pinnacle.exception.DataValidationFailedException;
|
||||
import com.cfive.pinnacle.mapper.permission.RoleMapper;
|
||||
import com.cfive.pinnacle.mapper.permission.PowerRoleMapper;
|
||||
import com.cfive.pinnacle.service.permission.IRoleService;
|
||||
@@ -13,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -51,6 +54,8 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean addRole(Role role) {
|
||||
handlePower(role);
|
||||
|
||||
if (roleMapper.insert(role) == 1) {
|
||||
role.getPowers().forEach(power -> {
|
||||
PowerRole powerRole = new PowerRole();
|
||||
@@ -69,6 +74,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean modifyRole(Role role) {
|
||||
handlePower(role);
|
||||
roleMapper.updateById(role);
|
||||
Role originalRole = getRole(role.getId());
|
||||
HashSet<Long> newPowerIds = new HashSet<>();
|
||||
@@ -97,4 +103,20 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
private void handlePower(Role role) {
|
||||
List<Power> powers = role.getPowers();
|
||||
Set<Long> elementIds = new HashSet<>();
|
||||
Set<Long> menuIds = new HashSet<>();
|
||||
powers.forEach(power -> {
|
||||
Long powerId = power.getId();
|
||||
if (powerId % 100 == 0) {
|
||||
throw new DataValidationFailedException("Illegal data");
|
||||
}
|
||||
elementIds.add(powerId - powerId % 100);
|
||||
menuIds.add(powerId - powerId % 10000);
|
||||
});
|
||||
elementIds.forEach(elementId -> powers.add(new Power(elementId, 2L)));
|
||||
menuIds.forEach(menuId -> powers.add(new Power(menuId, 1L)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,28 +187,28 @@ begin;
|
||||
insert into t_power(id, type_id)
|
||||
values (3020101, 3);
|
||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
||||
VALUES (3020101, '获取所有公告', 'notice:manage:get', id, 3030100, null);
|
||||
VALUES (3020101, '获取所有公告', 'notice:manage:get', id, 3020100, null);
|
||||
commit;
|
||||
|
||||
begin;
|
||||
insert into t_power(id, type_id)
|
||||
values (3020102, 3);
|
||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
||||
VALUES (3020102, '发布公告', 'notice:manage:add', id, 3030100, null);
|
||||
VALUES (3020102, '发布公告', 'notice:manage:add', id, 3020100, null);
|
||||
commit;
|
||||
|
||||
begin;
|
||||
insert into t_power(id, type_id)
|
||||
values (3020103, 3);
|
||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
||||
VALUES (3020103, '删除公告', 'notice:manage:delete', id, 3030100, null);
|
||||
VALUES (3020103, '删除公告', 'notice:manage:delete', id, 3020100, null);
|
||||
commit;
|
||||
|
||||
begin;
|
||||
insert into t_power(id, type_id)
|
||||
values (3020104, 3);
|
||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
||||
VALUES (3020104, '修改公告', 'notice:manage:modify', id, 3030100, null);
|
||||
VALUES (3020104, '修改公告', 'notice:manage:modify', id, 3020100, null);
|
||||
commit;
|
||||
|
||||
begin;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<icon-pinnacle-plus />
|
||||
</el-icon>
|
||||
</template>
|
||||
<template #default> 添加 </template>
|
||||
<template #default> 添加</template>
|
||||
</el-button>
|
||||
<common-table
|
||||
:table-date="roleTable"
|
||||
@@ -46,6 +46,16 @@
|
||||
show-word-limit
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="状态">
|
||||
<el-switch
|
||||
v-model="roleForm.enable"
|
||||
inline-prompt
|
||||
active-text="启用"
|
||||
:active-value="1"
|
||||
inactive-text="禁用"
|
||||
:inactive-value="0"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="角色权限">
|
||||
<el-tree
|
||||
:data="powerTree"
|
||||
@@ -58,23 +68,13 @@
|
||||
@check-change="handleSelectedPowerChange"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="状态">
|
||||
<el-switch
|
||||
v-model="roleForm.enable"
|
||||
inline-prompt
|
||||
active-text="启用"
|
||||
:active-value="1"
|
||||
inactive-text="禁用"
|
||||
:inactive-value="0"
|
||||
/>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-scrollbar>
|
||||
</template>
|
||||
<template #footer>
|
||||
<el-button type="primary" @click="handleSubmit" :disabled="dialogLoading"
|
||||
>提交</el-button
|
||||
>
|
||||
>提交
|
||||
</el-button>
|
||||
<el-button @click="handleCancel" :disabled="dialogLoading">取消</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
@@ -204,6 +204,11 @@ export default {
|
||||
}
|
||||
menu.children.push(element)
|
||||
}
|
||||
for (const menu of menuList) {
|
||||
if (menu.children.length === 1) {
|
||||
menu.children = menu.children[0].children
|
||||
}
|
||||
}
|
||||
this.powerTree = data.menuList
|
||||
this.dialogLoading = false
|
||||
} else {
|
||||
@@ -215,11 +220,13 @@ export default {
|
||||
})
|
||||
},
|
||||
handleSelectedPowerChange(data, checked, indeterminate) {
|
||||
if (data.children === undefined) {
|
||||
if (checked || indeterminate) {
|
||||
this.roleForm.selectedPower.add(data.powerId)
|
||||
} else {
|
||||
this.roleForm.selectedPower.delete(data.powerId)
|
||||
}
|
||||
}
|
||||
},
|
||||
handleAddBtn() {
|
||||
this.isAddNew = true
|
||||
@@ -235,12 +242,6 @@ export default {
|
||||
this.defaultSelectedPower.push(operation.powerId)
|
||||
this.roleForm.selectedPower.add(operation.powerId)
|
||||
}
|
||||
for (const element of row.elements) {
|
||||
this.roleForm.selectedPower.add(element.powerId)
|
||||
}
|
||||
for (const menu of row.menus) {
|
||||
this.roleForm.selectedPower.add(menu.powerId)
|
||||
}
|
||||
this.isAddNew = false
|
||||
this.dialogVisible = true
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user