free-be-account 0.0.27 → 0.0.29

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/index.js +31 -8
  2. package/package.json +1 -1
  3. package/utils.js +33 -0
package/index.js CHANGED
@@ -27,16 +27,15 @@ try {
27
27
  let __app_service_list_saved = false;
28
28
  let __saved_service_list;
29
29
 
30
- const __getServiceList = async (res, filter = { Enabled: true }, scopeFilter) => {
30
+ const __getServiceList = async (res, filter, scopeFilter) => {
31
31
  // add app.serviceList into db if not yet
32
32
  if (!__app_service_list_saved) {
33
33
  await res.app.modules.account.utils.saveServiceList(res.app);
34
- __app_service_list_saved = true;
35
- } else if (!filter){
34
+ } else if (!filter) {
36
35
  return __saved_service_list;
37
36
  }
38
37
 
39
- const allPerms = await res.app.models.permission.find(filter).lean();
38
+ const allPerms = await res.app.models.permission.find(filter ? { ...filter, Enabled: true } : { Enabled: true }).lean();
40
39
 
41
40
  const permList = {};
42
41
  if (allPerms && allPerms.length > 0) {
@@ -60,7 +59,7 @@ const __getServiceList = async (res, filter = { Enabled: true }, scopeFilter) =>
60
59
  },
61
60
  ((doc.Scope || []).length <= 0) ? {} : {
62
61
  Scope: doc.Scope.map(sc => {
63
- const dso = app.getContainerContent('DataScope').find(ds => ds.Name === sc.Name);
62
+ const dso = res.app.getContainerContent('DataScope').find(ds => ds.Name === sc.Name);
64
63
 
65
64
  if (!dso) {
66
65
  return {};
@@ -97,8 +96,9 @@ const __getServiceList = async (res, filter = { Enabled: true }, scopeFilter) =>
97
96
  })
98
97
  }
99
98
 
100
- if (!filter) {
99
+ if (!filter && !__app_service_list_saved) {
101
100
  __saved_service_list = permList;
101
+ __app_service_list_saved = true;
102
102
  }
103
103
 
104
104
  return permList;
@@ -1041,10 +1041,33 @@ module.exports = (app) => ({
1041
1041
  const uuid = uuidv1();
1042
1042
 
1043
1043
  res.app.cache.put(`captcha_${uuid}`, captcha.text, m.config.captcha.cache || 300000);
1044
- const matches = captcha.data.match(/d="([^"]*)"/g);
1044
+
1045
+ const pathAndStrokeList = [];
1046
+ // 1. 先把每个 <path .../> 整体拎出来
1047
+ const pathRE = /<path\b[^>]*\/>/gi;
1048
+
1049
+ // 2. 对每个标签再抽属性(顺序无关,可缺)
1050
+ const attrRE = /\b(d|stroke|fill)=["']([^"']*)["']/gi;
1051
+
1052
+ let pathMatch;
1053
+
1054
+ while ((pathMatch = pathRE.exec(captcha.data)) !== null) {
1055
+ const obj = { p: undefined, s: undefined, f: undefined };
1056
+ let attrMatch;
1057
+ attrRE.lastIndex = 0; // 重置内层正则
1058
+ while ((attrMatch = attrRE.exec(pathMatch[0])) !== null) {
1059
+ const [, key, val] = attrMatch;
1060
+ if (key === 'd') obj.p = val;
1061
+ if (key === 'stroke') obj.s = val;
1062
+ if (key === 'fill') obj.f = val;
1063
+ }
1064
+
1065
+ pathAndStrokeList.push(obj);
1066
+ }
1045
1067
 
1046
1068
  res.endWithData({
1047
- captcha: matches.map((m) => m.replace('d="', '').replace('"', '')),
1069
+ captcha: pathAndStrokeList,
1070
+ f: m.config.captcha.options.background,
1048
1071
  id: uuid,
1049
1072
  });
1050
1073
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "free-be-account",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "main": "index.js",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
package/utils.js CHANGED
@@ -210,6 +210,38 @@ async function saveServiceList (app, clean=false) {
210
210
  await checkService(serviceList, undefined, '');
211
211
  }
212
212
 
213
+ /**
214
+ * 给指定的静态资源文件添加权限控制记录
215
+ *
216
+ * @param {String or Array} assets asset file(s)
217
+ * @param {String or Object} user The current user
218
+ * @param {String} users Users
219
+ * @param {String} perms Permissions
220
+ * @param {String} refs Referers
221
+ * @returns
222
+ */
223
+ async function addPermControlToAssetFiles(afile, user, users, perms, refs) {
224
+ if (!afile) return;
225
+
226
+ if (!Array.isArray(afile)) {
227
+ afile = [afile];
228
+ }
229
+
230
+ for (let i = 0; i < afile.length; i += 1) {
231
+ const af = afile[i];
232
+
233
+ const assetsFilePath = af.split(/[\\|/]/g).slice(-2).join('/');
234
+ await app.models.staticResourcePermissionControl.create({
235
+ User: user?.id || user,
236
+ ResourcePath: assetsFilePath,
237
+
238
+ Users: users || '',
239
+ Permissions: perms || '',
240
+ Referers: refs || '',
241
+ });
242
+ }
243
+ }
244
+
213
245
  module.exports = {
214
246
  clearPermission,
215
247
  getPermissionPathList,
@@ -218,4 +250,5 @@ module.exports = {
218
250
  getClearPwd,
219
251
  crypto,
220
252
  saveServiceList,
253
+ addPermControlToAssetFiles,
221
254
  }