rbac主要是用来存放权限的,全称叫作基于角色权限控制mysql
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色id',
`name` varchar(255) DEFAULT NULL COMMENT '标题',
`status` int(1) DEFAULT '1' COMMENT '0:不可用,1:可用',
`created_at` datetime DEFAULT NULL COMMENT '建立时间',
`updated_at` datetime DEFAULT NULL COMMENT '更改时间',
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色';
复制代码
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` varchar(255) DEFAULT NULL COMMENT '用户名',
`password` varchar(255) DEFAULT NULL COMMENT '用户密码',
`created_at` datetime DEFAULT NULL COMMENT '建立时间',
`updated_at` datetime DEFAULT NULL COMMENT '更改时间',
`role_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
复制代码
CREATE TABLE `permission` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '权限id',
`title` varchar(255) DEFAULT NULL COMMENT '标题',
`url` varchar(255) DEFAULT NULL COMMENT '链接地址',
`status` int(1) DEFAULT '1' COMMENT '0:不可用,1:可用',
`created_at` datetime DEFAULT NULL COMMENT '建立时间',
`updated_at` datetime DEFAULT NULL COMMENT '更改时间',
`permission_id` int(11) DEFAULT NULL COMMENT '当前表id',
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='权限';
复制代码
CREATE TABLE `role_permission` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色权限id',
`role_id` int(11) DEFAULT NULL COMMENT '角色id',
`permission_id` int(11) DEFAULT NULL COMMENT '权限id',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB ENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='角色权限多对多';
复制代码
egg-init egg-rbac --type=simple
cd egg-example
cnpm install
复制代码
cnpm install egg-sequelize mysql2 egg-view-ejs --save
复制代码
config\plugin.jssql
module.exports = {
ejs: {
enable: true,
package: 'egg-view-ejs',
},
sequelize: {
enable: true,
package: 'egg-sequelize',
},
};
复制代码
config\config.default.js数据库
config.sequelize = {
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
dialectOptions: {
charset: 'utf8mb4',
},
database: 'eggrbac',
host: 'localhost',
port: '3306',
username: 'root',
password: '123456',
timezone: '+08:00',
};
复制代码
由于查找role的时候,想要知道这个role下面有多少个user,因此npm
app.model.Role.hasMany(app.model.User, { as: 'user' });
复制代码
app\model\role.jsbash
'use strict';
module.exports = app => {
const { INTEGER, STRING, DATE } = app.Sequelize;
const Role = app.model.define('role', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: STRING,
},
status: {
type: INTEGER,
},
created_at: DATE,
updated_at: DATE,
}, {
freezeTableName: true,
});
Role.associate = function() {
app.model.Role.hasMany(app.model.User, { as: 'user' });
};
return Role;
};
复制代码
由于查找user的时候,想要知道这个user归属于哪个role,因此app
app.model.User.belongsTo(app.model.Role, { as: 'role' });
复制代码
app\model\user.jsasync
'use strict';
module.exports = app => {
const { INTEGER, STRING, DATE } = app.Sequelize;
const User = app.model.define('user', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
username: {
type: STRING,
},
password: {
type: STRING,
},
created_at: DATE,
updated_at: DATE,
role_id: {
type: INTEGER,
},
}, {
freezeTableName: true,
});
User.associate = function() {
app.model.User.belongsTo(app.model.Role, { as: 'role' });
};
return User;
};
复制代码
自关联查询,因此post
app.model.Permission.belongsTo(app.model.Permission, { as: 'permissions', foreignKey: 'permission_id' });
复制代码
app\model\permission.js测试
'use strict';
module.exports = app => {
const { INTEGER, STRING, DATE } = app.Sequelize;
const Permission = app.model.define('permission', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING,
},
url: {
type: STRING,
},
status: {
type: INTEGER,
},
created_at: DATE,
updated_at: DATE,
permission_id: {
type: INTEGER,
},
}, {
freezeTableName: true,
});
Permission.associate = function() {
app.model.Permission.belongsTo(app.model.Permission, { as: 'permissions', foreignKey: 'permission_id' });
};
return Permission;
};
复制代码
查询role_id,permission_id归属,因此ui
app.model.RolePermission.belongsTo(app.model.Role, { as: 'role' });
app.model.RolePermission.belongsTo(app.model.Permission, { as: 'permission' });
复制代码
app\model\role_permission.js
'use strict';
module.exports = app => {
const { INTEGER, STRING, DATE } = app.Sequelize;
const RolePermission = app.model.define('role_permission', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
role_id: {
type: INTEGER,
},
permission_id: {
type: INTEGER,
},
created_at: DATE,
updated_at: DATE,
}, {
freezeTableName: true,
});
RolePermission.associate = function() {
app.model.RolePermission.belongsTo(app.model.Role, { as: 'role' });
app.model.RolePermission.belongsTo(app.model.Permission, { as: 'permission' });
};
return RolePermission;
};
复制代码
app\controller\home.js
async index() {
const { ctx } = this;
const data = await ctx.model.Role.findAll();
ctx.body = data;
}
复制代码