New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

egg-rbac

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-rbac - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

31

lib/mongoose/index.js

@@ -29,6 +29,2 @@ 'use strict';

addPermission(_id, permissionIds) {
return this.Role.updateOne({ _id }, { $addToSet: { grants: { $each: permissionIds } } });
}
newPermission({ name, alias }) {

@@ -47,2 +43,29 @@ return this.Permission.findOne({ name })

modifyRoleAlias(_id, alias) {
return this.Role.updateOne({ _id }, { $set: { alias } });
}
modifyPermissionAlias(_id, alias) {
return this.Permission.updateOne({ _id }, { $set: { alias } });
}
removeRole(_id) {
return this.Role.remove({ _id });
}
removePermission(_id) {
return Promise.all([
this.Permission.remove({ _id }),
this.Role.update({}, { $pull: { grants: _id } }),
]);
}
addPermission(_id, permissionIds) {
return this.Role.updateOne({ _id }, { $addToSet: { grants: { $each: permissionIds } } });
}
removePermissions(_id, permissionIds) {
return this.Role.updateOne({ _id }, { $pull: { grants: { $in: permissionIds } } });
}
insertManyPermission(permissions) {

@@ -49,0 +72,0 @@ return this.Permission.insertMany(permissions)

@@ -8,2 +8,4 @@ 'use strict';

alias: { type: String },
create_at: { type: Date, default: Date.now },
update_at: { type: Date, default: Date.now },
});

@@ -10,0 +12,0 @@

@@ -10,2 +10,4 @@ 'use strict';

grants: [{ type: ObjectId, ref: 'Permission' }],
create_at: { type: Date, default: Date.now },
update_at: { type: Date, default: Date.now },
});

@@ -12,0 +14,0 @@

@@ -8,4 +8,11 @@ 'use strict';

