mm_power 1.0.0 → 1.0.2

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/index.js +19 -1949
  3. package/package.json +55 -50
package/index.js CHANGED
@@ -1,1949 +1,19 @@
1
- require('mm_expand');
2
-
3
- /**
4
- * 鉴权类
5
- * @class
6
- */
7
- class Power {
8
- /**
9
- * 创建鉴权实例
10
- * @param {Object} config 配置
11
- */
12
- constructor(config) {
13
- this.config = {
14
- // 用户ID字段名
15
- user_id: 'user_id',
16
- // 管理员级别字段名
17
- gm: 'gm',
18
- // VIP级别字段名
19
- vip: 'vip',
20
- // 商家级别字段名
21
- mc: 'mc',
22
- // 管理组权限字段名
23
- admins: 'admins',
24
- // 用户组权限字段名
25
- groups: 'groups',
26
- // 登录状态字段名
27
- login: 'login',
28
- // 权限生效开始时间字段名
29
- time_start: 'time_start',
30
- // 权限生效结束时间字段名
31
- time_end: 'time_end'
32
- };
33
- this._routes = {}; // 路由权限配置
34
- this._roles = {}; // 角色权限配置
35
- this._groups = {}; // 用户组配置
36
- this._admins = {}; // 管理组配置
37
- this._cache = {}; // 配置缓存
38
- this._loader = null; // 异步加载器
39
- this.setConfig(config || {});
40
- }
41
-
42
- /**
43
- * 转换用户数据到内部标准格式
44
- * @param {Object} userData 原始用户数据
45
- * @returns {Object} 转换后的标准格式用户数据
46
- */
47
- _transformUser(userData) {
48
- if (!userData) return null;
49
-
50
- // 如果已经是标准格式,直接返回
51
- if (this._isStandardFormat(userData)) {
52
- return userData;
53
- }
54
-
55
- const transformed = this._mapStandardFields(userData);
56
- this._handleUndefined(userData, transformed);
57
- this._copyOtherFields(userData, transformed);
58
-
59
- return transformed;
60
- }
61
-
62
- /**
63
- *
64
- */
65
- _isStandardFormat(userData) {
66
- return userData.user_id !== undefined &&
67
- userData.admins !== undefined &&
68
- userData.groups !== undefined;
69
- }
70
-
71
- /**
72
- *
73
- */
74
- _mapStandardFields(userData) {
75
- const transformed = {};
76
-
77
- // 映射标准字段
78
- transformed.user_id = userData[this.config.user_id];
79
- transformed.gm = userData[this.config.gm];
80
- transformed.vip = userData[this.config.vip];
81
- transformed.mc = userData[this.config.mc];
82
- transformed.admins = userData[this.config.admins];
83
- transformed.groups = userData[this.config.groups];
84
- transformed.login = userData[this.config.login];
85
- transformed.time_start = userData[this.config.time_start];
86
- transformed.time_end = userData[this.config.time_end];
87
-
88
- return transformed;
89
- }
90
-
91
- /**
92
- *
93
- */
94
- _handleUndefined(userData, transformed) {
95
- // 如果映射后的字段是undefined,但原始数据中存在同名字段,则使用原始数据
96
- if (transformed.admins === undefined && userData.admins !== undefined) {
97
- transformed.admins = userData.admins;
98
- }
99
- if (transformed.groups === undefined && userData.groups !== undefined) {
100
- transformed.groups = userData.groups;
101
- }
102
- if (transformed.user_id === undefined && userData.user_id !== undefined) {
103
- transformed.user_id = userData.user_id;
104
- }
105
- }
106
-
107
- /**
108
- *
109
- */
110
- _copyOtherFields(userData, transformed) {
111
- // 复制其他字段
112
- for (const key in userData) {
113
- if (!(key in transformed) && userData.hasOwnProperty(key)) {
114
- transformed[key] = userData[key];
115
- }
116
- }
117
- }
118
- }
119
-
120
- /**
121
- * 设置异步配置加载器
122
- * @param {Function} loader 异步加载器
123
- */
124
- Power.prototype.setLoader = function (loader) {
125
- if (typeof loader !== 'function') {
126
- throw new TypeError('加载器必须是函数');
127
- }
128
- this._loader = loader;
129
- };
130
-
131
- /**
132
- * 异步加载配置
133
- * @param {string} type 配置类型
134
- * @param {string} key 配置键
135
- * @returns {Promise<Object>} 配置
136
- */
137
- Power.prototype._loadConfig = async function (type, key) {
138
- if (!this._loader) {
139
- throw new Error('异步加载器未设置');
140
- }
141
-
142
- const cache_key = `${type}:${key}`;
143
- if (this._cache[cache_key]) {
144
- return this._cache[cache_key];
145
- }
146
-
147
- const config = await this._loader(type, key);
148
- this._cache[cache_key] = config;
149
- return config;
150
- };
151
-
152
- /**
153
- * 异步设置配置
154
- * @param {Object} config 配置
155
- * @returns {Promise<void>}
156
- */
157
- Power.prototype.setConfigAsync = async function (config) {
158
- if (!config) {
159
- throw new TypeError('配置是必需的');
160
- }
161
- if (typeof config !== 'object') {
162
- throw new TypeError('配置必须是对象');
163
- }
164
- Object.assign(this.config, config);
165
- };
166
-
167
- /**
168
- * 异步设置路由权限配置
169
- * @param {Object} routes 路由配置
170
- * @returns {Promise<void>}
171
- */
172
- Power.prototype.setRoutesAsync = async function (routes) {
173
- if (!routes) {
174
- throw new TypeError('路由是必需的');
175
- }
176
- if (typeof routes !== 'object') {
177
- throw new TypeError('路由必须是对象');
178
- }
179
- this._routes = routes;
180
- };
181
-
182
- /**
183
- * 异步设置角色权限配置
184
- * @param {Object} roles 角色配置
185
- * @returns {Promise<void>}
186
- */
187
- Power.prototype.setRolesAsync = async function (roles) {
188
- if (!roles) {
189
- throw new TypeError('角色是必需的');
190
- }
191
- if (typeof roles !== 'object') {
192
- throw new TypeError('角色必须是对象');
193
- }
194
- this._roles = roles;
195
- };
196
-
197
- /**
198
- * 异步设置用户组配置
199
- * @param {Object} groups 用户组配置
200
- * @returns {Promise<void>}
201
- */
202
- Power.prototype.setGroupsAsync = async function (groups) {
203
- if (!groups) {
204
- throw new TypeError('用户组是必需的');
205
- }
206
- if (typeof groups !== 'object') {
207
- throw new TypeError('用户组必须是对象');
208
- }
209
- this._groups = groups;
210
- };
211
-
212
- /**
213
- * 异步设置管理组配置
214
- * @param {Object} admins 管理组配置
215
- * @returns {Promise<void>}
216
- */
217
- Power.prototype.setAdminsAsync = async function (admins) {
218
- if (!admins) {
219
- throw new TypeError('管理组是必需的');
220
- }
221
- if (typeof admins !== 'object') {
222
- throw new TypeError('管理组必须是对象');
223
- }
224
- this._admins = admins;
225
- };
226
-
227
- /**
228
- * 异步检查管理组权限
229
- * @param {Object} user 用户
230
- * @param {string} admin 管理组
231
- * @returns {Promise<string>} 结果
232
- */
233
-
234
-
235
-
236
- Power.prototype._checkAdminLevel = function (options, user) {
237
- if (!options.gm || !user.gm) {
238
- return null;
239
- }
240
-
241
- if (user.gm < options.gm) {
242
- return `管理级别不足: ${user.gm} < ${options.gm}`;
243
- }
244
-
245
- return null;
246
- };
247
-
248
- Power.prototype.checkAdminAsync = async function (user, admin) {
249
- if (!user) {
250
- throw new TypeError('用户是必需的');
251
- }
252
- if (!admin) {
253
- throw new TypeError('管理组是必需的');
254
- }
255
-
256
- // 如果user是字符串,则认为是用户ID,需要通过getUserAsync获取用户对象
257
- const userObj = await this._getUserIfNeeded(user);
258
- if (typeof user === 'string' && !userObj) {
259
- return '用户不存在';
260
- }
261
-
262
- // 转换用户数据到内部标准格式
263
- const std_user = this._transformUser(userObj);
264
-
265
- // 异步加载管理组配置
266
- const options = await this._loadConfig('admins', admin);
267
- if (!options) {
268
- return `管理组配置不存在: ${admin}`;
269
- }
270
-
271
- // 检查各项权限
272
- const checks = [
273
- () => this._checkUserInAdmin(std_user, admin),
274
- () => this._checkAdminTime(options, admin),
275
- () => this._checkAdminIp(options, std_user),
276
- () => this._checkAdminLevel(options, std_user)
277
- ];
278
-
279
- for (const check of checks) {
280
- const result = check();
281
- if (result) {
282
- return result;
283
- }
284
- }
285
-
286
- return '';
287
- };
288
-
289
- Power.prototype._getUserIfNeeded = async function (user) {
290
- if (typeof user === 'string') {
291
- return await this.getUserAsync(user);
292
- }
293
- return user;
294
- };
295
-
296
- Power.prototype._checkUserInAdmin = function (user, admin) {
297
- if (!user.admins || !user.admins.includes(admin)) {
298
- return `用户不属于管理组: ${admin}`;
299
- }
300
- return null;
301
- };
302
-
303
- /**
304
- * 异步检查管理组权限(支持多个管理组)
305
- * @param {Object} user 用户
306
- * @param {string|Array} admins 管理组
307
- * @param {number} way 检查方式
308
- * @returns {Promise<string>} 结果
309
- */
310
- Power.prototype.checkAdminsAsync = async function (user, admins, way = 0) {
311
- if (!user) {
312
- throw new TypeError('用户是必需的');
313
- }
314
- if (!admins) {
315
- throw new TypeError('管理组是必需的');
316
- }
317
-
318
- // 如果user是字符串,则认为是用户ID
319
- const userObj = await this._getUserIfNeeded(user);
320
- if (typeof user === 'string' && !userObj) {
321
- return '用户不存在';
322
- }
323
-
324
- const grps = Array.isArray(admins) ? admins : [admins];
325
-
326
- switch (way) {
327
- case 1:
328
- return await this._checkAllAdminsAsync(userObj, grps);
329
- case 2:
330
- return await this._checkAnyAdminAsync(userObj, grps);
331
- default:
332
- return await this._checkFirstAdmin(userObj, grps);
333
- }
334
- };
335
-
336
- Power.prototype._checkAllAdminsAsync = async function (userObj, grps) {
337
- // 必须拥有所有管理组权限
338
- for (const grp of grps) {
339
- const res = await this.checkAdminAsync(userObj, grp);
340
- if (res) {
341
- return res;
342
- }
343
- }
344
- return '';
345
- };
346
-
347
- Power.prototype._checkAnyAdminAsync = async function (userObj, grps) {
348
- // 拥有任一管理组权限即可
349
- for (const grp of grps) {
350
- const res = await this.checkAdminAsync(userObj, grp);
351
- if (!res) {
352
- return '';
353
- }
354
- }
355
- return `缺少任一管理组权限: ${grps.join(', ')}`;
356
- };
357
-
358
- Power.prototype._checkFirstAdmin = async function (userObj, grps) {
359
- // 默认检查第一个管理组
360
- if (grps.length > 0) {
361
- return await this.checkAdminAsync(userObj, grps[0]);
362
- }
363
- return '';
364
- };
365
-
366
- /**
367
- * 设置配置
368
- * @param {Object} config 配置
369
- */
370
- Power.prototype.setConfig = function (config) {
371
- if (!config) {
372
- throw new TypeError('配置是必需的');
373
- }
374
- if (typeof config !== 'object') {
375
- throw new TypeError('配置必须是对象');
376
- }
377
- Object.assign(this.config, config);
378
- };
379
-
380
- /**
381
- * 设置路由权限配置
382
- * @param {Object} routes 路由配置
383
- */
384
- Power.prototype.setRoutes = function (routes) {
385
- if (!routes) {
386
- throw new TypeError('路由是必需的');
387
- }
388
- if (typeof routes !== 'object') {
389
- throw new TypeError('路由必须是对象');
390
- }
391
- this._routes = routes;
392
- };
393
-
394
- /**
395
- * 设置角色权限配置
396
- * @param {Object} roles 角色配置
397
- */
398
- Power.prototype.setRoles = function (roles) {
399
- if (!roles) {
400
- throw new TypeError('角色是必需的');
401
- }
402
- if (typeof roles !== 'object') {
403
- throw new TypeError('角色必须是对象');
404
- }
405
- this._roles = roles;
406
- };
407
-
408
- /**
409
- * 设置用户组配置
410
- * @param {Object} groups 用户组配置
411
- */
412
- Power.prototype.setGroups = function (groups) {
413
- if (!groups) {
414
- throw new TypeError('用户组是必需的');
415
- }
416
- if (typeof groups !== 'object') {
417
- throw new TypeError('用户组必须是对象');
418
- }
419
- this._groups = groups;
420
- };
421
-
422
- /**
423
- * 设置管理组配置
424
- * @param {Object} admins 管理组配置
425
- */
426
- Power.prototype.setAdmins = function (admins) {
427
- if (!admins) {
428
- throw new TypeError('管理组是必需的');
429
- }
430
- if (typeof admins !== 'object') {
431
- throw new TypeError('管理组必须是对象');
432
- }
433
- this._admins = admins;
434
- };
435
-
436
- /**
437
- * 检查管理组权限
438
- * @param {Object} user 用户
439
- * @param {string} admin 管理组
440
- * @returns {string} 结果
441
- */
442
- Power.prototype.checkAdmin = function (user, admin) {
443
- if (!user) {
444
- throw new TypeError('用户是必需的');
445
- }
446
- if (!admin) {
447
- throw new TypeError('管理组是必需的');
448
- }
449
-
450
- // 转换用户数据到内部标准格式
451
- const stdUser = this._transformUser(user);
452
-
453
- // 检查用户是否属于该管理组
454
- if (!stdUser.admins || !stdUser.admins.includes(admin)) {
455
- return `用户不属于管理组: ${admin}`;
456
- }
457
-
458
- // 获取管理组配置
459
- const options = this._admins[admin];
460
- if (!options) {
461
- return `管理组配置不存在: ${admin}`;
462
- }
463
-
464
- return this._checkAdminDetails(options, stdUser, admin);
465
- };
466
-
467
- /**
468
- * 检查管理组详细权限
469
- * @param {Object} options 管理组配置
470
- * @param {Object} stdUser 标准用户对象
471
- * @param {string} admin 管理组
472
- * @returns {string} 结果
473
- */
474
- Power.prototype._checkAdminDetails = function (options, stdUser, admin) {
475
- // 检查时间范围权限
476
- const time_result = this._checkAdminTime(options, admin);
477
- if (time_result) {
478
- return time_result;
479
- }
480
-
481
- // 检查IP白名单
482
- const ip_result = this._checkAdminIp(options, stdUser);
483
- if (ip_result) {
484
- return ip_result;
485
- }
486
-
487
- // 检查管理级别
488
- const level_result = this._checkAdminLevel(options, stdUser);
489
- if (level_result) {
490
- return level_result;
491
- }
492
-
493
- return '';
494
- };
495
-
496
- /**
497
- * 检查管理组时间范围权限
498
- * @param {Object} options 管理组配置
499
- * @param {string} admin 管理组
500
- * @returns {string} 结果
501
- */
502
- Power.prototype._checkAdminTime = function (options, admin) {
503
- if (!options.time_start || !options.time_end) {
504
- return null;
505
- }
506
-
507
- const now = new Date();
508
- const start = new Date(options.time_start);
509
- const end = new Date(options.time_end);
510
-
511
- if (now < start || now > end) {
512
- return `管理组时间范围权限不足: ${admin}`;
513
- }
514
-
515
- return null;
516
- };
517
-
518
- /**
519
- * 检查管理组IP白名单权限
520
- * @param {Object} options 管理组配置
521
- * @param {Object} user 用户
522
- * @returns {string} 结果
523
- */
524
- Power.prototype._checkAdminIp = function (options, user) {
525
- if (!options.ip_whitelist || !user.ip) {
526
- return null;
527
- }
528
-
529
- if (!options.ip_whitelist.includes(user.ip)) {
530
- return `IP不在管理组白名单内: ${user.ip}`;
531
- }
532
-
533
- return null;
534
- };
535
-
536
- /**
537
- * 检查管理级别权限
538
- * @param {Object} options 管理组配置
539
- * @param {Object} user 用户
540
- * @returns {string} 结果
541
- */
542
- Power.prototype._checkAdminLevel = function (options, user) {
543
- if (!options.gm || !user.gm) {
544
- return null;
545
- }
546
-
547
- if (user.gm < options.gm) {
548
- return `管理级别不足: ${user.gm} < ${options.gm}`;
549
- }
550
-
551
- return null;
552
- };
553
-
554
- /**
555
- * 检查管理组权限(支持多个管理组)
556
- * @param {Object} user 用户
557
- * @param {string|Array} admins 管理组
558
- * @param {number} way 检查方式
559
- * @returns {string} 结果
560
- */
561
- Power.prototype.checkAdmins = function (user, admins, way = 0) {
562
- if (!user) {
563
- throw new TypeError('用户是必需的');
564
- }
565
- if (!admins) {
566
- throw new TypeError('管理组是必需的');
567
- }
568
-
569
- // 转换用户数据到内部标准格式
570
- const std_user = this._transformUser(user);
571
-
572
- const grps = Array.isArray(admins) ? admins : [admins];
573
-
574
- if (way === 1) {
575
- return this._checkAllAdmins(std_user, grps);
576
- } else if (way === 2) {
577
- return this._checkAnyAdmin(std_user, grps);
578
- } else {
579
- return this._checkFirstAdmin(std_user, grps);
580
- }
581
- };
582
-
583
- /**
584
- * 检查所有管理组权限
585
- * @param {Object} user 用户
586
- * @param {Array} grps 管理组列表
587
- * @returns {string} 结果
588
- */
589
- Power.prototype._checkAllAdmins = function (user, grps) {
590
- for (const grp of grps) {
591
- const res = this.checkAdmin(user, grp);
592
- if (res) {
593
- return res;
594
- }
595
- }
596
- return '';
597
- };
598
-
599
- /**
600
- * 检查任一管理组权限
601
- * @param {Object} user 用户
602
- * @param {Array} grps 管理组列表
603
- * @returns {string} 结果
604
- */
605
- Power.prototype._checkAnyAdmin = function (user, grps) {
606
- let has_perm = false;
607
- for (const grp of grps) {
608
- const res = this.checkAdmin(user, grp);
609
- if (!res) {
610
- has_perm = true;
611
- break;
612
- }
613
- }
614
- if (!has_perm) {
615
- return `缺少任一管理组权限: ${grps.join(', ')}`;
616
- }
617
- return '';
618
- };
619
-
620
- /**
621
- * 检查第一个管理组权限
622
- * @param {Object} user 用户
623
- * @param {Array} grps 管理组列表
624
- * @returns {string} 结果
625
- */
626
- Power.prototype._checkFirstAdmin = function (user, grps) {
627
- if (grps.length > 0) {
628
- return this.checkAdmin(user, grps[0]);
629
- }
630
- return '';
631
- };
632
-
633
- /**
634
- * 检查字段权限
635
- * @param {Object} user 用户
636
- * @param {Object} power 权限
637
- * @param {Function} func 函数
638
- * @param {number} way 方式
639
- * @returns {string} 结果
640
- */
641
- Power.prototype.checkField = function (
642
- user,
643
- power = {
644
- vip: 0,
645
- gm: 0,
646
- mc: 0
647
- },
648
- func,
649
- way = 0
650
- ) {
651
- if (!user) {
652
- throw new TypeError('用户是必需的');
653
- }
654
-
655
- // 转换用户数据到内部标准格式
656
- const std_user = this._transformUser(user);
657
-
658
- this._initUserFields(std_user);
659
-
660
- if (way === 1) {
661
- return this._checkAllFields(std_user, power);
662
- } else if (way === 2) {
663
- return this._checkAnyFields(std_user, power);
664
- } else {
665
- return this._checkDefaultFields(std_user, power);
666
- }
667
- };
668
-
669
- /**
670
- * 初始化用户字段
671
- * @param {Object} user 用户
672
- */
673
- Power.prototype._initUserFields = function (user) {
674
- if (!user.vip) {
675
- user.vip = 0;
676
- }
677
- if (!user.gm) {
678
- user.gm = 0;
679
- }
680
- if (!user.mc) {
681
- user.mc = 0;
682
- }
683
- };
684
-
685
- /**
686
- * 检查所有字段权限
687
- * @param {Object} user 用户
688
- * @param {Object} power 权限
689
- * @returns {string} 结果
690
- */
691
- Power.prototype._checkAllFields = function (user, power) {
692
- if (user.gm < power.gm) {
693
- return 'gm等级不足';
694
- }
695
- if (user.mc < power.mc) {
696
- return 'mc等级不足';
697
- }
698
- if (user.vip < power.vip) {
699
- return 'vip等级不足';
700
- }
701
- return '';
702
- };
703
-
704
- /**
705
- * 检查任一字段权限
706
- * @param {Object} user 用户
707
- * @param {Object} power 权限
708
- * @returns {string} 结果
709
- */
710
- Power.prototype._checkAnyFields = function (user, power) {
711
- if (user.vip < power.vip && user.gm < power.gm && user.mc < power.mc) {
712
- return 'vip等级、gm等级、mc等级不足';
713
- }
714
- return '';
715
- };
716
-
717
- /**
718
- * 检查默认字段权限
719
- * @param {Object} user 用户
720
- * @param {Object} power 权限
721
- * @returns {string} 结果
722
- */
723
- Power.prototype._checkDefaultFields = function (user, power) {
724
- if (user.gm < power.gm) {
725
- if (user.vip < power.vip && user.mc < power.mc) {
726
- return 'vip等级、mc等级不足';
727
- }
728
- }
729
- return '';
730
- };
731
-
732
- /**
733
- * 获取用户
734
- * @param {string} user_id 用户ID
735
- * @returns {Object} 用户
736
- */
737
- Power.prototype.getUser = async function (user_id) {
738
- return {
739
- vip: 2,
740
- gm: 5,
741
- mc: 3,
742
- admin: '',
743
- group: 'vip'
744
- };
745
- };
746
-
747
- /**
748
- * 检查权限
749
- * @param {string} user_id 用户ID
750
- * @param {Function} func 函数
751
- * @param {number} way 方式
752
- * @returns {string} 结果
753
- */
754
- Power.prototype.check = async function (
755
- user_id,
756
- power = {},
757
- func = null,
758
- way = 0
759
- ) {
760
- const user = await this.getUser(user_id);
761
- if (!user) {
762
- return '用户不存在';
763
- }
764
- const result = this.checkField(user, power, func, way);
765
- if (result) {
766
- return result;
767
- }
768
- const result2 = this.checkPath(user, power, func, way);
769
- if (result2) {
770
- return result2;
771
- }
772
- const result3 = this.checkMethod(user, power, func, way);
773
- if (result3) {
774
- return result3;
775
- }
776
- return '';
777
- };
778
-
779
- /**
780
- * 检查路径权限
781
- * @param {Object} user 用户
782
- * @param {Object} power 权限
783
- * @param {Function} func 函数
784
- * @param {number} way 方式
785
- * @returns {string} 结果
786
- */
787
- Power.prototype.checkPath = function (user, power = {}, func = null, way = 0) {
788
- if (!user) {
789
- throw new TypeError('用户是必需的');
790
- }
791
-
792
- // 转换用户数据到内部标准格式
793
- const std_user = this._transformUser(user);
794
-
795
- if (power.path) {
796
- const user_paths = std_user.paths || [];
797
- const required_paths = Array.isArray(power.path)
798
- ? power.path
799
- : [power.path];
800
-
801
- if (way === 1) {
802
- return this._checkAllPaths(user_paths, required_paths);
803
- } else if (way === 2) {
804
- return this._checkAnyPath(user_paths, required_paths);
805
- } else {
806
- return this._checkFirstPath(user_paths, required_paths);
807
- }
808
- }
809
-
810
- if (func) {
811
- return func(std_user, power, 'checkPath');
812
- }
813
- return '';
814
- };
815
-
816
- /**
817
- * 检查所有路径权限
818
- * @param {Array} user_paths 用户路径列表
819
- * @param {Array} required_paths 所需路径列表
820
- * @returns {string} 结果
821
- */
822
- Power.prototype._checkAllPaths = function (user_paths, required_paths) {
823
- for (const path of required_paths) {
824
- if (!user_paths.includes(path)) {
825
- return `缺少路径权限: ${path}`;
826
- }
827
- }
828
- return '';
829
- };
830
-
831
- /**
832
- * 检查任一路径权限
833
- * @param {Array} user_paths 用户路径列表
834
- * @param {Array} required_paths 所需路径列表
835
- * @returns {string} 结果
836
- */
837
- Power.prototype._checkAnyPath = function (user_paths, required_paths) {
838
- let has_access = false;
839
- for (const path of required_paths) {
840
- if (user_paths.includes(path)) {
841
- has_access = true;
842
- break;
843
- }
844
- }
845
- if (!has_access) {
846
- return '无任何路径权限';
847
- }
848
- return '';
849
- };
850
-
851
- /**
852
- * 检查第一个路径权限
853
- * @param {Array} user_paths 用户路径列表
854
- * @param {Array} required_paths 所需路径列表
855
- * @returns {string} 结果
856
- */
857
- Power.prototype._checkFirstPath = function (user_paths, required_paths) {
858
- if (required_paths.length > 0 && !user_paths.includes(required_paths[0])) {
859
- return `缺少路径权限: ${required_paths[0]}`;
860
- }
861
- return '';
862
- };
863
-
864
- /**
865
- * 检查方法权限
866
- * @param {Object} user 用户
867
- * @param {Object} power 权限
868
- * @param {Function} func 函数
869
- * @param {number} way 方式
870
- * @returns {string} 结果
871
- */
872
- Power.prototype.checkMethod = function (
873
- user,
874
- power = {},
875
- func = null,
876
- way = 0
877
- ) {
878
- if (!user) {
879
- throw new TypeError('用户是必需的');
880
- }
881
-
882
- // 转换用户数据到内部标准格式
883
- const std_user = this._transformUser(user);
884
-
885
- if (power.method) {
886
- const method_result = this._checkMethodWay(std_user, power, way);
887
- if (method_result) {
888
- return method_result;
889
- }
890
- }
891
-
892
- if (func) {
893
- return func(std_user, power, 'checkMethod');
894
- }
895
- return '';
896
- };
897
-
898
- /**
899
- * 检查方法权限方式
900
- * @param {Object} user 用户
901
- * @param {Object} power 权限
902
- * @param {number} way 方式
903
- * @returns {string} 结果
904
- */
905
- Power.prototype._checkMethodWay = function (user, power, way) {
906
- const user_methods = user.methods || [];
907
- const required_methods = Array.isArray(power.method)
908
- ? power.method
909
- : [power.method];
910
-
911
- if (way === 1) {
912
- return this._checkAllMethods(user_methods, required_methods);
913
- } else if (way === 2) {
914
- return this._checkAnyMethod(user_methods, required_methods);
915
- } else {
916
- return this._checkFirstMethod(user_methods, required_methods);
917
- }
918
- };
919
-
920
- /**
921
- * 检查所有方法权限
922
- * @param {Array} user_methods 用户方法
923
- * @param {Array} required_methods 要求方法
924
- * @returns {string} 结果
925
- */
926
- Power.prototype._checkAllMethods = function (user_methods, required_methods) {
927
- for (const method of required_methods) {
928
- if (!user_methods.includes(method.toUpperCase())) {
929
- return `缺少方法权限: ${method}`;
930
- }
931
- }
932
- return null;
933
- };
934
-
935
- /**
936
- * 检查任一方法权限
937
- * @param {Array} user_methods 用户方法
938
- * @param {Array} required_methods 要求方法
939
- * @returns {string} 结果
940
- */
941
- Power.prototype._checkAnyMethod = function (user_methods, required_methods) {
942
- let has_access = false;
943
- for (const method of required_methods) {
944
- if (user_methods.includes(method.toUpperCase())) {
945
- has_access = true;
946
- break;
947
- }
948
- }
949
- if (!has_access) {
950
- return '无任何方法权限';
951
- }
952
- return null;
953
- };
954
-
955
- /**
956
- * 检查第一个方法权限
957
- * @param {Array} user_methods 用户方法
958
- * @param {Array} required_methods 要求方法
959
- * @returns {string} 结果
960
- */
961
- Power.prototype._checkFirstMethod = function (user_methods, required_methods) {
962
- if (
963
- required_methods.length > 0 &&
964
- !user_methods.includes(required_methods[0].toUpperCase())
965
- ) {
966
- return `缺少方法权限: ${required_methods[0]}`;
967
- }
968
- return null;
969
- };
970
-
971
- /**
972
- * 检查角色权限
973
- * @param {Object} user 用户
974
- * @param {string} role 角色
975
- * @returns {string} 结果
976
- */
977
- Power.prototype.checkRole = function (user, role) {
978
- if (!user) {
979
- throw new TypeError('用户是必需的');
980
- }
981
- if (!role) {
982
- throw new TypeError('角色是必需的');
983
- }
984
-
985
- // 转换用户数据到内部标准格式
986
- const std_user = this._transformUser(user);
987
-
988
- const user_roles = std_user.roles || [];
989
- if (!user_roles.includes(role)) {
990
- return `缺少角色权限: ${role}`;
991
- }
992
-
993
- // 检查角色层级
994
- if (this._roles[role]) {
995
- const required_level = this._roles[role].level || 0;
996
- const user_level = std_user.level || 0;
997
- if (user_level < required_level) {
998
- return `角色等级不足: ${role}`;
999
- }
1000
- }
1001
-
1002
- return '';
1003
- };
1004
-
1005
- /**
1006
- * 检查用户组权限
1007
- * @param {Object} user 用户
1008
- * @param {string} group 用户组
1009
- * @returns {string} 结果
1010
- */
1011
- Power.prototype.checkGroup = function (user, group) {
1012
- if (!user) {
1013
- throw new TypeError('用户是必需的');
1014
- }
1015
- if (!group) {
1016
- throw new TypeError('用户组是必需的');
1017
- }
1018
-
1019
- // 转换用户数据到内部标准格式
1020
- const std_user = this._transformUser(user);
1021
-
1022
- const user_groups = std_user.groups || [];
1023
- if (!user_groups.includes(group)) {
1024
- return `缺少用户组权限: ${group}`;
1025
- }
1026
-
1027
- // 检查用户组权限
1028
- if (this._groups[group]) {
1029
- const options = this._groups[group];
1030
-
1031
- const time_result = this._checkGroupTime(options, group);
1032
- if (time_result) {
1033
- return time_result;
1034
- }
1035
-
1036
- const ip_result = this._checkGroupIp(options, std_user);
1037
- if (ip_result) {
1038
- return ip_result;
1039
- }
1040
- }
1041
-
1042
- return '';
1043
- };
1044
-
1045
- /**
1046
- * 检查用户组时间范围权限
1047
- * @param {Object} options 用户组配置
1048
- * @param {string} group 用户组
1049
- * @returns {string} 结果
1050
- */
1051
- Power.prototype._checkGroupTime = function (options, group) {
1052
- if (!options.time_start || !options.time_end) {
1053
- return null;
1054
- }
1055
-
1056
- const now = new Date();
1057
- const start = new Date(options.time_start);
1058
- const end = new Date(options.time_end);
1059
- if (now < start || now > end) {
1060
- return `用户组时间范围限制: ${group}`;
1061
- }
1062
-
1063
- return null;
1064
- };
1065
-
1066
- /**
1067
- * 检查用户组IP白名单权限
1068
- * @param {Object} options 用户组配置
1069
- * @param {Object} user 用户
1070
- * @returns {string} 结果
1071
- */
1072
- Power.prototype._checkGroupIp = function (options, user) {
1073
- if (!options.ip_whitelist || !user.ip) {
1074
- return null;
1075
- }
1076
-
1077
- const allowed_ips = options.ip_whitelist;
1078
- if (!allowed_ips.includes(user.ip)) {
1079
- return `IP不在白名单内: ${user.ip}`;
1080
- }
1081
-
1082
- return null;
1083
- };
1084
-
1085
- /**
1086
- * 检查路由权限
1087
- * @param {Object} user 用户
1088
- * @param {string} path 路径
1089
- * @param {string} method 方法
1090
- * @returns {string} 结果
1091
- */
1092
- Power.prototype._findRouteConfig = function (path) {
1093
- for (const route_path in this._routes) {
1094
- if (this._matchRoute(path, route_path)) {
1095
- return this._routes[route_path];
1096
- }
1097
- }
1098
- return null;
1099
- };
1100
-
1101
- Power.prototype._checkMethod = function (route_cfg, method) {
1102
- const method_cfg = route_cfg.methods || {};
1103
- const allowed_methods = method_cfg[method.toUpperCase()] || method_cfg['*'];
1104
-
1105
- if (!allowed_methods) {
1106
- return `方法不允许: ${method}`;
1107
- }
1108
- return allowed_methods;
1109
- };
1110
-
1111
- Power.prototype._checkRole = function (user, allowed_methods) {
1112
- if (!allowed_methods.roles) {
1113
- return null;
1114
- }
1115
-
1116
- const req_roles = Array.isArray(allowed_methods.roles)
1117
- ? allowed_methods.roles
1118
- : [allowed_methods.roles];
1119
-
1120
- for (const role of req_roles) {
1121
- if (this.checkRole(user, role) === '') {
1122
- return null;
1123
- }
1124
- }
1125
-
1126
- return `缺少所需角色权限: ${req_roles.join(', ')}`;
1127
- };
1128
-
1129
- Power.prototype._checkGroup = function (user, allowed_methods) {
1130
- if (!allowed_methods.groups) {
1131
- return null;
1132
- }
1133
-
1134
- const req_groups = Array.isArray(allowed_methods.groups)
1135
- ? allowed_methods.groups
1136
- : [allowed_methods.groups];
1137
-
1138
- for (const group of req_groups) {
1139
- if (this.checkGroup(user, group) === '') {
1140
- return null;
1141
- }
1142
- }
1143
-
1144
- return `缺少所需用户组权限: ${req_groups.join(', ')}`;
1145
- };
1146
-
1147
- Power.prototype._checkAdminPerm = function (user, allowed_methods) {
1148
- if (!allowed_methods.admins) {
1149
- return null;
1150
- }
1151
-
1152
- const req_admins = Array.isArray(allowed_methods.admins)
1153
- ? allowed_methods.admins
1154
- : [allowed_methods.admins];
1155
-
1156
- for (const admin of req_admins) {
1157
- if (this.checkAdmin(user, admin) === '') {
1158
- return null;
1159
- }
1160
- }
1161
-
1162
- return `缺少所需管理组权限: ${req_admins.join(', ')}`;
1163
- };
1164
-
1165
- Power.prototype._checkDataScope = function (user, allowed_methods) {
1166
- if (!allowed_methods.scope || !user.scope) {
1167
- return null;
1168
- }
1169
-
1170
- const user_scope = user.scope;
1171
- const req_scope = allowed_methods.scope;
1172
-
1173
- if (!this._checkDataScope(user_scope, req_scope)) {
1174
- return '数据范围权限不足';
1175
- }
1176
-
1177
- return null;
1178
- };
1179
-
1180
- Power.prototype.checkRoute = function (user, path, method = 'GET') {
1181
- if (!user) {
1182
- throw new TypeError('用户是必需的');
1183
- }
1184
- if (!path) {
1185
- throw new TypeError('路径是必需的');
1186
- }
1187
-
1188
- // 转换用户数据到内部标准格式
1189
- const std_user = this._transformUser(user);
1190
-
1191
- // 查找路由配置
1192
- const route_cfg = this._findRouteConfig(path);
1193
- if (!route_cfg) {
1194
- return ''; // 无路由配置,默认允许
1195
- }
1196
-
1197
- // 检查方法权限
1198
- const method_result = this._checkMethod(route_cfg, method);
1199
- if (typeof method_result === 'string') {
1200
- return method_result;
1201
- }
1202
- const allowed_methods = method_result;
1203
-
1204
- // 检查所有权限类型
1205
- const result = this._checkAllRoutePerms(std_user, allowed_methods);
1206
- if (result) {
1207
- return result;
1208
- }
1209
-
1210
- return '';
1211
- };
1212
-
1213
- /**
1214
- * 检查所有路由权限
1215
- * @param {Object} user 用户
1216
- * @param {Object} allowed_methods 允许的方法
1217
- * @returns {string} 结果
1218
- */
1219
- Power.prototype._checkAllRoutePerms = function (user, allowed_methods) {
1220
- const checks = [
1221
- () => this._checkRole(user, allowed_methods),
1222
- () => this._checkGroup(user, allowed_methods),
1223
- () => this._checkAdminPerm(user, allowed_methods),
1224
- () => this._checkDataScope(user, allowed_methods)
1225
- ];
1226
-
1227
- for (const check of checks) {
1228
- const result = check();
1229
- if (result) {
1230
- return result;
1231
- }
1232
- }
1233
- return null;
1234
- };
1235
-
1236
- /**
1237
- * 异步检查角色权限
1238
- * @param {Object} user 用户
1239
- * @param {string} role 角色
1240
- * @returns {Promise<string>} 结果
1241
- */
1242
- Power.prototype.checkRoleAsync = async function (user, role) {
1243
- if (!user) {
1244
- throw new TypeError('用户是必需的');
1245
- }
1246
- if (!role) {
1247
- throw new TypeError('角色是必需的');
1248
- }
1249
-
1250
- // 如果user是字符串,则认为是用户ID,需要通过getUserAsync获取用户对象
1251
- let userObj = user;
1252
- if (typeof user === 'string') {
1253
- userObj = await this.getUserAsync(user);
1254
- if (!userObj) {
1255
- return '用户不存在';
1256
- }
1257
- }
1258
-
1259
- // 转换用户数据到内部标准格式
1260
- const std_user = this._transformUser(userObj);
1261
-
1262
- const user_roles = std_user.roles || [];
1263
- if (!user_roles.includes(role)) {
1264
- return `缺少角色权限: ${role}`;
1265
- }
1266
-
1267
- // 异步加载角色配置
1268
- const role_config = await this._loadConfig('roles', role);
1269
- if (role_config) {
1270
- const required_level = role_config.level || 0;
1271
- const user_level = std_user.level || 0;
1272
- if (user_level < required_level) {
1273
- return `角色等级不足: ${role}`;
1274
- }
1275
- }
1276
-
1277
- return '';
1278
- };
1279
-
1280
- /**
1281
- * 异步检查用户组权限
1282
- * @param {Object} user 用户
1283
- * @param {string} group 用户组
1284
- * @returns {Promise<string>} 结果
1285
- */
1286
- Power.prototype.checkGroupAsync = async function (user, group) {
1287
- if (!user) {
1288
- throw new TypeError('用户是必需的');
1289
- }
1290
- if (!group) {
1291
- throw new TypeError('用户组是必需的');
1292
- }
1293
-
1294
- // 如果user是字符串,则认为是用户ID,需要通过getUserAsync获取用户对象
1295
- let userObj = user;
1296
- if (typeof user === 'string') {
1297
- userObj = await this.getUserAsync(user);
1298
- if (!userObj) {
1299
- return '用户不存在';
1300
- }
1301
- }
1302
-
1303
- // 转换用户数据到内部标准格式
1304
- const std_user = this._transformUser(userObj);
1305
-
1306
- const user_groups = std_user.groups || [];
1307
- if (!user_groups.includes(group)) {
1308
- return `缺少用户组权限: ${group}`;
1309
- }
1310
-
1311
- // 异步加载用户组配置
1312
- const options = await this._loadConfig('groups', group);
1313
- if (options) {
1314
- return await this._checkGrpDtlAsync(options, std_user, group);
1315
- }
1316
-
1317
- return '';
1318
- };
1319
-
1320
- /**
1321
- * 异步检查用户组详细权限
1322
- * @param {Object} options 用户组配置
1323
- * @param {Object} std_user 标准用户对象
1324
- * @param {string} group 用户组
1325
- * @returns {Promise<string>} 结果
1326
- */
1327
- Power.prototype._checkGrpDtlAsync = async function (options, std_user, group) {
1328
- const time_result = await this._checkGroupTimeAsync(options, group);
1329
- if (time_result) {
1330
- return time_result;
1331
- }
1332
-
1333
- const ip_result = this._checkGroupIpAsync(options, std_user);
1334
- if (ip_result) {
1335
- return ip_result;
1336
- }
1337
-
1338
- return '';
1339
- };
1340
-
1341
- /**
1342
- * 异步检查用户组时间范围权限
1343
- * @param {Object} options 用户组配置
1344
- * @param {string} group 用户组
1345
- * @returns {Promise<string>} 结果
1346
- */
1347
- Power.prototype._checkGroupTimeAsync = async function (options, group) {
1348
- // 检查是否存在时间配置
1349
- if (!options.time_start && !options.time_end) {
1350
- return '';
1351
- }
1352
-
1353
- const now = new Date();
1354
- const start = options.time_start ? new Date(options.time_start) : null;
1355
- const end = options.time_end ? new Date(options.time_end) : null;
1356
-
1357
- // 检查开始时间
1358
- if (start && now < start) {
1359
- return `用户组时间范围限制: ${group}`;
1360
- }
1361
-
1362
- // 检查结束时间
1363
- if (end && now > end) {
1364
- return `用户组时间范围限制: ${group}`;
1365
- }
1366
-
1367
- return '';
1368
- };
1369
-
1370
- /**
1371
- * 异步检查用户组IP白名单权限
1372
- * @param {Object} options 用户组配置
1373
- * @param {Object} user 用户
1374
- * @returns {Promise<string|null>} 结果
1375
- */
1376
- Power.prototype._checkGroupIpAsync = async function (options, user) {
1377
- if (!options.ip_whitelist || !user.ip) {
1378
- return '';
1379
- }
1380
-
1381
- const allowed_ips = options.ip_whitelist;
1382
- if (!allowed_ips.includes(user.ip)) {
1383
- return `IP不在白名单内: ${user.ip}`;
1384
- }
1385
-
1386
- return '';
1387
- };
1388
-
1389
- /**
1390
- * 异步检查路由权限
1391
- * @param {Object} user 用户
1392
- * @param {string} path 路径
1393
- * @param {string} method 方法
1394
- * @returns {Promise<string>} 结果
1395
- */
1396
- Power.prototype._checkRoleAsync = async function (user, allowed_methods) {
1397
- if (!allowed_methods.roles) {
1398
- return null;
1399
- }
1400
-
1401
- const req_roles = Array.isArray(allowed_methods.roles)
1402
- ? allowed_methods.roles
1403
- : [allowed_methods.roles];
1404
-
1405
- for (const role of req_roles) {
1406
- const result = await this.checkRoleAsync(user, role);
1407
- if (result === '') {
1408
- return null;
1409
- }
1410
- }
1411
-
1412
- return `缺少所需角色权限: ${req_roles.join(', ')}`;
1413
- };
1414
-
1415
- Power.prototype._checkGroupAsync = async function (user, allowed_methods) {
1416
- if (!allowed_methods.groups) {
1417
- return null;
1418
- }
1419
-
1420
- const req_groups = Array.isArray(allowed_methods.groups)
1421
- ? allowed_methods.groups
1422
- : [allowed_methods.groups];
1423
-
1424
- for (const group of req_groups) {
1425
- const result = await this.checkGroupAsync(user, group);
1426
- if (result === '') {
1427
- return null;
1428
- }
1429
- }
1430
-
1431
- return `缺少所需用户组权限: ${req_groups.join(', ')}`;
1432
- };
1433
-
1434
- Power.prototype._checkAdminAsyncPerm = async function (user, allowed_methods) {
1435
- if (!allowed_methods.admins) {
1436
- return null;
1437
- }
1438
-
1439
- const req_admins = Array.isArray(allowed_methods.admins)
1440
- ? allowed_methods.admins
1441
- : [allowed_methods.admins];
1442
-
1443
- for (const admin of req_admins) {
1444
- const result = await this.checkAdminAsync(user, admin);
1445
- if (result === '') {
1446
- return null;
1447
- }
1448
- }
1449
-
1450
- return `缺少所需管理组权限: ${req_admins.join(', ')}`;
1451
- };
1452
-
1453
- Power.prototype.checkRouteAsync = async function (user, path, method = 'GET') {
1454
- if (!user) {
1455
- throw new TypeError('用户是必需的');
1456
- }
1457
- if (!path) {
1458
- throw new TypeError('路径是必需的');
1459
- }
1460
-
1461
- // 如果user是字符串,则认为是用户ID,需要通过getUserAsync获取用户对象
1462
- const userObj = await this._getUserIfNeeded(user);
1463
- if (typeof user === 'string' && !userObj) {
1464
- return '用户不存在';
1465
- }
1466
-
1467
- // 转换用户数据到内部标准格式
1468
- const std_user = this._transformUser(userObj);
1469
-
1470
- // 查找匹配的路由配置
1471
- const route_cfg = this._findRouteConfig(path);
1472
- if (!route_cfg) {
1473
- return ''; // 无路由配置,默认允许
1474
- }
1475
-
1476
- // 检查方法权限
1477
- const method_result = this._checkMethod(route_cfg, method);
1478
- if (typeof method_result === 'string') {
1479
- return method_result;
1480
- }
1481
- const allowed_methods = method_result;
1482
-
1483
- // 异步检查所有权限类型
1484
- const result = await this._checkRouteAsync(std_user, allowed_methods);
1485
- if (result) {
1486
- return result;
1487
- }
1488
-
1489
- return '';
1490
- };
1491
-
1492
- /**
1493
- * 异步检查所有路由权限
1494
- * @param {Object} user 用户
1495
- * @param {Object} allowed_methods 允许的方法
1496
- * @returns {Promise<string>} 结果
1497
- */
1498
- Power.prototype._checkRouteAsync = async function (user, allowed_methods) {
1499
- const checks = [
1500
- () => this._checkRoleAsyncWrap(user, allowed_methods),
1501
- () => this._checkGroupAsyncWrap(user, allowed_methods),
1502
- () => this._checkAdmAsyncPW(user, allowed_methods),
1503
- () => this._checkDataScope(user, allowed_methods)
1504
- ];
1505
-
1506
- for (const check of checks) {
1507
- const result = await check();
1508
- if (result) {
1509
- return result;
1510
- }
1511
- }
1512
- return null;
1513
- };
1514
-
1515
- Power.prototype._checkRoleAsyncWrap = async function (user, allowed_methods) {
1516
- return await this._checkRoleAsync(user, allowed_methods);
1517
- };
1518
-
1519
- Power.prototype._checkGroupAsyncWrap = async function (user, allowed_methods) {
1520
- return await this._checkGroupAsync(user, allowed_methods);
1521
- };
1522
-
1523
- Power.prototype._checkAdmAsyncPW = async function (user, allowed_methods) {
1524
- return await this._checkAdminAsyncPerm(user, allowed_methods);
1525
- };
1526
-
1527
- /**
1528
- * 异步获取用户
1529
- * @param {string} user_id 用户ID
1530
- * @returns {Promise<Object>} 用户
1531
- */
1532
- Power.prototype.getUserAsync = async function (user_id) {
1533
- if (!this._loader) {
1534
- throw new Error('异步加载器未设置');
1535
- }
1536
-
1537
- const cache_key = `user:${user_id}`;
1538
- if (this._cache[cache_key]) {
1539
- return this._cache[cache_key];
1540
- }
1541
-
1542
- const user = await this._loader('user', user_id);
1543
- this._cache[cache_key] = user;
1544
- return user;
1545
- };
1546
-
1547
- /**
1548
- * 异步检查权限
1549
- * @param {string} user_id 用户ID
1550
- * @param {Object} power 权限
1551
- * @param {Function} func 函数
1552
- * @param {number} way 方式
1553
- * @returns {Promise<string>} 结果
1554
- */
1555
- Power.prototype.checkAsync = async function (
1556
- user_id,
1557
- power = {},
1558
- func = null,
1559
- way = 0
1560
- ) {
1561
- const user = await this.getUserAsync(user_id);
1562
- if (!user) {
1563
- return '用户不存在';
1564
- }
1565
-
1566
- const std_user = this._transformUser(user);
1567
- return await this._checkAllAsyncPerms(std_user, power);
1568
- };
1569
-
1570
- /**
1571
- * 异步检查所有权限
1572
- * @param {Object} user 用户
1573
- * @param {Object} power 权限
1574
- * @returns {Promise<string>} 结果
1575
- */
1576
- Power.prototype._checkAllAsyncPerms = async function (user, power) {
1577
- const checks = [
1578
- { method: '_checkAsyncPath', async: true },
1579
- { method: '_checkAsyncRole', async: true },
1580
- { method: '_checkAsyncGroup', async: true },
1581
- { method: '_checkAsyncAdmin', async: true },
1582
- { method: '_checkAsyncPower', async: false }
1583
- ];
1584
-
1585
- for (const check of checks) {
1586
- const result = check.async
1587
- ? await this[check.method](user, power)
1588
- : this[check.method](user, power);
1589
- if (result) {
1590
- return result;
1591
- }
1592
- }
1593
-
1594
- return '';
1595
- };
1596
-
1597
- /**
1598
- * 异步检查路径权限
1599
- * @param {Object} user 用户
1600
- * @param {Object} power 权限
1601
- * @returns {Promise<string>} 结果
1602
- */
1603
- Power.prototype._checkAsyncPath = async function (user, power) {
1604
- if (power.path) {
1605
- const path_result = await this.checkRouteAsync(
1606
- user,
1607
- power.path,
1608
- power.method || 'GET'
1609
- );
1610
- if (path_result !== '') {
1611
- return path_result;
1612
- }
1613
- }
1614
- return null;
1615
- };
1616
-
1617
- /**
1618
- * 异步检查角色权限
1619
- * @param {Object} user 用户
1620
- * @param {Object} power 权限
1621
- * @returns {Promise<string>} 结果
1622
- */
1623
- Power.prototype._checkAsyncRole = async function (user, power) {
1624
- if (power.role) {
1625
- const role_result = await this.checkRoleAsync(user, power.role);
1626
- if (role_result !== '') {
1627
- return role_result;
1628
- }
1629
- }
1630
- return null;
1631
- };
1632
-
1633
- /**
1634
- * 异步检查用户组权限
1635
- * @param {Object} user 用户
1636
- * @param {Object} power 权限
1637
- * @returns {Promise<string>} 结果
1638
- */
1639
- Power.prototype._checkAsyncGroup = async function (user, power) {
1640
- if (power.group) {
1641
- const group_result = await this.checkGroupAsync(user, power.group);
1642
- if (group_result !== '') {
1643
- return group_result;
1644
- }
1645
- }
1646
- return null;
1647
- };
1648
-
1649
- /**
1650
- * 异步检查管理组权限
1651
- * @param {Object} user 用户
1652
- * @param {Object} power 权限
1653
- * @returns {Promise<string>} 结果
1654
- */
1655
- Power.prototype._checkAsyncAdmin = async function (user, power) {
1656
- if (power.admin) {
1657
- const admin_result = await this.checkAdminsAsync(
1658
- user,
1659
- power.admin,
1660
- power.way || 0
1661
- );
1662
- if (admin_result !== '') {
1663
- return admin_result;
1664
- }
1665
- }
1666
- return null;
1667
- };
1668
-
1669
- /**
1670
- * 异步检查字段权限
1671
- * @param {Object} user 用户
1672
- * @param {Object} power 权限
1673
- * @returns {string} 结果
1674
- */
1675
- Power.prototype._checkAsyncPower = function (user, power) {
1676
- if (power.power) {
1677
- const power_result = this.checkField(user, power.power);
1678
- if (power_result !== '') {
1679
- return power_result;
1680
- }
1681
- }
1682
- return null;
1683
- };
1684
-
1685
- /**
1686
- * 匹配路由路径
1687
- * @param {string} request_path 请求路径
1688
- * @param {string} config_path 配置路径
1689
- * @returns {boolean} 是否匹配
1690
- */
1691
- Power.prototype._matchRoute = function (request_path, config_path) {
1692
- if (config_path === '*') return true;
1693
- if (config_path === request_path) return true;
1694
-
1695
- // 支持通配符匹配
1696
- if (config_path.includes('*')) {
1697
- const regex = new RegExp('^' + config_path.replace(/\*/g, '.*') + '$');
1698
- return regex.test(request_path);
1699
- }
1700
-
1701
- return false;
1702
- };
1703
-
1704
- /**
1705
- * 检查数据范围权限
1706
- * @param {Object} user_scope 用户数据范围
1707
- * @param {Object} required_scope 要求数据范围
1708
- * @returns {boolean} 是否有权限
1709
- */
1710
- Power.prototype._checkDataScope = function (user_scope, required_scope) {
1711
- // 检查部门权限
1712
- if (required_scope.dept_ids) {
1713
- const user_depts = user_scope.dept_ids || [];
1714
- const required_depts = Array.isArray(required_scope.dept_ids)
1715
- ? required_scope.dept_ids
1716
- : [required_scope.dept_ids];
1717
-
1718
- let has_dept = false;
1719
- for (const dept of required_depts) {
1720
- if (user_depts.includes(dept)) {
1721
- has_dept = true;
1722
- break;
1723
- }
1724
- }
1725
- if (!has_dept) return false;
1726
- }
1727
-
1728
- // 检查数据权限级别
1729
- if (required_scope.level && user_scope.level) {
1730
- if (user_scope.level < required_scope.level) {
1731
- return false;
1732
- }
1733
- }
1734
-
1735
- return true;
1736
- };
1737
-
1738
- /**
1739
- * 获取路由允许的用户组
1740
- * @param {string} path 路径
1741
- * @param {string} method 方法
1742
- * @returns {Array} 允许的用户组列表
1743
- */
1744
- Power.prototype.getRouteGroups = function (path, method = 'GET') {
1745
- let route_config = null;
1746
- for (const route_path in this._routes) {
1747
- if (this._matchRoute(path, route_path)) {
1748
- route_config = this._routes[route_path];
1749
- break;
1750
- }
1751
- }
1752
-
1753
- if (!route_config) {
1754
- return [];
1755
- }
1756
-
1757
- const method_config = route_config.methods || {};
1758
- const allowed_methods =
1759
- method_config[method.toUpperCase()] || method_config['*'];
1760
-
1761
- if (!allowed_methods || !allowed_methods.groups) {
1762
- return [];
1763
- }
1764
-
1765
- return Array.isArray(allowed_methods.groups)
1766
- ? allowed_methods.groups
1767
- : [allowed_methods.groups];
1768
- };
1769
-
1770
- /**
1771
- * 获取路由允许的角色
1772
- * @param {string} path 路径
1773
- * @param {string} method 方法
1774
- * @returns {Array} 允许的角色列表
1775
- */
1776
- Power.prototype.getRouteRoles = function (path, method = 'GET') {
1777
- let route_config = null;
1778
- for (const route_path in this._routes) {
1779
- if (this._matchRoute(path, route_path)) {
1780
- route_config = this._routes[route_path];
1781
- break;
1782
- }
1783
- }
1784
-
1785
- if (!route_config) {
1786
- return [];
1787
- }
1788
-
1789
- const method_config = route_config.methods || {};
1790
- const allowed_methods =
1791
- method_config[method.toUpperCase()] || method_config['*'];
1792
-
1793
- if (!allowed_methods || !allowed_methods.roles) {
1794
- return [];
1795
- }
1796
-
1797
- return Array.isArray(allowed_methods.roles)
1798
- ? allowed_methods.roles
1799
- : [allowed_methods.roles];
1800
- };
1801
-
1802
- /**
1803
- * 综合权限检查
1804
- * @param {Object} user 用户
1805
- * @param {Object} options 选项
1806
- * @returns {string} 结果
1807
- */
1808
- Power.prototype.checkAll = function (user, options = {}) {
1809
- if (!user) {
1810
- throw new TypeError('user is required');
1811
- }
1812
-
1813
- const {
1814
- path,
1815
- method = 'GET',
1816
- role,
1817
- group,
1818
- admin,
1819
- power = {},
1820
- func = null,
1821
- way = 0
1822
- } = options;
1823
-
1824
- // 检查所有权限类型
1825
- const result = this._checkAllPerms(user, {
1826
- path,
1827
- method,
1828
- role,
1829
- group,
1830
- admin,
1831
- power,
1832
- func,
1833
- way
1834
- });
1835
- if (result) {
1836
- return result;
1837
- }
1838
-
1839
- return '';
1840
- };
1841
-
1842
- /**
1843
- * 检查所有权限类型
1844
- * @param {Object} user 用户
1845
- * @param {Object} options 选项
1846
- * @returns {string} 结果
1847
- */
1848
- Power.prototype._checkAllPerms = function (user, options) {
1849
- const checks = [
1850
- () => this._checkRoutePerm(user, options),
1851
- () => this._checkRolePerm(user, options),
1852
- () => this._checkGroupPerm(user, options),
1853
- () => this._checkAdminPerm(user, options),
1854
- () => this._checkFieldPerm(user, options)
1855
- ];
1856
-
1857
- for (const check of checks) {
1858
- const result = check();
1859
- if (result) {
1860
- return result;
1861
- }
1862
- }
1863
- return null;
1864
- };
1865
-
1866
- /**
1867
- * 检查路由权限
1868
- * @param {Object} user 用户
1869
- * @param {Object} options 选项
1870
- * @returns {string} 结果
1871
- */
1872
- Power.prototype._checkRoutePerm = function (user, options) {
1873
- if (options.path) {
1874
- const route_result = this.checkRoute(user, options.path, options.method);
1875
- if (route_result) {
1876
- return route_result;
1877
- }
1878
- }
1879
- return null;
1880
- };
1881
-
1882
- /**
1883
- * 检查角色权限
1884
- * @param {Object} user 用户
1885
- * @param {Object} options 选项
1886
- * @returns {string} 结果
1887
- */
1888
- Power.prototype._checkRolePerm = function (user, options) {
1889
- if (options.role) {
1890
- const role_result = this.checkRole(user, options.role);
1891
- if (role_result) {
1892
- return role_result;
1893
- }
1894
- }
1895
- return null;
1896
- };
1897
-
1898
- /**
1899
- * 检查用户组权限
1900
- * @param {Object} user 用户
1901
- * @param {Object} options 选项
1902
- * @returns {string} 结果
1903
- */
1904
- Power.prototype._checkGroupPerm = function (user, options) {
1905
- if (options.group) {
1906
- const group_result = this.checkGroup(user, options.group);
1907
- if (group_result) {
1908
- return group_result;
1909
- }
1910
- }
1911
- return null;
1912
- };
1913
-
1914
- /**
1915
- * 检查管理组权限
1916
- * @param {Object} user 用户
1917
- * @param {Object} options 选项
1918
- * @returns {string} 结果
1919
- */
1920
- Power.prototype._checkAdminPerm = function (user, options) {
1921
- if (options.admin) {
1922
- const admin_result = this.checkAdmins(user, options.admin, options.way);
1923
- if (admin_result) {
1924
- return admin_result;
1925
- }
1926
- }
1927
- return null;
1928
- };
1929
-
1930
- /**
1931
- * 检查字段权限
1932
- * @param {Object} user 用户
1933
- * @param {Object} options 选项
1934
- * @returns {string} 结果
1935
- */
1936
- Power.prototype._checkFieldPerm = function (user, options) {
1937
- const field_result = this.checkField(
1938
- user,
1939
- options.power,
1940
- options.func,
1941
- options.way
1942
- );
1943
- if (field_result) {
1944
- return field_result;
1945
- }
1946
- return null;
1947
- };
1948
-
1949
- module.exports = { Power };
1
+ const Admin = require('mm_corejs').Admin;
2
+ const Power = require('./lib/power.js');
3
+
4
+ const admin = new Admin();
5
+ admin.setClass(Power);
6
+
7
+ module.exports = {
8
+ Power,
9
+ admin,
10
+ powerAdmin(scope, config) {
11
+ if (!scope) {
12
+ scope = 'os';
13
+ }
14
+ return admin.get(scope, config);
15
+ },
16
+ getPower() {
17
+ return admin.get('os');
18
+ }
19
+ }