not-node 5.0.22 → 5.1.0

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 (126) hide show
  1. package/.eslintrc.json +1 -1
  2. package/bin/not-deploy.js +52 -0
  3. package/index.js +21 -19
  4. package/package.json +1 -1
  5. package/src/app.js +61 -58
  6. package/src/auth/abstract.js +17 -19
  7. package/src/auth/const.js +8 -12
  8. package/src/auth/fields.js +143 -124
  9. package/src/auth/index.js +14 -14
  10. package/src/auth/roles.js +64 -63
  11. package/src/auth/routes.js +89 -54
  12. package/src/auth/rules.js +63 -63
  13. package/src/auth/session.js +60 -62
  14. package/src/bootstrap/form.js +13 -13
  15. package/src/bootstrap/logic.js +45 -41
  16. package/src/bootstrap/model.js +14 -17
  17. package/src/bootstrap/route.js +132 -53
  18. package/src/common.js +86 -79
  19. package/src/core/fields/ID.js +6 -6
  20. package/src/core/fields/__closed.js +3 -3
  21. package/src/core/fields/__latest.js +3 -3
  22. package/src/core/fields/__version.js +3 -3
  23. package/src/core/fields/__versions.js +3 -3
  24. package/src/core/fields/_id.js +6 -6
  25. package/src/core/fields/active.js +9 -9
  26. package/src/core/fields/codeName.js +9 -9
  27. package/src/core/fields/createdAt.js +17 -17
  28. package/src/core/fields/default.js +9 -9
  29. package/src/core/fields/description.js +11 -11
  30. package/src/core/fields/email.js +9 -9
  31. package/src/core/fields/enabled.js +9 -9
  32. package/src/core/fields/expiredAt.js +16 -16
  33. package/src/core/fields/height.js +11 -11
  34. package/src/core/fields/ip.js +10 -10
  35. package/src/core/fields/objectId.js +10 -10
  36. package/src/core/fields/owner.js +13 -13
  37. package/src/core/fields/ownerModel.js +11 -11
  38. package/src/core/fields/price.js +11 -11
  39. package/src/core/fields/requiredObject.js +10 -10
  40. package/src/core/fields/session.js +10 -10
  41. package/src/core/fields/size.js +11 -11
  42. package/src/core/fields/telephone.js +9 -9
  43. package/src/core/fields/title.js +15 -15
  44. package/src/core/fields/updatedAt.js +17 -17
  45. package/src/core/fields/userId.js +11 -12
  46. package/src/core/fields/uuid.js +11 -11
  47. package/src/core/fields/validators/email.js +6 -4
  48. package/src/core/fields/validators/owner.js +6 -4
  49. package/src/core/fields/width.js +11 -11
  50. package/src/domain.js +435 -431
  51. package/src/env.js +23 -23
  52. package/src/error.js +20 -23
  53. package/src/exceptions/db.js +23 -0
  54. package/src/exceptions/http.js +43 -0
  55. package/src/fields/index.js +106 -111
  56. package/src/form/fabric.js +19 -24
  57. package/src/form/form.js +195 -186
  58. package/src/form/index.js +2 -2
  59. package/src/generic/index.js +2 -0
  60. package/src/generic/logic.js +595 -0
  61. package/src/generic/route.js +76 -0
  62. package/src/init/additional.js +7 -7
  63. package/src/init/app.js +75 -68
  64. package/src/init/bodyparser.js +14 -14
  65. package/src/init/compression.js +6 -7
  66. package/src/init/core.js +12 -12
  67. package/src/init/cors.js +22 -24
  68. package/src/init/db/index.js +41 -43
  69. package/src/init/db/ioredis.js +28 -20
  70. package/src/init/db/mongoose.js +42 -32
  71. package/src/init/db/redis.js +59 -48
  72. package/src/init/env.js +44 -36
  73. package/src/init/express.js +18 -21
  74. package/src/init/fileupload.js +8 -10
  75. package/src/init/http.js +65 -47
  76. package/src/init/index.js +141 -148
  77. package/src/init/informer.js +7 -9
  78. package/src/init/methodoverride.js +4 -6
  79. package/src/init/middleware.js +23 -26
  80. package/src/init/modules.js +4 -4
  81. package/src/init/monitoring.js +8 -8
  82. package/src/init/rateLimiter.js +53 -45
  83. package/src/init/routes.js +67 -69
  84. package/src/init/security.js +37 -33
  85. package/src/init/sequence.js +111 -107
  86. package/src/init/sequence.standart.js +54 -56
  87. package/src/init/sessions/index.js +21 -21
  88. package/src/init/sessions/mongoose.js +49 -36
  89. package/src/init/sessions/redis.js +28 -27
  90. package/src/init/static.js +53 -40
  91. package/src/init/template.js +17 -17
  92. package/src/lib.js +230 -200
  93. package/src/manifest/batchRunner.js +26 -25
  94. package/src/manifest/initializator/forms.js +24 -24
  95. package/src/manifest/initializator/index.js +8 -10
  96. package/src/manifest/initializator/manifests.js +45 -42
  97. package/src/manifest/initializator/models.js +37 -34
  98. package/src/manifest/manifest.filter.js +130 -97
  99. package/src/manifest/manifest.js +103 -77
  100. package/src/manifest/module.js +350 -360
  101. package/src/manifest/registrator/fields.js +90 -85
  102. package/src/manifest/registrator/forms.js +47 -47
  103. package/src/manifest/registrator/index.js +14 -16
  104. package/src/manifest/registrator/locales.js +17 -16
  105. package/src/manifest/registrator/logics.js +66 -64
  106. package/src/manifest/registrator/models.js +64 -62
  107. package/src/manifest/registrator/routes.js +171 -132
  108. package/src/manifest/registrator/routes.ws.js +109 -103
  109. package/src/manifest/route.js +216 -161
  110. package/src/model/buildValidator.js +53 -42
  111. package/src/model/default.js +304 -286
  112. package/src/model/enrich.js +69 -61
  113. package/src/model/increment.js +124 -137
  114. package/src/model/proto.js +179 -148
  115. package/src/model/routine.js +85 -76
  116. package/src/model/utils.js +33 -0
  117. package/src/model/versioning.js +148 -130
  118. package/src/obsolete.js +12 -8
  119. package/src/parser.js +29 -22
  120. package/src/repos.js +29 -31
  121. package/src/rollup.js +75 -65
  122. package/src/shell.helpers.js +28 -28
  123. package/static.js +31 -0
  124. package/src/generic/router.js +0 -16
  125. package/src/manifest/module.models.js +0 -0
  126. package/src/manifest/module.status.js +0 -0
