not-node 5.0.22 → 5.1.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.
- package/.eslintrc.json +1 -1
- package/bin/not-deploy.js +52 -0
- package/index.js +21 -19
- package/package.json +1 -1
- package/src/app.js +61 -58
- package/src/auth/abstract.js +17 -19
- package/src/auth/const.js +8 -12
- package/src/auth/fields.js +143 -124
- package/src/auth/index.js +14 -14
- package/src/auth/roles.js +64 -63
- package/src/auth/routes.js +89 -54
- package/src/auth/rules.js +63 -63
- package/src/auth/session.js +60 -62
- package/src/bootstrap/form.js +13 -13
- package/src/bootstrap/logic.js +45 -41
- package/src/bootstrap/model.js +14 -17
- package/src/bootstrap/route.js +132 -53
- package/src/common.js +86 -79
- package/src/core/fields/ID.js +6 -6
- package/src/core/fields/__closed.js +3 -3
- package/src/core/fields/__latest.js +3 -3
- package/src/core/fields/__version.js +3 -3
- package/src/core/fields/__versions.js +3 -3
- package/src/core/fields/_id.js +6 -6
- package/src/core/fields/active.js +9 -9
- package/src/core/fields/codeName.js +9 -9
- package/src/core/fields/createdAt.js +17 -17
- package/src/core/fields/default.js +9 -9
- package/src/core/fields/description.js +11 -11
- package/src/core/fields/email.js +9 -9
- package/src/core/fields/enabled.js +9 -9
- package/src/core/fields/expiredAt.js +16 -16
- package/src/core/fields/height.js +11 -11
- package/src/core/fields/ip.js +10 -10
- package/src/core/fields/objectId.js +10 -10
- package/src/core/fields/owner.js +13 -13
- package/src/core/fields/ownerModel.js +11 -11
- package/src/core/fields/price.js +11 -11
- package/src/core/fields/requiredObject.js +10 -10
- package/src/core/fields/session.js +10 -10
- package/src/core/fields/size.js +11 -11
- package/src/core/fields/telephone.js +9 -9
- package/src/core/fields/title.js +15 -15
- package/src/core/fields/updatedAt.js +17 -17
- package/src/core/fields/userId.js +11 -12
- package/src/core/fields/uuid.js +11 -11
- package/src/core/fields/validators/email.js +6 -4
- package/src/core/fields/validators/owner.js +6 -4
- package/src/core/fields/width.js +11 -11
- package/src/domain.js +435 -431
- package/src/env.js +23 -23
- package/src/error.js +20 -23
- package/src/exceptions/db.js +23 -0
- package/src/exceptions/http.js +43 -0
- package/src/fields/index.js +106 -111
- package/src/form/fabric.js +19 -24
- package/src/form/form.js +195 -186
- package/src/form/index.js +2 -2
- package/src/generic/index.js +2 -0
- package/src/generic/logic.js +595 -0
- package/src/generic/route.js +76 -0
- package/src/init/additional.js +7 -7
- package/src/init/app.js +75 -68
- package/src/init/bodyparser.js +14 -14
- package/src/init/compression.js +6 -7
- package/src/init/core.js +12 -12
- package/src/init/cors.js +22 -24
- package/src/init/db/index.js +41 -43
- package/src/init/db/ioredis.js +28 -20
- package/src/init/db/mongoose.js +42 -32
- package/src/init/db/redis.js +59 -48
- package/src/init/env.js +44 -36
- package/src/init/express.js +18 -21
- package/src/init/fileupload.js +8 -10
- package/src/init/http.js +65 -47
- package/src/init/index.js +141 -148
- package/src/init/informer.js +7 -9
- package/src/init/methodoverride.js +4 -6
- package/src/init/middleware.js +23 -26
- package/src/init/modules.js +4 -4
- package/src/init/monitoring.js +8 -8
- package/src/init/rateLimiter.js +53 -45
- package/src/init/routes.js +67 -69
- package/src/init/security.js +37 -33
- package/src/init/sequence.js +111 -107
- package/src/init/sequence.standart.js +54 -56
- package/src/init/sessions/index.js +21 -21
- package/src/init/sessions/mongoose.js +49 -36
- package/src/init/sessions/redis.js +28 -27
- package/src/init/static.js +53 -40
- package/src/init/template.js +17 -17
- package/src/lib.js +230 -200
- package/src/manifest/batchRunner.js +26 -25
- package/src/manifest/initializator/forms.js +24 -24
- package/src/manifest/initializator/index.js +8 -10
- package/src/manifest/initializator/manifests.js +45 -42
- package/src/manifest/initializator/models.js +37 -34
- package/src/manifest/manifest.filter.js +130 -97
- package/src/manifest/manifest.js +103 -77
- package/src/manifest/module.js +350 -360
- package/src/manifest/registrator/fields.js +90 -85
- package/src/manifest/registrator/forms.js +47 -47
- package/src/manifest/registrator/index.js +14 -16
- package/src/manifest/registrator/locales.js +17 -16
- package/src/manifest/registrator/logics.js +66 -64
- package/src/manifest/registrator/models.js +64 -62
- package/src/manifest/registrator/routes.js +171 -132
- package/src/manifest/registrator/routes.ws.js +109 -103
- package/src/manifest/route.js +216 -161
- package/src/model/buildValidator.js +53 -42
- package/src/model/default.js +304 -286
- package/src/model/enrich.js +69 -61
- package/src/model/increment.js +124 -137
- package/src/model/proto.js +179 -148
- package/src/model/routine.js +85 -76
- package/src/model/utils.js +33 -0
- package/src/model/versioning.js +148 -130
- package/src/obsolete.js +12 -8
- package/src/parser.js +29 -22
- package/src/repos.js +29 -31
- package/src/rollup.js +75 -65
- package/src/shell.helpers.js +28 -28
- package/static.js +31 -0
- package/src/generic/router.js +0 -16
- package/src/manifest/module.models.js +0 -0
- package/src/manifest/module.status.js +0 -0
package/.eslintrc.json
CHANGED
|
@@ -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(
|
|
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(
|
|
9
|
+
module.exports.Error = require("./src/error.js");
|
|
10
10
|
/** Authentication module */
|
|
11
|
-
module.exports.Auth = require(
|
|
11
|
+
module.exports.Auth = require("./src/auth");
|
|
12
12
|
/** Manifest infrastructure */
|
|
13
|
-
module.exports.Manifest = require(
|
|
13
|
+
module.exports.Manifest = require("./src/manifest/manifest");
|
|
14
14
|
/** Web Application */
|
|
15
|
-
module.exports.notApp = require(
|
|
15
|
+
module.exports.notApp = require("./src/app");
|
|
16
16
|
/** General Application */
|
|
17
|
-
module.exports.notDomain = require(
|
|
17
|
+
module.exports.notDomain = require("./src/domain");
|
|
18
18
|
/** Mongoose Documents versioning */
|
|
19
|
-
module.exports.Versioning = require(
|
|
19
|
+
module.exports.Versioning = require("./src/model/versioning");
|
|
20
20
|
/** Mongoose Model autoincrement field */
|
|
21
|
-
module.exports.Increment = require(
|
|
21
|
+
module.exports.Increment = require("./src/model/increment");
|
|
22
22
|
/** Mongoose Model prototype */
|
|
23
|
-
module.exports.Proto = require(
|
|
23
|
+
module.exports.Proto = require("./src/model/proto");
|
|
24
24
|
/** Mongoose Model additional features enricher */
|
|
25
|
-
module.exports.Enrich = require(
|
|
25
|
+
module.exports.Enrich = require("./src/model/enrich");
|
|
26
26
|
/** Mongoose Documents routine operation*/
|
|
27
|
-
module.exports.Routine = require(
|
|
27
|
+
module.exports.Routine = require("./src/model/routine");
|
|
28
28
|
/** Common functions */
|
|
29
|
-
module.exports.Common = require(
|
|
29
|
+
module.exports.Common = require("./src/common");
|
|
30
30
|
/** Fields library manager */
|
|
31
|
-
module.exports.Fields = require(
|
|
31
|
+
module.exports.Fields = require("./src/fields");
|
|
32
32
|
/** Form validation template **/
|
|
33
|
-
module.exports.Form = require(
|
|
33
|
+
module.exports.Form = require("./src/form").Form;
|
|
34
34
|
/** Form validation template fabric **/
|
|
35
|
-
module.exports.FormFabric = require(
|
|
35
|
+
module.exports.FormFabric = require("./src/form").FormFabric;
|
|
36
36
|
/** Application initialization procedures */
|
|
37
|
-
module.exports.Init = require(
|
|
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(
|
|
41
|
+
module.exports.Bootstrap = require("./src/bootstrap");
|
|
42
|
+
/** Application generic helpers */
|
|
43
|
+
module.exports.Generic = require("./src/generic/index.js");
|
package/package.json
CHANGED
package/src/app.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const Auth = require(
|
|
2
|
-
const notDomain = require(
|
|
3
|
-
const merge = require(
|
|
4
|
-
const parent = require(
|
|
5
|
-
const {objHas} = require(
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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;
|
package/src/auth/abstract.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
const CONST = require("./const");
|
|
1
2
|
|
|
2
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
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
|
-
|
|
34
|
-
|
|
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
|
|
3
|
-
const DEFAULT_USER_ROLE_FOR_GUEST = 'guest';
|
|
4
|
+
const OBJECT_STRING = "[object String]";
|
|
4
5
|
|
|
5
|
-
const
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
};
|
package/src/auth/fields.js
CHANGED
|
@@ -1,152 +1,171 @@
|
|
|
1
|
-
const ABSTRACT = require(
|
|
2
|
-
const COMMON = require(
|
|
3
|
-
const CONST = require(
|
|
4
|
-
const {objHas}
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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(
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
164
|
+
getSafeFieldsForRoleAction,
|
|
165
|
+
fieldIsSafe,
|
|
166
|
+
extractSafeFields,
|
|
167
|
+
isOwner,
|
|
168
|
+
getOwnerId,
|
|
169
|
+
ruleIsWildcard,
|
|
170
|
+
createSpecial,
|
|
152
171
|
};
|