RBAC权限模型

本文详细介绍了基于角色的访问控制(RBAC)在MySQL中的应用,涉及用户、角色和权限表的设计,以及如何通过JSON数据格式存储和查询角色及权限。通过实例展示了如何为用户分配角色,从而实现权限的高效管理。
摘要由CSDN通过智能技术生成

目录

1.基本概念

 2.实现


1.基本概念

全写(Role-Based Access Control)基于角色的访问控制

操作系统的各种权限不直接赋予具体用户,而是在用户集合和权限集合中间创立一个角色集合,每一种角色对应一组响应的权限,一旦用户被分配适当的角色后,该用户就拥有此角色的所有权限功能,减少系统开销

 

 2.实现

创建三个表

用户表

-- auto-generated definition
create table tb_user
(
    id          int unsigned auto_increment comment '主键'
        primary key,
    open_id     varchar(200)    null comment '长期授权字符串',
    nickname    varchar(200)    null comment '昵称',
    photo       varchar(200)    null comment '头像网址',
    name        varchar(20)     null comment '姓名',
    sex         enum ('男', '女') null comment '性别',
    tel         char(11)        null comment '手机号码',
    email       varchar(200)    null comment '邮箱',
    hiredate    date            null comment '入职日期',
    role        json            not null comment '角色',
    root        tinyint(1)      not null comment '是否是超级管理员',
    dept_id     int unsigned    null comment '部门编号',
    status      tinyint         not null comment '状态',
    create_time datetime        not null on update CURRENT_TIMESTAMP comment '创建时间',
    constraint unq_open_id
        unique (open_id)
)
    comment '用户表' charset = utf8;

create index idx_dept_id
    on tb_user (dept_id);

create index idx_status
    on tb_user (status);

create index unq_email
    on tb_user (email);

行为表

create table tb_permission
(
    id              int unsigned auto_increment comment '主键'
        primary key,
    permission_name varchar(200) not null comment '权限',
    module_id       int unsigned not null comment '模块ID',
    action_id       int unsigned not null comment '行为ID',
    constraint unq_complex
        unique (module_id, action_id),
    constraint unq_permission
        unique (permission_name)
)
    charset = utf8;

角色表

create table tb_role
(
    id          int unsigned auto_increment comment '主键'
        primary key,
    role_name   varchar(200) not null comment '角色名称',
    permissions json         not null comment '权限集合',
    constraint unq_role_name
        unique (role_name)
)
    comment '角色表' charset = utf8;

 Mysql5.7之后引入JSON数据格式,字段permission。支持索引

可以保存两种数据1.json数据2.数组

进行查询

#  举例 创建json数组
SELECT JSON_ARRAY (10,20,30);
# 查询包含100
SELECT JSON_CONTAINS(JSON_ARRAY(10,20,30), "100");

select distinct p.permission_name
from tb_user u
join tb_role r on JSON_CONTAINS(u.role, CAST(r.id As CHAR ))
join tb_permission p on JSON_CONTAINS(r.permissions,CAST(p.id as char ))
where u.id=1 and u.status=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值