用户权限设计(一)-数据库设计

   在WEB开发中,难免要解决权限管理的问题,在这里结合项目和之前对权限的理解,记录下权限系统构建的过程。

   主要运用的技术:Spring MVC + Mybatis+Bootstrap + Apache Shiro + MySQL

   权限系统设计思想:权限主要是指明哪些用户可以对系统的哪些功能有什么样的权限,而一个系统的功能从程序开发的角度来说就是对数据的CURD(增、删、改、查)。我们进一步把用户就行分组处理,因为可能不同的用户都是拥有相同的权限的,这样我们只要把这些用户归属于用户组,再对用户组进行授权,这样就有效的控制了用户。从权限的作用来看,我们需要的实体有:用户,权限,角色。一般来说一个用户可能属于多个角色,就像那些当官的挂着一堆职称一样,一个角色能包含多个用户;类似的角色和权限具有类似的关系。从描述中我们再细化下,给出以下E-R图(LZ自己画的,可能不符合E-R设计标准,主要方便看):


                                 用户权限设计 E-R 图

上面的图是LZ 用PowerDesigner画出来的,感兴趣的可以搜一下。我在这里用户权重这个概念,角色不直接拥有权限,让权重直接对权限,这样对权限进行了细分,可以让同一个角色在基础权限上在进行划分。

PowerDesigner生成MySQL 脚本如下:

/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2017/4/13 19:22:14                           */
/*==============================================================*/


drop table if exists sys_permissions;

drop table if exists sys_role;

drop table if exists sys_role_per;

drop table if exists sys_user;

drop table if exists sys_user_role;

drop table if exists sys_weight;

drop table if exists sys_weight_per;

/*==============================================================*/
/* Table: sys_permissions   系统权限表                                    */
/*==============================================================*/
create table sys_permissions
(
   ID                   bigint not null,
   PER_NAME             varchar(50) not null,
   PER_DESC             varchar(100) not null,
   PER_LEVEL            int,
   PER_PARENT_ID        bigint,
   PER_URL              varchar(100),
   PER_AUTHORIZATION    varchar(50) not null,
   PER_RANKING          int,
   PER_STATUS           tinyint not null,
   ICON_URL             varchar(100),
   CREATIME             datetime,
   UPDATETIME           datetime,
   primary key (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

alter table sys_permissions comment '权限表';

/*==============================================================*/
/* Table: sys_role                系统角色表                              */
/*==============================================================*/
create table sys_role
(
   ID                   bigint not null COMMENT 'ID',
   NAME                 varchar(100) COMMENT '角色名称',
   CODE                 varchar(50) COMMENT '编号',
   DESCRIPTION          varchar(200) COMMENT '角色描述',
   WEIGHT               bigint COMMENT '权重表中权重号',
   primary key (ID)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/*==============================================================*/
/* Table: sys_role_per        角色权限表                                  */
/*==============================================================*/
create table sys_role_per
(
   ROLE_ID              bigint COMMENT '角色表ID',
   PER_ID               bigint COMMENT '权限表ID',
   ID                   bigint not null COMMENT 'ID',
   primary key (ID)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/*==============================================================*/
/* Table: sys_user               用户表                               */
/*==============================================================*/
create table sys_user
(
   ID                   bigint not null COMMENT 'ID',
   USER_ID              varchar(120) COMMENT '用户ID',
   PWD                  varchar(50) COMMENT '密码',
   ICON                 varchar(300) COMMENT '图标',
   NICK_NAME            varchar(120) COMMENT '昵称',
   SEX                  varchar(2) COMMENT '性别',
   CREATE_TIME          datetime COMMENT '创建时间',
   UPDATE_TIME          datetime COMMENT '更新时间'
   primary key (ID)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

alter table sys_user comment '用户信息表';

/*==============================================================*/
/* Table: sys_user_role           用户角色表                              */
/*==============================================================*/
create table sys_user_role
(
   ID                   bigint not null COMMENT 'ID' ,
   USER_ID              bigint not null COMMENT '用户表ID',
   ROLE_ID              bigint not null COMMENT '角色ID',
   primary key (ID)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/*==============================================================*/
/* Table: sys_weight                   权重表                         */
/*==============================================================*/
create table sys_weight
(
   ID                   bigint not null COMMENT 'ID' ,
   WEIGHT_NAME          varchar(50) not null COMMENT '权重名称',
   WEIGHT_DES           varchar(200) COMMENT '权重描述',
   primary key (ID)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/*==============================================================*/
/* Table: sys_weight_per             权重权限表                           */
/*==============================================================*/
create table sys_weight_per
(
   ID                   bigint not null COMMENT 'ID',
   WEIGHT_ID            bigint not null COMMENT '权重ID',
   PER_ID               bigint not null COMMENT '权限ID',
   primary key (ID)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

alter table sys_role add constraint FK_ROLE_WEIGHT foreign key (WEIGHT)
      references sys_weight (ID) on delete restrict on update restrict;

alter table sys_role_per add constraint FK_ROLE_ID foreign key (ROLE_ID)
      references sys_role (ID) on delete restrict on update restrict;

alter table sys_role_per add constraint FK_PER_ID foreign key (PER_ID)
      references sys_permissions (ID) on delete restrict on update restrict;

alter table sys_user_role add constraint FK_USER_ROLE_USER_ID foreign key (USER_ID)
      references sys_user (ID) on delete restrict on update restrict;

alter table sys_user_role add constraint FK_USER_ROLE_ROLE_ID foreign key (ROLE_ID)
      references sys_role (ID) on delete restrict on update restrict;

alter table sys_weight_per add constraint FK_WEIGHT_PER_PER_ID foreign key (PER_ID)
      references sys_permissions (ID) on delete restrict on update restrict;

alter table sys_weight_per add constraint FK_WEIGHT_PER_WEIGHT_ID foreign key (WEIGHT_ID)
      references sys_weight (ID) on delete restrict on update restrict;


接下来就是把脚本在数据库跑下,这样基石(数据库表)就算搭建好了。


  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值