/**
* @class
*/
class rbac {
/**
* @constructs role
* @param {object} app eggjs application object
*/
constructor(app) {

@@ -31,2 +38,3 @@ this.config = app.config.mongoose;

* before start init permissions and roles
* @method rbac#initData
* @param {object[]} permissions - permission item array

@@ -42,9 +50,9 @@ * @param {string} permissions[].name - permission name

initData(permissions, roles) {
debug('init data permissions.length %O roles.length %O', permissions.length, roles.length);
if (!permissions) {
if (!permissions || permissions.length === 0) {
throw new Error('[egg-rbac] initData parameter permissions is undefined');
}
if (!roles) {
if (!roles || roles.length === 0) {
throw new Error('[egg-rbac] initData parameter roles is undefined');
}
debug('init data permissions.length %O roles.length %O', permissions.length, roles.length);

@@ -57,2 +65,4 @@ return this._initPermissions(permissions)

* Initialize permission
* @method rbac#_initPermissions
* @private
* @param {object[]} permissions - permission item array

@@ -86,2 +96,4 @@ * @param {string} permissions[].name - permission name

* Initialize roles and initialize superadmin role
* @method rbac#_initRole
* @private
* @param {object[]} roles - role item array

@@ -160,8 +172,7 @@ * @param {string} roles[].name - role name

* @method rbac#addPermission
* @param {object} options role info
* @param {string} options._id - role id
* @param {string[]} options.permissionIds - permission ids
* @param {string} _id - role id
* @param {string[]} permissionIds - permission ids
* @return {object} promise
*/
addPermission({ _id, permissionIds }) {
addPermission(_id, permissionIds) {
if (!_id) {

@@ -178,2 +189,75 @@ return new Error('[egg-rbac] addPermission parameter _id is undefined');

/**
* @method rbac#removePermissions
* @param {string} _id - role id
* @param {string[]} permissionIds - permission ids
* @return {object} promise
*/
removePermissions(_id, permissionIds) {
if (!_id) {
return new Error('[egg-rbac] removePermissions parameter _id is undefined');
}
if (!permissionIds || typeof permissionIds !== 'object' || permissionIds.length === 0) {
return new Error('[egg-rbac] removePermissions parameter permissionIds is undefined');
}
debug('new permission name %s alias %s', _id, permissionIds);
return this.storage.removePermissions(_id, permissionIds);
}
/**
* @method rbac#removeRole
* @param {string} _id - role _id
* @return {object} promise
*/
removeRole(_id) {
if (!_id) {
return new Error('[egg-rbac] removeRole parameter _id is undefined');
}
return this.storage.removeRole(_id);
}
/**
* @method rbac#removePermission
* @param {string} _id - permission _id
* @return {object} promise
*/
removePermission(_id) {
if (!_id) {
return new Error('[egg-rbac] removePermission parameter _id is undefined');
}
return this.storage.removePermission(_id);
}
/**
* @method rbac#modifyRoleAlias
* @param {string} _id - role _id
* @param {string} alias - new alias string
* @return {object} promise
*/
modifyRoleAlias(_id, alias) {
if (!_id) {
return new Error('[egg-rbac] modifyRoleAlias parameter _id is undefined');
}
if (!alias) {
return new Error('[egg-rbac] modifyRoleAlias parameter alias is undefined');
}
return this.storage.modifyRoleAlias(_id, alias);
}
/**
* @method rbac#modifyPermissionAlias
* @param {string} _id - role _id
* @param {string} alias - new alias string
* @return {object} promise
*/
modifyPermissionAlias(_id, alias) {
if (!_id) {
return new Error('[egg-rbac] modifyPermissionAlias parameter _id is undefined');
}
if (!alias) {
return new Error('[egg-rbac] modifyPermissionAlias parameter alias is undefined');
}
return this.storage.modifyPermissionAlias(_id, alias);
}
/**
* @method rbac#getRolePermission

@@ -201,2 +285,14 @@ * @param {string} name - role name

/**
* @method rbac#getRole
* @param {string} name role name
* @return {object} promise
*/
getRole(name) {
if (!name || typeof name !== 'string') {
return new Error('[egg-rbac] getRole parameter name must string');
}
return this.storage.getRole(name);
}
/**
* @method rbac#getAllRoles

@@ -210,3 +306,3 @@ * @return {object} promise

/**
*
* @method rbac#can
* @param {string} permissionName - permission name

@@ -213,0 +309,0 @@ * @return {function} middleware function

12

lib/role.js
'use strict';
const assert = require('assert');
/**
* @class Role
*/
module.exports = exports = class Role {
/**
*
* @constructs Role
* @param {string} roleName - role name

@@ -48,2 +50,5 @@ * @param {object} permissionItems - singleton rbac object

/**
* @member {string}
*/
get roleName() {

@@ -54,3 +59,3 @@ return this._name;

/**
*
* @method Role#can
* @param {string} permissionName - permisston name

@@ -65,2 +70,3 @@ * @return {boolen} can or not

* check the role grant all permission or not
* @method Role#canAll
* @param {string} permissionNames - permisston name

@@ -67,0 +73,0 @@ * @return {boolen} can or not

{
"name": "egg-rbac",
"version": "0.1.0",
"version": "0.2.0",
"description": "Role Based Access Control for eggjs",

@@ -30,2 +30,3 @@ "eggPlugin": {

"eslint-config-egg": "^5.1.1",
"jsdoc": "^3.5.5",
"mongoose": "^4.13.1",

@@ -45,3 +46,4 @@ "power-assert": "^1.4.4",

"pkgfiles": "egg-bin pkgfiles",
"autod": "autod"
"autod": "autod",
"docs": "jsdoc -c jsDocConfig.json -r"
},

@@ -48,0 +50,0 @@ "files": [

@@ -1,2 +0,1 @@

# Under development
# egg-rbac

@@ -27,2 +26,3 @@

-->
## [中文说明](./README.zh_CN.md)

@@ -35,2 +35,6 @@ ## Install

## depend on egg plugin
- [egg-mongoose](https://github.com/eggjs/egg-mongoose)
## Usage

@@ -48,17 +52,43 @@

config getRoleName
```js
// {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('');
},
};
```
see [config/config.default.js](config/config.default.js) for more detail.
Initialize roles and permissions
```js
// {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](./test/fixtures/apps/rbac-test/config/config.unittest.js) for more detail.
## Example
<!-- example here -->
see [rbac-test](./test/fixtures/apps/rbac-test/)
## Questions & Suggestions
## Remarks
Please open an issue [here](https://github.com/eggjs/egg/issues).
- It will create a superadmin role which own all permissions.

@@ -65,0 +95,0 @@ ## License

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc