mysql。PHP 后台权限管理 模块
背景:订单系统后台权限管理
功能要求:
- 可以设置人员角色
- 可以设置员工所属部门
- 员工对访问页面,按钮,请求数据查看。只能看到授权后的。
方案:
- 员工信息表,保存员工基本信息,用 session 保存,登出后清理 ssesion 。
- 部门表,保存好员工部门基础信息,并且建字段 parent_id ,支撑无限极部门管理,书写遍历部门树公用方法,部门表作为基础表,提拱主 id 到部门信息表中记录下员工部门信息。
- 角色表,正式控制访问权限的表,记录设置访问权限ID,用以判断权限。
- 角色和部门无唯一限制,员工信息可以跨部门。故员工表保留 department_id 为 text, 数据存为 json ; 角色表保留 role_id 为 text, 数据存为 json 。
- 员工会存在于其他系统中,那么有一个 员工 id, 存在于oa系统,各下游系统也会保留 该ID
表设计
用户表 dp_system_user
字段 | 类型 | 默认值 | 备注 |
id | int 11 | not null | 角色id |
| varchar 64 | not null | 员工名,写入为员工拥有公司邮箱 |
gid | int 11 | not null | 系统 ID |
account | varchar 32 | not null | 员工名 |
department_id | text 多个部门 65,657,8 | not null | 部门 ID |
is_delete | boolean | 0 | 是否可用 |
add_time | int 10 | not null | 添加时间 |
update_time | int 10 | not null | 更新时间 |
append_id | int 11 | not null | 添加人ID |
role_id | text | not null | 角色id |
部门表 dp_system_department
字段 | 类型 | 默认值 | 备注 |
id | int 11 | not null | 部门 id |
department_name | varchar 64 | not null | 部门名称 |
parent_id | int 11 | 0 | 上级部门 id |
add_time | int 10 | not null | 添加时间 |
append_id | int 11 | not null | 添加人ID |
is_delete | tinyint | not null | 是否删除 |
角色表 dp_system_role
字段 | 类型 | 默认值 | 备注 |
id | int | not null | 角色id |
role_name | varchar 100 | not null | 角色名 |
add_time | int 10 | not null | 添加时间 |
append_id | int 10 | not null | 添加人ID |
is_delete | tinyint | not null | 是否删除 |
常见模型 rbac (rolu based access control ) 基于角色访问控制,因为角色多起来管理不便,所以引用了权限组,部门的概念。部门主要职责就是批量授权
然而开始设置权限就需要一个表来存允许访问的 模块信息与用户信息
模块表 dp_system_template
字段 | 类型 | 默认值 | 备注 | 例子 |
id | int 11 | not null | 模块id | 1 |
template_code | varchar 64 | not null | 模版code | order.order_list |
template_name | varchar 64 | not null | 模版 名字 | 订单查询列表 |
add_user | int 11 | not null | 添加人 | 45 |
last_time | int 10 | not null | 修改时间 | 154234657 |
is_delete | boolean | not null | 是否可用 | 0 |
允许访问信息表dp_system_permission
字段 | 类型 | 默认值 | 备注 |
id | int 11 | not null | id |
role_id | int 11 | not null | 权限ID |
template_id | int 11 | not null | 模版id |
模块可能无限增长,方便查询效率高,模块ID与 权限ID 多对多存储。
后来mysql主表日增数据 100,000。经历三个月,数据量到达 9,000,000。业务员2000,每次访问数据,先筛选可以访问的权限店铺,然后 in 范围查询,造成查询压力旁大,解决方案:
案例sql
select * from om_unshipped_order where id in (29765473,,,,,省略4000id)and accountId in(567,,,省略200id)limit 50。
1,备份数据库,一个月备份一次,备份出完结订单涉及的表数据,减少数据库压力。
2,可以做成查询延后,首次告诉已经接受请求请稍后,ajax轮训,监控结果,后台PHP分页运行计算结果后,放入一个文件中,文件内容只含有orderid,一行一个,便于页面读取分页读取数据,二次查询数据库查询出结果,文件名用sessionid,中途查询关系存放到redis,保证计算出正确的文件名,方便获取文件。
3,另外一个思路,整合数据到mysql表,用于检索需要得数据,或者放入elasticsearch,计算出我们的结果。
4,一个思路,如果是spu,这样的查询,通过规则整合成int 整型,查询就仅仅需要取出数字区间,非常快