$ npm i egg-rbac --save
egg-rbac version | egg version |
---|---|
0.3.0 | 1.x |
0.4.0 | 2.0.0 |
From 0.4.0 version egg-rbac does not support egg 1.x.
// {app_root}/config/plugin.js
exports.rbac = {
enable: true,
package: 'egg-rbac',
};
config getRoleName
// {app_root}/config/config.default.js
exports.rbac = {
/**
* @param {object} ctx - egg context object
* @return {object} promise, if resolve data is falsy, no role
*/
* getRoleName(ctx) {
return Promise.resolve('');
},
};
Initialize roles and permissions
// {app_root/config/rbac.js}
'use strict';
exports.permissions = [
// action_resource
// { name: 'create_user', alias: '创建用户' },
// { name: 'delete_user', alias: '删除用户' },
// { name: 'query_user', alias: '查询用户' },
// { name: 'edit_user', alias: '修改用户' },
];
exports.roles = [
// { name: 'admin', alias: '管理员', grants: exports.permissions.map(item => item.name) },
];
see config/config.unittest.js for more detail.
- see rbac-test
- practical example egg-rbac-example
- It will create a superadmin role which own all permissions.