mm_power 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
+ module.exports = new Proxy({}, {
6
+ get(_t, key) {
7
+ if (typeof key !== 'string') return undefined;
8
+ if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
+ return require('./' + key);
10
+ },
11
+ has(_t, key) {
12
+ return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
+ },
14
+ ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
+ getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
+ });
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+ /**
3
+ * power —— RBAC 权限校验工具
4
+ * 复用模块自身的 Power 实例(os.js 注入配置),只做参数整形,不重写鉴权规则。
5
+ */
6
+ const { powerAdmin } = require('../../../index.js');
7
+
8
+ /**
9
+ * 取作用域内的鉴权实例
10
+ * @param {string} scope 作用域
11
+ * @returns {object} Power 实例
12
+ */
13
+ function _power(scope) {
14
+ return powerAdmin(scope || 'os');
15
+ }
16
+
17
+ async function main(params, ctx, options) {
18
+ void options;
19
+ const p = params || {};
20
+ const action = p.action || 'check';
21
+ const done = (data) => { ctx.output = data; return data; };
22
+
23
+ try {
24
+ const power = _power(p.scope);
25
+
26
+ if (action === 'user') {
27
+ return done({ code: 0, msg: '成功', user: power.getUser(p.user) });
28
+ }
29
+
30
+ if (action === 'admin') {
31
+ if (!p.admin) throw new TypeError('admin 管理组名称不能为空');
32
+ const reason = power.checkAdmin(p.user, p.admin);
33
+ return done({ code: 0, msg: reason || '通过', ok: !reason, reason: reason || '' });
34
+ }
35
+
36
+ if (action === 'field') {
37
+ const need = p.power || {};
38
+ const reason = power.checkField(p.user, {
39
+ vip: Number(need.vip || 0),
40
+ gm: Number(need.gm || 0),
41
+ mc: Number(need.mc || 0)
42
+ });
43
+ return done({ code: 0, msg: reason || '通过', ok: !reason, reason: reason || '' });
44
+ }
45
+
46
+ if (action === 'check') {
47
+ const reason = power.check(p.user, p.power || {});
48
+ return done({ code: 0, msg: reason || '通过', ok: !reason, reason: reason || '' });
49
+ }
50
+
51
+ return done({ code: 10001, msg: '不支持的操作: ' + action, ok: false });
52
+ } catch (error) {
53
+ return done({
54
+ code: 10000,
55
+ msg: '鉴权执行失败:' + error.message,
56
+ ok: false,
57
+ error: { name: error.name, message: error.message }
58
+ });
59
+ }
60
+ }
61
+
62
+ module.exports = { main };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_power",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "功能完整的JavaScript鉴权模块,支持RBAC模型和字段权限控制,完全满足gm、vip、mc等用户账户字段鉴权需求",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,9 +12,11 @@
12
12
  "example-async": "node example_async.js"
13
13
  },
14
14
  "keywords": [
15
+ "mm_mod",
16
+ "mm_crosscut",
17
+ "mm_authz_policy",
15
18
  "鉴权",
16
19
  "权限管理",
17
- "RBAC",
18
20
  "权限控制",
19
21
  "用户权限",
20
22
  "路由权限",
@@ -27,11 +29,13 @@
27
29
  "rbac",
28
30
  "access-control"
29
31
  ],
32
+ "layer": "interface",
33
+ "group": "gateway",
30
34
  "author": "MM Modules",
31
35
  "license": "MIT",
32
36
  "repository": {
33
37
  "type": "git",
34
- "url": "https://gitee.com/mm_modules_ok/mm_power.git"
38
+ "url": "https://gitee.com/qiuwenwu91/mm_power.git"
35
39
  },
36
40
  "engines": {
37
41
  "node": ">=12.0.0"
@@ -42,11 +46,15 @@
42
46
  },
43
47
  "dependencies": {
44
48
  "eslint-plugin-jsdoc": "^64.0.1",
45
- "mm_expand": "^2.5.1"
49
+ "mm_admin": "^1.1.0",
50
+ "mm_helper": "^1.0.0",
51
+ "mm_kv_store": "^1.0.1"
46
52
  },
47
53
  "files": [
48
54
  "index.js",
49
55
  "README.md",
50
- "LICENSE"
56
+ "LICENSE",
57
+ "tool",
58
+ "lib"
51
59
  ]
52
60
  }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * power —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/power/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_power').tools.power 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/power');
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "power",
3
+ "title": "RBAC 权限校验",
4
+ "description": "基于 mm_power 的鉴权能力:按用户对象校验管理组、字段等级(gm/vip/mc)与综合权限(路径/方法/角色/用户组)。适用场景:判断某用户能否访问接口、字段等级是否达标、是否属于指定管理组。工作流提示:action 选 check/admin/field/user,check 传 user 与 power,admin 传 user 与 admin,field 传 user 与 power{vip,gm,mc}。",
5
+ "category": "权限安全",
6
+ "group": "security",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": {
10
+ "type": "object",
11
+ "properties": {
12
+ "action": {
13
+ "type": "string",
14
+ "description": "操作类型: check(综合权限校验)/admin(管理组校验)/field(字段等级校验)/user(取标准用户对象)",
15
+ "enum": [
16
+ "check",
17
+ "admin",
18
+ "field",
19
+ "user"
20
+ ],
21
+ "default": "check"
22
+ },
23
+ "user": {
24
+ "type": "object",
25
+ "description": "用户对象,支持 gm/vip/mc、roles、groups、admins、user_id 等字段"
26
+ },
27
+ "admin": {
28
+ "type": "string",
29
+ "description": "管理组名称(admin 操作必填)"
30
+ },
31
+ "power": {
32
+ "type": "object",
33
+ "description": "权限要求:check 传 {path,method,role,group},field 传 {vip,gm,mc} 等级阈值"
34
+ },
35
+ "scope": {
36
+ "type": "string",
37
+ "description": "权限实例作用域,缺省 os(与 os.js 注入的实例一致)",
38
+ "default": "os"
39
+ }
40
+ },
41
+ "required": [
42
+ "action",
43
+ "user"
44
+ ]
45
+ },
46
+ "returns": {
47
+ "type": "object",
48
+ "properties": {
49
+ "code": {
50
+ "type": "number",
51
+ "description": "0 表示校验完成,10000 表示执行异常"
52
+ },
53
+ "msg": {
54
+ "type": "string",
55
+ "description": "结果描述,校验通过时为「通过」"
56
+ },
57
+ "ok": {
58
+ "type": "boolean",
59
+ "description": "是否通过校验"
60
+ },
61
+ "reason": {
62
+ "type": "string",
63
+ "description": "不通过原因"
64
+ },
65
+ "user": {
66
+ "type": "object",
67
+ "description": "标准化的用户对象(user 操作)"
68
+ },
69
+ "error": {
70
+ "type": "object",
71
+ "description": "异常对象 { name, message }"
72
+ }
73
+ }
74
+ },
75
+ "handler": "manager"
76
+ }