本文讨论一种常见的权限控制方式:
权限管理使用分层思想:一共分四层
1.菜单/页面/按钮授权 |
系统用户对菜单/页面/按钮的可见性 |
2.接口(功能)授权 |
系统用户对接口的是否有访问权限 |
3.数据授权 |
系统用户对接口有访问权限,但是只能操作被授权的数据.
授权功能: 1.新建(能新建肯定可以查和改) 2.能看不能改 3.能看能改不能新建 4.根本不能看
角色自身需要功能: 1.新建(能新建肯定可以查和改)
某一个角色对另一个角色的数据有诉求: 2.能看不能改 3.能看能改不能新建 例如:role1角色能看role2角色的数据,但是不能修改其数据。
实现方式: 1.静态业务数据配置表,例如商户,商品等,每个表添加两个字段(如tbl_sapo_product表): create_role_id last_update_role_id
示例: -- 以下操作前提为相关接口均已授权 1.role1登录进来,新建记录,create_role_id = last_update_role_id = role1 2.role1登录进来,role1对create_role_id = role1的数据,有增改查权限; 3.role1对于create_role_id=role2的数据如有相关操作类型授权,可以进行相关类型的数据操作([能看不能改]或[能看能改]两个类型),有更新操作则更新last_update_role_id=role1; (即:tbl_sapo_admin_role_rel中配置了father_role_id=role2,child_role_id=role1,func_id=【更新功能接口】)
2.对于动态业务数据表,例如订单等,只有查/改需求
方案一(基于静态表权限): 动态数据表一定引用了静态数据表,例如:订单表中有商品id.价格id等 这些静态数据的归属和授权决定了这些动态数据的归属和授权. 即你是该商品的创建者或者有该数据权限,你可以查看该商品下的订单. 以查询订单为例: 1.首先role1对[订单查询]接口有调用权限 2.订单中的商品id对应的商品create_role_id=role1 或者create_role_id=role2, 但是role1继承role2数据和[订单查询]操作权限 3.role1可以顺利查询. (每个接口使用哪些静态表id由接口自己判定)
方案二(动态和静态表授权解耦): 你有权维护(增改查)这个商店和产品,不见得你就能查看这个商店下所有产品的订单记录 例如:role1有权限增改查一类门店和门店下的商品,但是他只能查看某个门店的某几个商品的订单信息.
以查询订单为例: 1.首先role1对[订单查询]接口有调用权限. 2.查询role1和[订单查询]操作授权了哪些商品id的集合. 3.订单中的商品id在授权集合中则顺利查询.
方案二: 优点:是处理静态配置表和动态业务表的接口授权完全解耦,更加灵活。
|
4.字段授权 |
只能看见被授权的字段 |
;
;
;
;
;
;
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_account` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`login_name` varchar(255) NOT NULL COMMENT "登录名",
`login_password` varchar(255) NOT NULL COMMENT "登录密码",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-初始化,1-已邀请,2-已注册",
`remark` varchar(255) DEFAULT NULL COMMENT "备注",
`admin_user_id` int(10) unsigned DEFAULT NULL COMMENT "管理人员id,tbl_sapo_admin_user表id",
PRIMARY KEY (`id`),
UNIQUE KEY `uni_idx_admin_account_login_name` (`login_name`),
KEY `idx_admin_account_admin_user_id` (`admin_user_id`),
CONSTRAINT `fk_admin_account_admin_user_id` FOREIGN KEY (`admin_user_id`) REFERENCES `tbl_sapo_admin_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="系统管理账户";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_api` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`api_code` varchar(255) NOT NULL COMMENT "api代码",
`api_path` varchar(1024) NOT NULL COMMENT "api路径",
`api_desc` varchar(255) NOT NULL COMMENT "api描述",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
`handle_type` int(10) unsigned NOT NULL DEFAULT 0 COMMENT "操作类型:0-静态数据,1-动态数据",
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="系统接口";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_func_api` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`func_id` int(10) unsigned NOT NULL COMMENT "系统功能id,tbl_sapo_admin_sys_func表id",
`api_id` int(10) unsigned NOT NULL COMMENT "接口id,tbl_sapo_admin_api表id",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
PRIMARY KEY (`id`),
KEY `idx_admin_func_api_func_id` (`func_id`),
KEY `idx_admin_func_api_api_id` (`api_id`),
CONSTRAINT `fk_admin_func_api_api_id` FOREIGN KEY (`api_id`) REFERENCES `tbl_sapo_admin_api` (`id`),
CONSTRAINT `fk_admin_func_api_func_id` FOREIGN KEY (`func_id`) REFERENCES `tbl_sapo_admin_sys_func` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="功能api";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_handle_log` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`handle_type` int(10) unsigned NOT NULL DEFAULT 0 COMMENT "操作类型:0-登录",
`result` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "结果:0-失败,1-成功",
`admin_account_id` int(10) unsigned NOT NULL,
`admin_user_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_admin_handle_log_admin_account_id` (`admin_account_id`),
KEY `idx_admin_handle_log_admin_user_id` (`admin_user_id`),
CONSTRAINT `fk_admin_handle_log_admin_account_id` FOREIGN KEY (`admin_account_id`) REFERENCES `tbl_sapo_admin_account` (`id`),
CONSTRAINT `fk_admin_handle_log_admin_user_id` FOREIGN KEY (`admin_user_id`) REFERENCES `tbl_sapo_admin_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="管理员操作日志";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_menu` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`parent_menu_id` int(10) unsigned DEFAULT NULL COMMENT "父菜单id,tbl_sapo_admin_menu表id",
`name` varchar(255) DEFAULT NULL COMMENT "菜单名",
`link` varchar(512) DEFAULT NULL COMMENT "菜单路径",
`icon` varchar(512) DEFAULT NULL COMMENT "菜单图标",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
PRIMARY KEY (`id`),
KEY `idx_admin_menu_parent_menu_id` (`parent_menu_id`),
CONSTRAINT `fk_admin_menu_parent_menu_id` FOREIGN KEY (`parent_menu_id`) REFERENCES `tbl_sapo_admin_menu` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="菜单";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_role` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`role_code` varchar(255) NOT NULL COMMENT "角色编号",
`role_name` varchar(255) NOT NULL COMMENT "角色名称",
`role_desc` varchar(255) DEFAULT NULL COMMENT "角色名称",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
`super_flag` int(10) unsigned NOT NULL DEFAULT 0 COMMENT "超级管理员标志:0-非超级,1-超级管理员",
PRIMARY KEY (`id`),
UNIQUE KEY `uni_idx_admin_role_role_code` (`role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="角色";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_role_menu` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`role_id` int(10) unsigned NOT NULL COMMENT "系统角色id,tbl_sapa_admin_role表id",
`menu_id` int(10) unsigned NOT NULL COMMENT "系统菜单id,tbl_sapo_admin_menu表id",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
PRIMARY KEY (`id`),
KEY `idx_admin_role_menu_menu_id` (`menu_id`),
KEY `idx_admin_role_menu_role_id` (`role_id`),
CONSTRAINT `fk_admin_role_menu_menu_id` FOREIGN KEY (`menu_id`) REFERENCES `tbl_sapo_admin_menu` (`id`),
CONSTRAINT `fk_admin_role_menu_role_id` FOREIGN KEY (`role_id`) REFERENCES `tbl_sapo_admin_role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="角色菜单";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_role_rel` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
`father_role_id` int(10) unsigned NOT NULL COMMENT "父角色id,tbl_sapo_admin_role表id",
`child_role_id` int(10) unsigned NOT NULL COMMENT "子角色id,tbl_sapo_admin_role表id",
`data_auth_type_id` int(10) unsigned NOT NULL COMMENT "授权类型id,tbl_sapo_admin_data_auth表id",
`func_id` int(10) unsigned NOT NULL COMMENT "系统功能id,tbl_sapo_admin_sys_func表id",
PRIMARY KEY (`id`),
KEY `idx_admin_role_rel_father_role_id` (`father_role_id`),
KEY `idx_admin_role_rel_child_role_id` (`child_role_id`),
KEY `idx_admin_role_rel_func_id` (`func_id`),
CONSTRAINT `fk_admin_role_rel_child_role_id` FOREIGN KEY (`child_role_id`) REFERENCES `tbl_sapo_admin_role` (`id`),
CONSTRAINT `fk_admin_role_rel_father_role_id` FOREIGN KEY (`father_role_id`) REFERENCES `tbl_sapo_admin_role` (`id`),
CONSTRAINT `fk_admin_role_rel_func_id` FOREIGN KEY (`func_id`) REFERENCES `tbl_sapo_admin_sys_func` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="角色关系(子角色能继承父角色的数据)";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_role_sys_func` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
`role_id` int(10) unsigned DEFAULT NULL COMMENT "角色id,tbl_sapo_admin_role表id",
`func_id` int(10) unsigned DEFAULT NULL COMMENT "功能id,tbl_sapo_admin_sys_func表id",
PRIMARY KEY (`id`),
KEY `idx_admin_role_sys_func_role_id` (`role_id`),
KEY `idx_admin_role_sys_func_func_id` (`func_id`),
CONSTRAINT `fk_admin_role_sys_func_func_id` FOREIGN KEY (`func_id`) REFERENCES `tbl_sapo_admin_sys_func` (`id`),
CONSTRAINT `fk_admin_role_sys_func_role_id` FOREIGN KEY (`role_id`) REFERENCES `tbl_sapo_admin_role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="角色系统功能";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_sys_func` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
`func_code` varchar(255) DEFAULT NULL COMMENT "系统功能代码",
`func_desc` varchar(255) DEFAULT NULL COMMENT "系统功能描述",
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="系统功能";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`nick_name` varchar(255) DEFAULT NULL COMMENT "昵称",
`name` varchar(255) NOT NULL COMMENT "姓名",
`gender` int(10) unsigned NOT NULL DEFAULT 2 COMMENT "性别:0-女1-男,2-未知",
`phone_no` varchar(64) NOT NULL COMMENT "手机号",
`mail` varchar(255) NOT NULL COMMENT "邮箱",
`status` int(10) unsigned NOT NULL DEFAULT 0 COMMENT "状态:0-在岗,1-停岗,2-离岗",
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="管理人员";
CREATE TABLE IF NOT EXISTS `tbl_sapo_admin_user_role` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "主键",
`create_time` datetime(3) NOT NULL COMMENT "创建时间",
`last_update_time` datetime(3) DEFAULT NULL COMMENT "最后更新时间",
`account_id` int(10) unsigned NOT NULL COMMENT "账号id,tbl_sapo_admin_account表id",
`role_id` int(10) unsigned NOT NULL COMMENT "管理人员角色表id,tbl_sapo_admin_role表id",
`status` int(10) unsigned NOT NULL DEFAULT 1 COMMENT "状态:0-无效,1-有效",
PRIMARY KEY (`id`),
KEY `idx_admin_user_role_role_id` (`role_id`),
KEY `idx_admin_user_role_account_id` (`account_id`),
CONSTRAINT `fk_admin_user_role_account_id` FOREIGN KEY (`account_id`) REFERENCES `tbl_sapo_admin_account` (`id`),
CONSTRAINT `fk_admin_user_role_role_id` FOREIGN KEY (`role_id`) REFERENCES `tbl_sapo_admin_role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="账户角色";
;
;
;
;
View Code
本文来自云海天,作者:wanglifeng,转载请注明原文链接:https://www.cnblogs.com/wanglifeng717/p/16200942.html
原文地址:https://www.cnblogs.com/wanglifeng717/archive/2022/04/27/16200942.html