php RBAC权限控制系统mysql表设计

实现效果

1、菜单管理

2、角色管理

3、用户管理

Mysql表设计:

  1. s_menu 菜单表
    +------------+------------------+------+-----+---------+----------------+
    | Field      | Type             | Null | Key | Default | Extra          |
    +------------+------------------+------+-----+---------+----------------+
    | id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | pid        | int(11)          | YES  |     | 0       |                |父级id
    | crd        | int(11)          | YES  |     | 0       |                |排序
    | title      | varchar(20)      | YES  |     | NULL    |                |名称
    | controller | varchar(30)      | YES  |     | NULL    |                |控制器
    | method     | varchar(30)      | YES  |     | NULL    |                |方法
    | ishidden   | tinyint(1)       | YES  |     | 0       |                |0:显示  1:隐藏
    | status     | tinyint(1)       | YES  |     | 0       |                |0:正常  1:禁用
    | type       | tinyint(1)       | YES  |     | 0       |                |0:系统1 1:系统2(如果单个系统则不需要此字段)
    +------------+------------------+------+-----+---------+----------------+
  2. s_group 角色表
    +--------+------------------+------+-----+---------+----------------+
    | Field  | Type             | Null | Key | Default | Extra          |
    +--------+------------------+------+-----+---------+----------------+
    | id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | title  | varchar(30)      | YES  |     | NULL    |                |角色标题
    | rights | text             | YES  |     | NULL    |                |权限组,存s_menu表id的json数组
    +--------+------------------+------+-----+---------+----------------+
  3. s_user 用户表
    +----------+------------------+------+-----+---------+----------------+
    | Field    | Type             | Null | Key | Default | Extra          |
    +----------+------------------+------+-----+---------+----------------+
    | id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | user     | varchar(20)      | YES  |     | NULL    |                |用户名
    | pass     | varchar(100)     | YES  |     | NULL    |                |密码
    | truename | varchar(20)      | YES  |     | NULL    |                |真实姓名
    | gid      | int(11) unsigned | YES  |     | NULL    |                |角色表 id
    | gid_view | int(11) unsigned | YES  |     | NULL    |                |角色表2 id(意思是一个用户拥有两套系统的角色权限如果单个系统则不需要此字段)
    | status   | tinyint(4)       | NO   |     | 0       |                |用户状态 0:正常  1:禁用
    | add_time | int(11)          | YES  |     | NULL    |                |添加时间
    +----------+------------------+------+-----+---------+----------------+

原理:

  1. s_menu表间保存类文件的控制器、方法、菜单名称,子级菜单的pid为父级菜单的id进行存储,一级菜单pid默认为0,便于自定义菜单的遍历渲染
    +----+------+------+---------------+------------+--------+----------+--------+------+
    | id | pid  | crd  | title         | controller | method | ishidden | status | type |
    +----+------+------+---------------+------------+--------+----------+--------+------+
    |  1 |    0 |    7 | 安全          |            |        |        0 |      0 |    0 |
    |  2 |    1 |    0 | 用户管理      | User       | index  |        0 |      0 |    0 |
    |  3 |    1 |    1 | 角色管理      | Role       | index  |        0 |      0 |    0 |
    |  4 |    1 |    2 | 表单-菜单管理 | Menu       | index  |        0 |      0 |    0 |
    |  5 |    2 |    0 | 用户添加      | User       | add    |        1 |      0 |    0 |
    |  6 |    2 |    0 | 用户保存      | User       | save   |        1 |      0 |    0 |
    |  7 |    2 |    0 | 用户编辑      | User       | edit   |        1 |      0 |    0 |
    |  8 |    2 |    0 | 用户修改      | User       | update |        1 |      0 |    0 |
    |  9 |    2 |    0 | 用户删除      | User       | delete |        1 |      0 |    0 |
    | 10 |    3 |    0 | 角色添加      | Role       | add    |        1 |      0 |    0 |
    +----+------+------+---------------+------------+--------+----------+--------+------+

    如:安全下面有【用户管理】、【角色管理】、【表彰-菜单管理】3个子菜单

  2. s_group表将多个s_menu_id形成json数组绑定角色名称
    +----+----------+-----------------------------------------------------------------------------+
    | id | title    | rights                                                                      |
    +----+----------+-----------------------------------------------------------------------------+
    | 12 | 视觉设计 | [46,51,59,60,67,92,126,127,272,273,213,214,215,263,268,271,275,317,318,264] |
    | 14 | 财务     | [92,126,127,212,113,115,226,216,217,263,268,264]                            |
    +----+----------+-----------------------------------------------------------------------------+
  3. s_user绑定有角色组的id,当php登录后获取用户表id,就可查询到是否有当前访问的控制器的方法权限
    +----+--------+----------------------------------+----------+------+----------+--------+------------+
    | id | user   | pass                             | truename | gid  | gid_view | status | add_time   |
    +----+--------+----------------------------------+----------+------+----------+--------+------------+
    |  1 | xxx    | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  | xxx      |    1 |        2 |      0 | 1236456622 |
    | 12 | xxx    | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  | xxx      |    7 |        4 |      0 | 1546313958 |
    | 13 | xxx    | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  | xxx      |    9 |        4 |      0 | 1546313989 |
    | 14 | xxx    | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  | xxx      |   10 |        3 |      0 | 1546314013 |
    | 15 | xxx    | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  | xxx      |   16 |        3 |      0 | 1546314060 |
    +----+--------+----------------------------------+----------+------+----------+--------+------------+

猜你喜欢

转载自www.cnblogs.com/yachyu/p/10709798.html
今日推荐