package/.eslintrc.json CHANGED
@@ -28,7 +28,7 @@
28
28
  "node/prefer-global/url-search-params": ["error", "always"],
29
29
  "node/prefer-global/url": ["error", "always"],
30
30
  "node/no-unpublished-require": "warn",
31
- "indent": ["error", 2],
31
+ "indent": ["error", 4],
32
32
  "linebreak-style": [
33
33
  "error",
34
34
  "unix"
@@ -0,0 +1,52 @@
1
+ /**
2
+
3
+ #!/bin/bash
4
+ cwd
5
+ echo 'deploying to production'
6
+ cd reporter
7
+ npm run production
8
+ cd ../
9
+ echo 'syncing data'
10
+ rsync -av --progress --exclude-from='./deploy/.exclude' reporter cypher@appmon.ru:/var/server/appmon.ru/
11
+ rsync -av --progress deploy/production.json cypher@appmon.ru:/var/server/appmon.ru/deploy/production.json
12
+ ssh cypher@appmon.ru "cd /var/server/appmon.ru/reporter && npm i"
13
+ ssh cypher@appmon.ru "cd /var/server/appmon.ru && pm2 startOrRestart deploy/production.json"
14
+ exit 0;
15
+
16
+ read config
17
+ cycle through remote locations
18
+ deploy as selected method in config say
19
+
20
+ {
21
+ deploy:{
22
+ stage:{},
23
+ production:{
24
+ src:{
25
+ type: "fs", //fs, git
26
+ location: "/var/work/project", //path to dir or url of repo
27
+ include: [], //optional, files to not copy
28
+ exclude: [], //optional, files to not copy
29
+ //will be executed localy
30
+ before-setup: "", //optional, shell cmd
31
+ after-setup: "", //optional, shell cmd
32
+ before: "", //optional, shell cmd
33
+ after: "", //optional, shell cmd
34
+ secret: "./production.env" //optional
35
+ },
36
+ dest:{
37
+ username: "deploy-master-3000", //
38
+ server: ["username@server", "10.0.1.12"], //
39
+ path: "/path/on/server", //
40
+ //will be executed on remote side
41
+ before-setup: "", //optional, shell cmd
42
+ after-setup: "", //optional, shell cmd
43
+ before: "", //optional, shell cmd
44
+ after: "", //optional, shell cmd
45
+ secret: "/path/on/server/where/secret_should_be_copied" //optional
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+
52
+ **/
package/index.js CHANGED
@@ -1,41 +1,43 @@
1
1
  /**
2
- * @module not-node
3
- */
2
+ * @module not-node
3
+ */
4
4
 
5
- module.exports.Env = require('./src/env.js');
5
+ module.exports.Env = require("./src/env.js");
6
6
  /** Error module
7
7
  * @type {not-node/Error}
8
8
  */
9
- module.exports.Error = require('./src/error.js');
9
+ module.exports.Error = require("./src/error.js");
10
10
  /** Authentication module */
11
- module.exports.Auth = require('./src/auth');
11
+ module.exports.Auth = require("./src/auth");
12
12
  /** Manifest infrastructure */
13
- module.exports.Manifest = require('./src/manifest/manifest');
13
+ module.exports.Manifest = require("./src/manifest/manifest");
14
14
  /** Web Application */
15
- module.exports.notApp = require('./src/app');
15
+ module.exports.notApp = require("./src/app");
16
16
  /** General Application */
17
- module.exports.notDomain = require('./src/domain');
17
+ module.exports.notDomain = require("./src/domain");
18
18
  /** Mongoose Documents versioning */
19
- module.exports.Versioning = require('./src/model/versioning');
19
+ module.exports.Versioning = require("./src/model/versioning");
20
20
  /** Mongoose Model autoincrement field */
21
- module.exports.Increment = require('./src/model/increment');
21
+ module.exports.Increment = require("./src/model/increment");
22
22
  /** Mongoose Model prototype */
23
- module.exports.Proto = require('./src/model/proto');
23
+ module.exports.Proto = require("./src/model/proto");
24
24
  /** Mongoose Model additional features enricher */
25
- module.exports.Enrich = require('./src/model/enrich');
25
+ module.exports.Enrich = require("./src/model/enrich");
26
26
  /** Mongoose Documents routine operation*/
27
- module.exports.Routine = require('./src/model/routine');
27
+ module.exports.Routine = require("./src/model/routine");
28
28
  /** Common functions */
29
- module.exports.Common = require('./src/common');
29
+ module.exports.Common = require("./src/common");
30
30
  /** Fields library manager */
31
- module.exports.Fields = require('./src/fields');
31
+ module.exports.Fields = require("./src/fields");
32
32
  /** Form validation template **/
33
- module.exports.Form = require('./src/form').Form;
33
+ module.exports.Form = require("./src/form").Form;
34
34
  /** Form validation template fabric **/
35
- module.exports.FormFabric = require('./src/form').FormFabric;
35
+ module.exports.FormFabric = require("./src/form").FormFabric;
36
36
  /** Application initialization procedures */
37
- module.exports.Init = require('./src/init').Init;
37
+ module.exports.Init = require("./src/init").Init;
38
38
  /** Application object */
39
39
  module.exports.Application = null;
40
40
  /** Application bootstrap helpers */
41
- module.exports.Bootstrap = require('./src/bootstrap');
41
+ module.exports.Bootstrap = require("./src/bootstrap");
42
+ /** Application generic helpers */
43
+ module.exports.Generic = require("./src/generic/index.js");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "5.0.22",
3
+ "version": "5.1.0",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/app.js CHANGED
@@ -1,8 +1,8 @@
1
- const Auth = require('./auth');
2
- const notDomain = require('./domain');
3
- const merge = require('deepmerge');
4
- const parent = require('../index.js');
5
- const {objHas} = require('./common');
1
+ const Auth = require("./auth");
2
+ const notDomain = require("./domain");
3
+ const merge = require("deepmerge");
4
+ const parent = require("../index.js");
5
+ const { objHas } = require("./common");
6
6
 
7
7
  /**
8
8
  * Application
@@ -31,65 +31,68 @@ const {objHas} = require('./common');
31
31
  * .expose(ExpressApp);
32
32
  **/
33
33
  class notApp extends notDomain {
34
- constructor(options) {
35
- super(options);
36
- parent.Application = this;
37
- parent.getModel = this.getModel.bind(this);
38
- parent.getLogic = this.getLogic.bind(this);
39
- parent.getRoute = this.getRoute.bind(this);
40
- parent.getModelFile = this.getModelFile.bind(this);
41
- parent.getModelSchema = this.getModelSchema.bind(this);
42
- parent.getLogicFile = this.getLogicFile.bind(this);
43
- parent.execInModules = this.execInModules.bind(this);
44
- return this;
45
- }
46
-
47
- /**
48
- * Returns application manifest, by ExpressRequest
49
- * @params {object} req Express request object
50
- * @return {object} manifest
51
- **/
52
- getManifest(req) {
53
- const creds = Auth.extractAuthData(req);
54
- return this.collectManifest(creds);
55
- }
34
+ constructor(options) {
35
+ super(options);
36
+ parent.Application = this;
37
+ parent.getModel = this.getModel.bind(this);
38
+ parent.getLogic = this.getLogic.bind(this);
39
+ parent.getRoute = this.getRoute.bind(this);
40
+ parent.getModelFile = this.getModelFile.bind(this);
41
+ parent.getModelSchema = this.getModelSchema.bind(this);
42
+ parent.getLogicFile = this.getLogicFile.bind(this);
43
+ parent.execInModules = this.execInModules.bind(this);
44
+ return this;
45
+ }
56
46
 
57
- /**
58
- * Returns application manifest, by user credentials object
59
- * @params {object} creds not-node Auth.extractAuthData result
60
- * @return {object} manifest
61
- **/
62
- collectManifest(creds){
63
- let manifest = {};
64
- for (let modName of this.getModulesNames()) {
65
- manifest = merge(manifest, this.getModule(modName).getManifest(creds));
47
+ /**
48
+ * Returns application manifest, by ExpressRequest
49
+ * @params {object} req Express request object
50
+ * @return {object} manifest
51
+ **/
52
+ getManifest(req) {
53
+ const creds = Auth.extractAuthData(req);
54
+ return this.collectManifest(creds);
66
55
  }
67
- return manifest;
68
- }
69
56
 
70
- /**
71
- * Exposes routes to ExpressJS application
72
- * @param {object} app ExpressJS application instance
73
- **/
74
- expose(app) {
75
- this.forEachMod((modName, mod)=>{
76
- if (typeof mod.expose === 'function') {
77
- mod.expose(app, modName);
78
- }
79
- });
80
- }
57
+ /**
58
+ * Returns application manifest, by user credentials object
59
+ * @params {object} creds not-node Auth.extractAuthData result
60
+ * @return {object} manifest
61
+ **/
62
+ collectManifest(creds) {
63
+ let manifest = {};
64
+ for (let modName of this.getModulesNames()) {
65
+ manifest = merge(
66
+ manifest,
67
+ this.getModule(modName).getManifest(creds)
68
+ );
69
+ }
70
+ return manifest;
71
+ }
81
72
 
82
- getActionManifestForUser(model, action, user) {
83
- const manifest = this.collectManifest(user);
84
- if(Object.keys(manifest).includes(model)
85
- && objHas(manifest[model], 'actions')
86
- && objHas(manifest[model].actions, action)
87
- ){
88
- return manifest[model].actions[action];
73
+ /**
74
+ * Exposes routes to ExpressJS application
75
+ * @param {object} app ExpressJS application instance
76
+ **/
77
+ expose(app) {
78
+ this.forEachMod((modName, mod) => {
79
+ if (typeof mod.expose === "function") {
80
+ mod.expose(app, modName);
81
+ }
82
+ });
89
83
  }
90
- return false;
91
- }
92
84
 
85
+ getActionManifestForUser(model, action, user) {
86
+ const manifest = this.collectManifest(user);
87
+ if (
88
+ Object.keys(manifest).includes(model) &&
89
+ objHas(manifest[model], "actions") &&
90
+ objHas(manifest[model].actions, action)
91
+ ) {
92
+ return manifest[model].actions[action];
93
+ }
94
+ return false;
95
+ }
93
96
  }
94
97
 
95
98
  module.exports = notApp;
@@ -1,8 +1,7 @@
1
+ const CONST = require("./const");
1
2
 
2
- const CONST = require('./const');
3
-
4
- function isObjectString(val){
5
- return Object.prototype.toString.call(val) === CONST.OBJECT_STRING;
3
+ function isObjectString(val) {
4
+ return Object.prototype.toString.call(val) === CONST.OBJECT_STRING;
6
5
  }
7
6
 
8
7
  /**
@@ -13,23 +12,22 @@ function isObjectString(val){
13
12
  **/
14
13
 
15
14
  function intersect_safe(a, b) {
16
- let result = [];
17
- if (Array.isArray(a) && Array.isArray(b)) {
18
- if (b.length > a.length) {
19
- // indexOf to loop over shorter
20
- let t = b;
21
- b = a;
22
- a = t;
15
+ let result = [];
16
+ if (Array.isArray(a) && Array.isArray(b)) {
17
+ if (b.length > a.length) {
18
+ // indexOf to loop over shorter
19
+ let t = b;
20
+ b = a;
21
+ a = t;
22
+ }
23
+ result = a.filter((e) => {
24
+ if (b.indexOf(e) !== -1) return true;
25
+ });
23
26
  }
24
- result = a.filter((e) => {
25
- if (b.indexOf(e) !== -1) return true;
26
- });
27
- }
28
- return result;
27
+ return result;
29
28
  }
30
29
 
31
-
32
30
  module.exports = {
33
- isObjectString,
34
- intersect_safe
31
+ isObjectString,
32
+ intersect_safe,
35
33
  };
package/src/auth/const.js CHANGED
@@ -1,17 +1,13 @@
1
+ const DEFAULT_USER_ROLE_FOR_ADMIN = "root";
2
+ const DEFAULT_USER_ROLE_FOR_GUEST = "guest";
1
3
 
2
- const DEFAULT_USER_ROLE_FOR_ADMIN = 'root';
3
- const DEFAULT_USER_ROLE_FOR_GUEST = 'guest';
4
+ const OBJECT_STRING = "[object String]";
4
5
 
5
- const OBJECT_STRING = '[object String]';
6
-
7
- const ERR_NOT_AUTHORIZED = 'you_are_not_authorized';
8
-
9
- const DOCUMENT_OWNER_FIELD_NAME = 'ownerId';
6
+ const DOCUMENT_OWNER_FIELD_NAME = "owner";
10
7
 
11
8
  module.exports = {
12
- ERR_NOT_AUTHORIZED,
13
- OBJECT_STRING,
14
- DEFAULT_USER_ROLE_FOR_GUEST,
15
- DEFAULT_USER_ROLE_FOR_ADMIN,
16
- DOCUMENT_OWNER_FIELD_NAME
9
+ OBJECT_STRING,
10
+ DEFAULT_USER_ROLE_FOR_GUEST,
11
+ DEFAULT_USER_ROLE_FOR_ADMIN,
12
+ DOCUMENT_OWNER_FIELD_NAME,
17
13
  };
@@ -1,152 +1,171 @@
1
- const ABSTRACT = require('./abstract');
2
- const COMMON = require('../common');
3
- const CONST = require('./const');
4
- const {objHas} = require('../common');
1
+ const ABSTRACT = require("./abstract");
2
+ const COMMON = require("../common");
3
+ const CONST = require("./const");
4
+ const { objHas } = require("../common");
5
5
 
6
6
  /**
7
- * Get data owner ObjectId
8
- * @param {Object} data Document Object
9
- * @return {ObjectId|undefined} owner ObjectId or undefined if field is not found
10
- */
11
- function getOwnerId(data) {
12
- if(typeof data !== 'object'){return undefined;}
13
- if(
14
- objHas(data, CONST.DOCUMENT_OWNER_FIELD_NAME) &&
15
- data[CONST.DOCUMENT_OWNER_FIELD_NAME] &&
16
- COMMON.validateObjectId(data[CONST.DOCUMENT_OWNER_FIELD_NAME].toString())
17
- ){
18
- return data[CONST.DOCUMENT_OWNER_FIELD_NAME];
19
- }
20
- return undefined;
7
+ * Get data owner ObjectId
8
+ * @param {Object} data Document Object
9
+ * @return {ObjectId|undefined} owner ObjectId or undefined if field is not found
10
+ */
11
+ function getOwnerId(data, ownerFieldName = CONST.DOCUMENT_OWNER_FIELD_NAME) {
12
+ if (typeof data !== "object") {
13
+ return undefined;
14
+ }
15
+ if (
16
+ objHas(data, ownerFieldName) &&
17
+ data[ownerFieldName] &&
18
+ COMMON.validateObjectId(data[ownerFieldName].toString())
19
+ ) {
20
+ return data[ownerFieldName];
21
+ }
22
+ return undefined;
21
23
  }
22
24
 
23
25
  /**
24
- * Check if data is belongs to user
25
- * @param {Object} data object
26
- * @param {ObjectId} user_id possible owner
27
- * @return {boolean} true - belongs, false - not belongs
28
- **/
26
+ * Check if data is belongs to user
27
+ * @param {Object} data object
28
+ * @param {ObjectId} user_id possible owner
29
+ * @return {boolean} true - belongs, false - not belongs
30
+ **/
29
31
 
30
- function isOwner(data, user_id) {
31
- const ownerId = getOwnerId(data);
32
- return COMMON.compareObjectIds(ownerId, user_id);
32
+ function isOwner(
33
+ data,
34
+ user_id,
35
+ ownerFieldName = CONST.DOCUMENT_OWNER_FIELD_NAME
36
+ ) {
37
+ const ownerId = getOwnerId(data, ownerFieldName);
38
+ return COMMON.compareObjectIds(ownerId, user_id);
33
39
  }
34
40
 
35
41
  /**
36
- * Checks if safe field rule is some sort variant of wild card
37
- * @param {string|Array<string>} safeFor safe field rule
38
- * @return {boolean} if it's wildcard
39
- *
40
- */
41
- function ruleIsWildcard(safeFor){
42
- if(Array.isArray(safeFor)){
43
- return safeFor.includes('*');
44
- }else{
45
- return (safeFor === '*');
46
- }
42
+ * Checks if safe field rule is some sort variant of wild card
43
+ * @param {string|Array<string>} safeFor safe field rule
44
+ * @return {boolean} if it's wildcard
45
+ *
46
+ */
47
+ function ruleIsWildcard(safeFor) {
48
+ if (Array.isArray(safeFor)) {
49
+ return safeFor.includes("*");
50
+ } else {
51
+ return safeFor === "*";
52
+ }
47
53
  }
48
54
 
49
55
  /**
50
- * Check if field of target object is safe to access by actor with defined
51
- * roles in specific action
52
- * @param {Object} field description of field from schema
53
- * @param {string} action action to check against
54
- * @param {Array<string>} roles actor roles
55
- * @param {string} special special relations of actor and target (@owner, @system)
56
- * @return {boolean} true - safe
57
- **/
58
- function fieldIsSafe(field, action, roles, special){
59
- //if safe absent - then field is not accessible
60
- if(objHas(field, 'safe') && objHas(field.safe, action)){
61
- //anyone can
62
- if(ruleIsWildcard(field.safe[action])){
63
- return true;
64
- }
65
- //if there're list of roles
66
- if(!Array.isArray(field.safe[action])){
67
- return false;
56
+ * Check if field of target object is safe to access by actor with defined
57
+ * roles in specific action
58
+ * @param {Object} field description of field from schema
59
+ * @param {string} action action to check against
60
+ * @param {Array<string>} roles actor roles
61
+ * @param {string} special special relations of actor and target (@owner, @system)
62
+ * @return {boolean} true - safe
63
+ **/
64
+ function fieldIsSafe(field, action, roles, special) {
65
+ //if safe absent - then field is not accessible
66
+ if (objHas(field, "safe") && objHas(field.safe, action)) {
67
+ //anyone can
68
+ if (ruleIsWildcard(field.safe[action])) {
69
+ return true;
70
+ }
71
+ //if there're list of roles
72
+ if (!Array.isArray(field.safe[action])) {
73
+ return false;
74
+ }
75
+ if (
76
+ //если роли пользователя в списке
77
+ ABSTRACT.intersect_safe(roles, field.safe[action]).length || //или
78
+ //он в спец группе (владелец@owner, система@system),т.е.
79
+ //владеет данными или это системное действие
80
+ ABSTRACT.intersect_safe(special, field.safe[action]).length
81
+ ) {
82
+ return true;
83
+ }
68
84
  }
69
- if (
70
- //если роли пользователя в списке
71
- ABSTRACT.intersect_safe(roles, field.safe[action]).length
72
- || //или
73
- //он в спец группе (владелец@owner, система@system),т.е.
74
- //владеет данными или это системное действие
75
- ABSTRACT.intersect_safe(special, field.safe[action]).length
76
- ){
77
- return true;
78
- }
79
- }
80
- return false;
85
+ return false;
81
86
  }
82
87
 
83
88
  /**
84
- * Creates array of special roles
85
- * @param {boolean} owner if actor is owner of document
86
- * @param {boolean} system if actor is a system process
87
- * @return {Array<string>} list of special roles
88
- **/
89
- function createSpecial(owner, system){
90
- let special = [];
91
- if (owner === true) {
92
- special.push('@owner');
93
- }
94
- if (system === true) {
95
- special.push('@system');
96
- }
97
- return special;
89
+ * Creates array of special roles
90
+ * @param {boolean} owner if actor is owner of document
91
+ * @param {boolean} system if actor is a system process
92
+ * @return {Array<string>} list of special roles
93
+ **/
94
+ function createSpecial(owner, system) {
95
+ let special = [];
96
+ if (owner === true) {
97
+ special.push("@owner");
98
+ }
99
+ if (system === true) {
100
+ special.push("@system");
101
+ }
102
+ return special;
98
103
  }
99
104
 
100
105
  /**
101
- * Scans schema checks every field access regulations for specific action of
102
- * actor with roles. Returns list of fields that could be accessed safely.
103
- * @param {Object} schema mongoose model schema with extended fields from not-*
104
- * @param {string} action action to check against
105
- * @param {Array<string>} roles actor roles
106
- * @param {boolean} owner actor is an owner of document
107
- * @param {boolean} system actor is a system procedure
108
- * @return {Array<string>} list of accessible fields
109
- **/
106
+ * Scans schema checks every field access regulations for specific action of
107
+ * actor with roles. Returns list of fields that could be accessed safely.
108
+ * @param {Object} schema mongoose model schema with extended fields from not-*
109
+ * @param {string} action action to check against
110
+ * @param {Array<string>} roles actor roles
111
+ * @param {boolean} owner actor is an owner of document
112
+ * @param {boolean} system actor is a system procedure
113
+ * @return {Array<string>} list of accessible fields
114
+ **/
110
115
  function getSafeFieldsForRoleAction(schema, action, roles, owner, system) {
111
- let fields = [];
112
- let special = createSpecial(owner, system);
113
- for (let t in schema) {
114
- let field = schema[t];
115
- if (fieldIsSafe(field, action, roles, special)) {
116
- fields.push(t);
116
+ let fields = [];
117
+ let special = createSpecial(owner, system);
118
+ for (let t in schema) {
119
+ let field = schema[t];
120
+ if (fieldIsSafe(field, action, roles, special)) {
121
+ fields.push(t);
122
+ }
117
123
  }
118
- }
119
- return fields;
124
+ return fields;
120
125
  }
121
126
 
122
127
  /**
123
- * Using schema and information about action and actor to extract only safe information
124
- * forming new object with data only from safe fields
125
- * @param {Object} schema mongoose model schema with extended fields from not-*
126
- * @param {string} action action to check against
127
- * @param {Object} data source of data to extract from
128
- * @param {Array<string>} roles actor roles
129
- * @param {string|ObjectId}actorId actor objectId
130
- * @param {boolean} system true if actor is a system procedure
131
- * @return {Object} object containing only data from safe fields
132
- **/
133
- function extractSafeFields(schema, action, data, roles, actorId, system = false) {
134
- let fields = getSafeFieldsForRoleAction(schema, action, roles, isOwner(data, actorId), system);
135
- let result = {};
136
- fields.forEach((field) => {
137
- if (objHas(data, field)) {
138
- result[field] = data[field];
139
- }
140
- });
141
- return result;
128
+ * Using schema and information about action and actor to extract only safe information
129
+ * forming new object with data only from safe fields
130
+ * @param {Object} schema mongoose model schema with extended fields from not-*
131
+ * @param {string} action action to check against
132
+ * @param {Object} data source of data to extract from
133
+ * @param {Array<string>} roles actor roles
134
+ * @param {string|ObjectId}actorId actor objectId
135
+ * @param {boolean} system true if actor is a system procedure
136
+ * @return {Object} object containing only data from safe fields
137
+ **/
138
+ function extractSafeFields(
139
+ schema,
140
+ action,
141
+ data,
142
+ roles,
143
+ actorId,
144
+ system = false,
145
+ ownerFieldName = CONST.DOCUMENT_OWNER_FIELD_NAME
146
+ ) {
147
+ let fields = getSafeFieldsForRoleAction(
148
+ schema,
149
+ action,
150
+ roles,
151
+ isOwner(data, actorId, ownerFieldName),
152
+ system
153
+ );
154
+ let result = {};
155
+ fields.forEach((field) => {
156
+ if (objHas(data, field)) {
157
+ result[field] = data[field];
158
+ }
159
+ });
160
+ return result;
142
161
  }
143
162
 
144
163
  module.exports = {
145
- getSafeFieldsForRoleAction,
146
- fieldIsSafe,
147
- extractSafeFields,
148
- isOwner,
149
- getOwnerId,
150
- ruleIsWildcard,
151
- createSpecial
164
+ getSafeFieldsForRoleAction,
165
+ fieldIsSafe,
166
+ extractSafeFields,
167
+ isOwner,
168
+ getOwnerId,
169
+ ruleIsWildcard,
170
+ createSpecial,
152
171
  };