not-node 6.5.21 → 6.5.22

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.5.21",
3
+ "version": "6.5.22",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -135,4 +135,4 @@
135
135
  ]
136
136
  }
137
137
  }
138
- }
138
+ }
package/src/common.js CHANGED
@@ -486,3 +486,13 @@ const findSignature = (obj, signatures, strict = true, typeStrict = true) => {
486
486
  });
487
487
  };
488
488
  module.exports.findSignature = findSignature;
489
+
490
+ const isAction = (action) =>
491
+ action &&
492
+ (typeof action.run === "function" || typeof action === "function");
493
+
494
+ module.exports.isAction = isAction;
495
+
496
+ const isArrayOfActions = (list) => list.every(isAction);
497
+
498
+ module.exports.isArrayOfActions = isArrayOfActions;
@@ -4,5 +4,6 @@ const ActionsBefore = new ActionsSetsLibrary();
4
4
 
5
5
  ActionsBefore.add("ownage", require("./ownage"));
6
6
  ActionsBefore.add("standartQueries", require("./standart.queries.js"));
7
+ ActionsBefore.add("populate", require("./populate"));
7
8
 
8
9
  module.exports = ActionsBefore;
@@ -1,5 +1,8 @@
1
1
  const notFilter = require("not-filter");
2
- const { DOCUMENT_OWNER_FIELD_NAME } = require("../../../auth/const.js");
2
+ const {
3
+ DOCUMENT_OWNER_FIELD_NAME,
4
+ DOCUMENT_SESSION_FIELD_NAME,
5
+ } = require("../../../auth/const.js");
3
6
  const {
4
7
  OwnageExceptionIdentityUserIdIsNotDefined,
5
8
  } = require("../../../exceptions/action.js");
@@ -8,50 +11,107 @@ const ModelRoutine = require("../../../model/routine.js");
8
11
  //checks that
9
12
  module.exports = class OwnageBeforeAction {
10
13
  static #ownerFieldName = DOCUMENT_OWNER_FIELD_NAME;
14
+ static #sessionFieldName = DOCUMENT_SESSION_FIELD_NAME;
11
15
 
12
16
  static get ownerFieldName() {
13
17
  return this.#ownerFieldName;
14
18
  }
15
19
 
16
- static async run(logic, actionName, args) {
17
- const { identity, data, query, targetId, targetID } = args;
18
- if (identity.uid) {
19
- //if searching, counting, listing and so on
20
- //adding condition of ownership by this excat user
21
- let { filter, search } = query;
22
- if (filter) {
23
- filter = notFilter.filter.modifyRules(filter, {
24
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
25
- });
26
- if (search) {
27
- search = notFilter.filter.modifyRules(search, filter);
28
- }
20
+ static get sessionFieldName() {
21
+ return this.#sessionFieldName;
22
+ }
23
+
24
+ static setOwnage(logic, actionName, args, ownageFilter) {
25
+ const { query, targetId, targetID } = args;
26
+ let { filter, search } = query;
27
+ if (filter) {
28
+ filter = notFilter.filter.modifyRules(filter, ownageFilter);
29
+ if (search) {
30
+ search = notFilter.filter.modifyRules(search, filter);
29
31
  }
30
- args.defaultQueryById = {
31
- _id: targetId,
32
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
32
+ }
33
+ args.defaultQueryById = {
34
+ _id: targetId,
35
+ ...ownageFilter,
36
+ };
37
+ const Model = logic.getModel();
38
+ const incFieldName = ModelRoutine.incremental(Model);
39
+ if (incFieldName) {
40
+ args.defaultQueryByID = {
41
+ [incFieldName]: targetID,
42
+ ...ownageFilter,
33
43
  };
34
- const Model = logic.getModel();
35
- const incFieldName = ModelRoutine.incremental(Model);
36
- if (incFieldName) {
37
- args.defaultQueryByID = {
38
- [incFieldName]: targetID,
39
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
40
- };
41
- }
44
+ }
42
45
 
43
- args.defaultQueryMany = {
44
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
45
- };
46
- //mark data as owned by
47
- if (data) {
48
- data[OwnageBeforeAction.ownerFieldName] = identity.uid;
49
- }
46
+ args.defaultQueryMany = {
47
+ ...ownageFilter,
48
+ };
49
+ //mark data as owned by
50
+ if (typeof args.data == "object" && args.data) {
51
+ Object.assign(args.data, ownageFilter);
52
+ }
53
+ }
54
+
55
+ static createOwnageFilterForUser(identity) {
56
+ return Object.freeze({
57
+ [OwnageBeforeAction.ownerFieldName]: identity.uid,
58
+ });
59
+ }
60
+
61
+ static createOwnageFilterForSession(identity) {
62
+ return Object.freeze({
63
+ [OwnageBeforeAction.sessionFieldName]: identity.sid,
64
+ });
65
+ }
66
+
67
+ /**
68
+ * Returns object with filtering conditions to restrict access by owner or session
69
+ *
70
+ * @static
71
+ * @param {import('../../../types.js').notAppIdentityData} identity
72
+ * @return {object}
73
+ */
74
+ static getOwnageFilterForIdentity(identity) {
75
+ if (identity.uid) {
76
+ return OwnageBeforeAction.createOwnageFilterForUser(identity);
77
+ } else if (identity.sid) {
78
+ return OwnageBeforeAction.createOwnageFilterForSession(identity);
50
79
  } else {
80
+ return null;
81
+ }
82
+ }
83
+
84
+ /**
85
+ *
86
+ *
87
+ * @static
88
+ * @param {import('../../logic.js')} logic
89
+ * @param {String} actionName
90
+ * @param {import('../../../types.js').PreparedData} args
91
+ */
92
+ static async run(logic, actionName, args) {
93
+ if (!args.identity) {
51
94
  throw new OwnageExceptionIdentityUserIdIsNotDefined(
52
95
  actionName,
53
- identity
96
+ undefined
54
97
  );
55
98
  }
99
+ const ownageFilter = OwnageBeforeAction.getOwnageFilterForIdentity(
100
+ args.identity
101
+ );
102
+ if (ownageFilter === null) {
103
+ throw new OwnageExceptionIdentityUserIdIsNotDefined(
104
+ actionName,
105
+ args.identity
106
+ );
107
+ }
108
+ OwnageBeforeAction.setOwnage(logic, actionName, args, ownageFilter);
109
+ }
110
+
111
+ static ifActionNameEndsWith_Own() {
112
+ return Object.freeze({
113
+ condition: (actionName) => actionName.endsWith("Own"),
114
+ action: OwnageBeforeAction,
115
+ });
56
116
  }
57
117
  };
@@ -1,3 +1,12 @@
1
1
  module.exports = class PopulateBeforeAction {
2
- static async run(/*logic, actionName, args*/) {}
2
+ static async run(logic, actionName, args) {
3
+ try {
4
+ const { identity } = args;
5
+ args.populate = await logic.getPopulate(actionName, {
6
+ identity,
7
+ });
8
+ } catch (e) {
9
+ logic.log.error(e);
10
+ }
11
+ }
3
12
  };
@@ -5,9 +5,11 @@ module.exports = class StandartQueriesBeforeAction {
5
5
  static async run(logic, actionName, args) {
6
6
  try {
7
7
  const { targetId, targetID } = args;
8
- args.defaultQueryById = {
9
- _id: targetId,
10
- };
8
+ if (targetId) {
9
+ args.defaultQueryById = {
10
+ _id: targetId,
11
+ };
12
+ }
11
13
  const Model = logic.getModel();
12
14
  const incFieldName = ModelRoutine.incremental(Model);
13
15
  if (
@@ -11,8 +11,15 @@ module.exports = ({
11
11
  actionsSets = ["standart"],
12
12
  actions = {},
13
13
  beforeActions = {},
14
- beforeActionsAll = [require("./actions.before/standart.queries.js")],
14
+ beforeActionsOnCondition = [
15
+ require("./actions.before/ownage/ownage.js").ifActionNameEndsWith_Own(),
16
+ ], //each item = {condition: (actionName)=>boolean, action}
17
+ beforeActionsAll = [
18
+ require("./actions.before/standart.queries.js"),
19
+ require("./actions.before/populate/populate.js"),
20
+ ],
15
21
  afterActions = {},
22
+ afterActionsOnCondition = [], //each item = {condition: (actionName)=>boolean, action}
16
23
  populateBuilders = {},
17
24
  afterActionsAll = [],
18
25
  defaultPopulate = [],
@@ -36,11 +43,28 @@ module.exports = ({
36
43
  defaultPopulate,
37
44
  });
38
45
 
46
+ //adds before/after to all
39
47
  beforeActionsAll.forEach((action) => {
40
48
  Logic.onBefore(undefined, action);
41
49
  });
50
+
42
51
  afterActionsAll.forEach((action) => Logic.onAfter(undefined, action));
43
52
 
53
+ //adds before/after to as many as satisfies condition
54
+ Object.keys(ACTIONS).forEach((actionName) => {
55
+ beforeActionsOnCondition.forEach(({ condition, action }) => {
56
+ if (condition(actionName)) {
57
+ Logic.onBefore(actionName, action);
58
+ }
59
+ });
60
+ afterActionsOnCondition.forEach(({ condition, action }) => {
61
+ if (condition(actionName)) {
62
+ Logic.onAfter(actionName, action);
63
+ }
64
+ });
65
+ });
66
+
67
+ //adds before/after per actionName
44
68
  Object.keys(beforeActions).forEach((actionName) =>
45
69
  beforeActions[actionName].forEach((action) =>
46
70
  Logic.onBefore(actionName, action)
@@ -17,8 +17,8 @@ class LogicProxied {
17
17
  actions = new Map();
18
18
  actionRunner = ActionRunner;
19
19
 
20
- populateBuilders = {};
21
- defaultPopulate = [];
20
+ #populateBuilders = {};
21
+ #defaultPopulate = [];
22
22
 
23
23
  MODEL_NAME;
24
24
  MODULE_NAME;
@@ -32,7 +32,6 @@ class LogicProxied {
32
32
  logAction;
33
33
  logDebugAction;
34
34
 
35
-
36
35
  constructor(
37
36
  actions = {},
38
37
  actionRunner = ActionRunner,
@@ -48,9 +47,15 @@ class LogicProxied {
48
47
  this.MODEL_NAME = MODEL_NAME;
49
48
  this.MODULE_NAME = MODULE_NAME;
50
49
  this.USER_MODEL_NAME = USER_MODEL_NAME;
50
+ if (defaultPopulate) {
51
+ this.#defaultPopulate = defaultPopulate;
52
+ }
53
+ Object.freeze(this.#defaultPopulate);
51
54
 
52
- defaultPopulate && (this.defaultPopulate = defaultPopulate);
53
- populateBuilders && (this.populateBuilders = populateBuilders);
55
+ if (populateBuilders) {
56
+ this.#populateBuilders = populateBuilders;
57
+ }
58
+ Object.freeze(this.#populateBuilders);
54
59
 
55
60
  actionRunner && (this.actionRunner = actionRunner);
56
61
  this.afterPipes = new NamedActionPipes(
@@ -70,7 +75,7 @@ class LogicProxied {
70
75
 
71
76
  // proxy logic, do something before each call of all methods inside class
72
77
  // like if arg passed is 3, print something additionally
73
- return this.#proxy = new Proxy(this, {
78
+ return (this.#proxy = new Proxy(this, {
74
79
  get(target, prop) {
75
80
  if (target.actions.has(prop)) {
76
81
  return target.#getActionRunner(prop);
@@ -78,7 +83,7 @@ class LogicProxied {
78
83
  return target[prop];
79
84
  }
80
85
  },
81
- });
86
+ }));
82
87
  }
83
88
 
84
89
  #initTools(target) {
@@ -105,9 +110,7 @@ class LogicProxied {
105
110
  this.log &&
106
111
  this.log.debug(
107
112
  new Date(),
108
- `${this.MODULE_NAME}//Logic//${
109
- this.MODEL_NAME
110
- }//${action}`,
113
+ `${this.MODULE_NAME}//Logic//${this.MODEL_NAME}//${action}`,
111
114
  identity?.ip,
112
115
  identity?.root
113
116
  );
@@ -132,16 +135,16 @@ class LogicProxied {
132
135
 
133
136
  async getPopulate(actionName, prepared) {
134
137
  if (
135
- this.populateBuilders &&
136
- objHas(this.populateBuilders, actionName) &&
137
- isFunc(this.populateBuilders[actionName])
138
+ this.#populateBuilders &&
139
+ objHas(this.#populateBuilders, actionName) &&
140
+ isFunc(this.#populateBuilders[actionName])
138
141
  ) {
139
142
  return await executeFunctionAsAsync(
140
- this.populateBuilders[actionName],
143
+ this.#populateBuilders[actionName],
141
144
  [prepared]
142
145
  );
143
146
  }
144
- return this.defaultPopulate;
147
+ return [...this.#defaultPopulate];
145
148
  }
146
149
 
147
150
  #getActionRunner(actionName) {