1. 程式人生 > 實用技巧 >許可權系統設計

許可權系統設計

整體表結構

部門表設計

drop table if exists department;
create table department(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`name` varchar(255) not null comment '部門名稱',
`parent_id` bigint comment '上級部門id',
`desc` varchar(512) comment '部門描述',
`level` int not null comment '當前層級'
)comment '部門表';

insert into department values(1,"技術部",null,"技術為王",1);
insert into department values(2,"銷售部",null,"銷售為天",1);
insert into department values(3,"人力資源部",null,"HR最美",1);
insert into department values(4,"前端組",1,"html+js+css",2);
insert into department values(5,"後端組",1,"java+python+php",2);
insert into department values(6,"運維組",1,"prometheus",2);
insert into department values(7,"Java組",5,"html+js+css",3);
insert into department values(8,"Python組",5,"java+python+php",3);
insert into department values(9,"Php組",5,"prometheus",3);

insert into department values(10,"銷售1組",2,"今夜打老虎",2);
insert into department values(11,"銷售2組",2,"無敵風火輪",2);
insert into department values(12,"銷售3組",2,"天王蓋地虎",2);

insert into department values(13,"薪酬組",3,"數錢數到手抽筋",2);
insert into department values(14,"招聘組",3,"拿著白菜錢操著白粉心",2);
insert into department values(15,"員工關係組",3,"為中華之崛起而奮鬥",2);
  • 部門表全表查詢後構成樹結構
	@Override
	public List<DepartmentVo> getDepartmentVoTree() {
		List<Department> list = departmentMapperExt.getAll();
		List<DepartmentVo> departmentVoList = BeanCoperUtil.copyList(list, DepartmentVo.class);
		List<DepartmentVo> topList = departmentVoList.stream().filter(e -> e.getParentId() == null)
				.collect(Collectors.toList());
		departmentVoList.removeAll(topList);
		AtomicLong level = new AtomicLong(2);
		List<DepartmentVo> parentList = topList;
		while (!departmentVoList.isEmpty()) {
			List<DepartmentVo> chilrenList = departmentVoList.stream().filter(e -> e.getLevel() == level.longValue())
					.collect(Collectors.toList());
			Map<Long, DepartmentVo> parentMap = parentList.stream()
					.collect(Collectors.toMap(DepartmentVo::getId, e -> e));
			for (DepartmentVo departmentVo : chilrenList) {
				Long parentId = departmentVo.getParentId();
				List<DepartmentVo> currentChilrenList = parentMap.get(parentId).getChildren();
				if (currentChilrenList == null) {
					currentChilrenList = new ArrayList<>();
					parentMap.get(parentId).setChildren(currentChilrenList);
				}
				currentChilrenList.add(departmentVo);
			}
			departmentVoList.removeAll(chilrenList);
			parentList = chilrenList;
			level.addAndGet(1);
		}
		return topList;
	}
  • 使用jsTree展示

整體表設計-精簡版

drop database rbac;
create database if not exists  rbac DEFAULT CHARACTER SET utf8 ;
use rbac;
drop table if exists department;
create table department(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`name` varchar(255) not null comment '部門名稱',
`parent_id` bigint comment '上級部門id',
`desc` varchar(512) comment '部門描述',
`level` int not null comment '當前層級'
)comment '部門表';

insert into department values(1,"技術部",null,"技術為王",1);
insert into department values(2,"銷售部",null,"銷售為天",1);
insert into department values(3,"人力資源部",null,"HR最美",1);
insert into department values(4,"前端組",1,"html+js+css",2);
insert into department values(5,"後端組",1,"java+python+php",2);
insert into department values(6,"運維組",1,"prometheus",2);
insert into department values(7,"Java組",5,"html+js+css",3);
insert into department values(8,"Python組",5,"java+python+php",3);
insert into department values(9,"Php組",5,"prometheus",3);

insert into department values(10,"銷售1組",2,"今夜打老虎",2);
insert into department values(11,"銷售2組",2,"無敵風火輪",2);
insert into department values(12,"銷售3組",2,"天王蓋地虎",2);

insert into department values(13,"薪酬組",3,"數錢數到手抽筋",2);
insert into department values(14,"招聘組",3,"拿著白菜錢操著白粉心",2);
insert into department values(15,"員工關係組",3,"為中華之崛起而奮鬥",2);

create table user(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`username` varchar(255) comment '使用者名稱',
`password` varchar(255) comment '密碼'
)comment '使用者表';

create table department_user(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`department_id` bigint(20) comment '部門ID',
`user_id` bigint(20) comment '使用者ID'
)comment '部門-使用者表';

create table role(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`code` varchar(255) comment '角色編碼',
`name` varchar(255) comment '角色名稱'
)comment '角色表';

create table user_role(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`user_id` bigint(20) comment '使用者ID',
`role_id` bigint(20) comment '角色ID'
)comment '使用者-角色表';

create table department_role(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`department_id` bigint(20) comment '部門ID',
`role_id` bigint(20) comment '角色ID'
)comment '部門-角色表';

create table permission(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`code` varchar(255) comment '許可權編碼',
`name` varchar(255) comment '許可權名稱',
`resource` varchar(255) comment '許可權關聯的資源'
)comment '許可權表';

create table user_permission(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`user_id` bigint(20) comment '使用者ID',
`permission_id` bigint(20) comment '許可權ID'
)comment '使用者-許可權表';

create table role_permission(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`role_id` bigint(20) comment '角色ID',
`permission_id` bigint(20) comment '許可權ID'
)comment '角色-許可權表';

create table department_permission(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`role_id` bigint(20) comment '使用者ID',
`department_id` bigint(20) comment '許可權ID'
)comment '部門-許可權表';

create table operation_log(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT primary key comment '主鍵',
`table_id` int comment '表id:department=1;user=2;department_user=3;role=4;user_role=5;depart_role=6;permission=7;department_permission=8;user_permission=9;role_permission=10',
`sql`varchar(1024) comment '執行的sql',
`op_type` int comment '操作型別:select=0;insert=1;update=2;delete=3'
)comment '操作日誌表';