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;
|
package com.cfive.pinnacle.service.permission.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
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.Role;
|
||||||
import com.cfive.pinnacle.entity.permission.PowerRole;
|
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.RoleMapper;
|
||||||
import com.cfive.pinnacle.mapper.permission.PowerRoleMapper;
|
import com.cfive.pinnacle.mapper.permission.PowerRoleMapper;
|
||||||
import com.cfive.pinnacle.service.permission.IRoleService;
|
import com.cfive.pinnacle.service.permission.IRoleService;
|
||||||
@@ -13,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -51,6 +54,8 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
|
|||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean addRole(Role role) {
|
public boolean addRole(Role role) {
|
||||||
|
handlePower(role);
|
||||||
|
|
||||||
if (roleMapper.insert(role) == 1) {
|
if (roleMapper.insert(role) == 1) {
|
||||||
role.getPowers().forEach(power -> {
|
role.getPowers().forEach(power -> {
|
||||||
PowerRole powerRole = new PowerRole();
|
PowerRole powerRole = new PowerRole();
|
||||||
@@ -69,6 +74,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
|
|||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean modifyRole(Role role) {
|
public boolean modifyRole(Role role) {
|
||||||
|
handlePower(role);
|
||||||
roleMapper.updateById(role);
|
roleMapper.updateById(role);
|
||||||
Role originalRole = getRole(role.getId());
|
Role originalRole = getRole(role.getId());
|
||||||
HashSet<Long> newPowerIds = new HashSet<>();
|
HashSet<Long> newPowerIds = new HashSet<>();
|
||||||
@@ -97,4 +103,20 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
|
|||||||
});
|
});
|
||||||
return true;
|
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)
|
insert into t_power(id, type_id)
|
||||||
values (3020101, 3);
|
values (3020101, 3);
|
||||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
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;
|
commit;
|
||||||
|
|
||||||
begin;
|
begin;
|
||||||
insert into t_power(id, type_id)
|
insert into t_power(id, type_id)
|
||||||
values (3020102, 3);
|
values (3020102, 3);
|
||||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
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;
|
commit;
|
||||||
|
|
||||||
begin;
|
begin;
|
||||||
insert into t_power(id, type_id)
|
insert into t_power(id, type_id)
|
||||||
values (3020103, 3);
|
values (3020103, 3);
|
||||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
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;
|
commit;
|
||||||
|
|
||||||
begin;
|
begin;
|
||||||
insert into t_power(id, type_id)
|
insert into t_power(id, type_id)
|
||||||
values (3020104, 3);
|
values (3020104, 3);
|
||||||
insert into t_operation(id, name, code, power_id, element_id, parent_id)
|
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;
|
commit;
|
||||||
|
|
||||||
begin;
|
begin;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<icon-pinnacle-plus />
|
<icon-pinnacle-plus />
|
||||||
</el-icon>
|
</el-icon>
|
||||||
</template>
|
</template>
|
||||||
<template #default> 添加 </template>
|
<template #default> 添加</template>
|
||||||
</el-button>
|
</el-button>
|
||||||
<common-table
|
<common-table
|
||||||
:table-date="roleTable"
|
:table-date="roleTable"
|
||||||
@@ -46,6 +46,16 @@
|
|||||||
show-word-limit
|
show-word-limit
|
||||||
/>
|
/>
|
||||||
</el-form-item>
|
</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-form-item label="角色权限">
|
||||||
<el-tree
|
<el-tree
|
||||||
:data="powerTree"
|
:data="powerTree"
|
||||||
@@ -58,23 +68,13 @@
|
|||||||
@check-change="handleSelectedPowerChange"
|
@check-change="handleSelectedPowerChange"
|
||||||
/>
|
/>
|
||||||
</el-form-item>
|
</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-form>
|
||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
</template>
|
</template>
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<el-button type="primary" @click="handleSubmit" :disabled="dialogLoading"
|
<el-button type="primary" @click="handleSubmit" :disabled="dialogLoading"
|
||||||
>提交</el-button
|
>提交
|
||||||
>
|
</el-button>
|
||||||
<el-button @click="handleCancel" :disabled="dialogLoading">取消</el-button>
|
<el-button @click="handleCancel" :disabled="dialogLoading">取消</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
@@ -204,6 +204,11 @@ export default {
|
|||||||
}
|
}
|
||||||
menu.children.push(element)
|
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.powerTree = data.menuList
|
||||||
this.dialogLoading = false
|
this.dialogLoading = false
|
||||||
} else {
|
} else {
|
||||||
@@ -215,10 +220,12 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
handleSelectedPowerChange(data, checked, indeterminate) {
|
handleSelectedPowerChange(data, checked, indeterminate) {
|
||||||
if (checked || indeterminate) {
|
if (data.children === undefined) {
|
||||||
this.roleForm.selectedPower.add(data.powerId)
|
if (checked || indeterminate) {
|
||||||
} else {
|
this.roleForm.selectedPower.add(data.powerId)
|
||||||
this.roleForm.selectedPower.delete(data.powerId)
|
} else {
|
||||||
|
this.roleForm.selectedPower.delete(data.powerId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleAddBtn() {
|
handleAddBtn() {
|
||||||
@@ -235,12 +242,6 @@ export default {
|
|||||||
this.defaultSelectedPower.push(operation.powerId)
|
this.defaultSelectedPower.push(operation.powerId)
|
||||||
this.roleForm.selectedPower.add(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.isAddNew = false
|
||||||
this.dialogVisible = true
|
this.dialogVisible = true
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user