@bedrockio/model 0.2.21 → 0.2.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/dist/cjs/access.js +1 -10
- package/dist/cjs/include.js +26 -9
- package/dist/cjs/serialization.js +2 -2
- package/dist/cjs/validation.js +1 -1
- package/package.json +1 -1
- package/src/access.js +1 -9
- package/src/include.js +24 -8
- package/src/serialization.js +2 -2
- package/src/validation.js +1 -1
- package/types/access.d.ts +1 -3
- package/types/access.d.ts.map +1 -1
- package/types/include.d.ts +2 -1
- package/types/include.d.ts.map +1 -1
package/dist/cjs/access.js
CHANGED
|
@@ -4,22 +4,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.hasAccess = hasAccess;
|
|
7
|
-
exports.hasReadAccess = hasReadAccess;
|
|
8
|
-
exports.hasWriteAccess = hasWriteAccess;
|
|
9
7
|
var _errors = require("./errors");
|
|
10
8
|
var _warn = _interopRequireDefault(require("./warn"));
|
|
11
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
-
function hasReadAccess(allowed, options) {
|
|
13
|
-
return hasAccess('read', allowed, options);
|
|
14
|
-
}
|
|
15
|
-
function hasWriteAccess(allowed, options) {
|
|
16
|
-
return hasAccess('write', allowed, options);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
10
|
/**
|
|
20
11
|
* @param {string|string[]} allowed
|
|
21
12
|
*/
|
|
22
|
-
function hasAccess(
|
|
13
|
+
function hasAccess(allowed = 'all', options = {}) {
|
|
23
14
|
if (allowed === 'all') {
|
|
24
15
|
return true;
|
|
25
16
|
} else if (allowed === 'none') {
|
package/dist/cjs/include.js
CHANGED
|
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.INCLUDE_FIELD_SCHEMA = void 0;
|
|
7
7
|
exports.applyInclude = applyInclude;
|
|
8
8
|
exports.checkSelects = checkSelects;
|
|
9
|
-
exports.
|
|
9
|
+
exports.getDocumentParams = getDocumentParams;
|
|
10
|
+
exports.getParams = getParams;
|
|
10
11
|
var _mongoose = _interopRequireDefault(require("mongoose"));
|
|
11
12
|
var _lodash = require("lodash");
|
|
12
13
|
var _yada = _interopRequireDefault(require("@bedrockio/yada"));
|
|
@@ -36,7 +37,7 @@ function applyInclude(schema) {
|
|
|
36
37
|
const {
|
|
37
38
|
select,
|
|
38
39
|
populate
|
|
39
|
-
} =
|
|
40
|
+
} = getQueryParams(this, filter.include);
|
|
40
41
|
this.select(select);
|
|
41
42
|
this.populate(populate);
|
|
42
43
|
delete filter.include;
|
|
@@ -78,7 +79,7 @@ function applyInclude(schema) {
|
|
|
78
79
|
const {
|
|
79
80
|
select,
|
|
80
81
|
populate
|
|
81
|
-
} =
|
|
82
|
+
} = getDocumentParams(this, include);
|
|
82
83
|
this.$locals.select = select;
|
|
83
84
|
this.$locals.populate = populate;
|
|
84
85
|
}
|
|
@@ -103,7 +104,7 @@ function applyInclude(schema) {
|
|
|
103
104
|
const {
|
|
104
105
|
select,
|
|
105
106
|
populate
|
|
106
|
-
} =
|
|
107
|
+
} = getDocumentParams(this, include);
|
|
107
108
|
this.$locals.select = select;
|
|
108
109
|
await this.populate(populate);
|
|
109
110
|
}
|
|
@@ -147,16 +148,32 @@ function checkSelects(doc, ret) {
|
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
// Exported for testing.
|
|
150
|
-
function
|
|
151
|
+
function getParams(modelName, arg) {
|
|
151
152
|
const paths = Array.isArray(arg) ? arg : [arg];
|
|
152
153
|
const node = pathsToNode(paths, modelName);
|
|
153
154
|
return nodeToPopulates(node);
|
|
154
155
|
}
|
|
155
|
-
|
|
156
|
-
|
|
156
|
+
|
|
157
|
+
// Exported for testing.
|
|
158
|
+
function getDocumentParams(doc, arg) {
|
|
159
|
+
const params = getParams(doc.constructor.modelName, arg);
|
|
160
|
+
params.populate = params.populate.filter(p => {
|
|
161
|
+
return !isDocumentPopulated(doc, p);
|
|
162
|
+
});
|
|
163
|
+
return params;
|
|
164
|
+
}
|
|
165
|
+
function isDocumentPopulated(doc, params) {
|
|
166
|
+
if (doc.populated(params.path)) {
|
|
167
|
+
const sub = doc.get(params.path);
|
|
168
|
+
return params.populate.every(p => {
|
|
169
|
+
return isDocumentPopulated(sub, p);
|
|
170
|
+
});
|
|
171
|
+
} else {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
157
174
|
}
|
|
158
|
-
function
|
|
159
|
-
return
|
|
175
|
+
function getQueryParams(query, arg) {
|
|
176
|
+
return getParams(query.model.modelName, arg);
|
|
160
177
|
}
|
|
161
178
|
|
|
162
179
|
// Note that:
|
|
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.serializeOptions = void 0;
|
|
7
7
|
var _lodash = require("lodash");
|
|
8
8
|
var _include = require("./include");
|
|
9
|
-
var _access = require("./access");
|
|
10
9
|
var _utils = require("./utils");
|
|
10
|
+
var _access = require("./access");
|
|
11
11
|
const DISALLOWED_FIELDS = ['deleted', 'deletedRefs'];
|
|
12
12
|
const serializeOptions = exports.serializeOptions = {
|
|
13
13
|
getters: true,
|
|
@@ -63,7 +63,7 @@ function isAllowedField(key, field, options) {
|
|
|
63
63
|
readAccess
|
|
64
64
|
} = (0, _utils.getField)(field, key);
|
|
65
65
|
try {
|
|
66
|
-
return (0, _access.
|
|
66
|
+
return (0, _access.hasAccess)(readAccess, options);
|
|
67
67
|
} catch {
|
|
68
68
|
return false;
|
|
69
69
|
}
|
package/dist/cjs/validation.js
CHANGED
|
@@ -406,7 +406,7 @@ function validateAccess(type, schema, allowed, options) {
|
|
|
406
406
|
const document = options[(0, _lodash.lowerFirst)(modelName)] || options['document'];
|
|
407
407
|
let isAllowed;
|
|
408
408
|
try {
|
|
409
|
-
isAllowed = (0, _access.hasAccess)(
|
|
409
|
+
isAllowed = (0, _access.hasAccess)(allowed, {
|
|
410
410
|
...options,
|
|
411
411
|
document
|
|
412
412
|
});
|
package/package.json
CHANGED
package/src/access.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
import { ImplementationError } from './errors';
|
|
2
2
|
import warn from './warn';
|
|
3
3
|
|
|
4
|
-
export function hasReadAccess(allowed, options) {
|
|
5
|
-
return hasAccess('read', allowed, options);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function hasWriteAccess(allowed, options) {
|
|
9
|
-
return hasAccess('write', allowed, options);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
4
|
/**
|
|
13
5
|
* @param {string|string[]} allowed
|
|
14
6
|
*/
|
|
15
|
-
export function hasAccess(
|
|
7
|
+
export function hasAccess(allowed = 'all', options = {}) {
|
|
16
8
|
if (allowed === 'all') {
|
|
17
9
|
return true;
|
|
18
10
|
} else if (allowed === 'none') {
|
package/src/include.js
CHANGED
|
@@ -31,7 +31,7 @@ export function applyInclude(schema) {
|
|
|
31
31
|
schema.pre(/^find/, function (next) {
|
|
32
32
|
const filter = this.getFilter();
|
|
33
33
|
if (filter.include) {
|
|
34
|
-
const { select, populate } =
|
|
34
|
+
const { select, populate } = getQueryParams(this, filter.include);
|
|
35
35
|
this.select(select);
|
|
36
36
|
this.populate(populate);
|
|
37
37
|
delete filter.include;
|
|
@@ -70,7 +70,7 @@ export function applyInclude(schema) {
|
|
|
70
70
|
this.assign(rest);
|
|
71
71
|
|
|
72
72
|
if (include) {
|
|
73
|
-
const { select, populate } =
|
|
73
|
+
const { select, populate } = getDocumentParams(this, include);
|
|
74
74
|
this.$locals.select = select;
|
|
75
75
|
this.$locals.populate = populate;
|
|
76
76
|
}
|
|
@@ -91,7 +91,7 @@ export function applyInclude(schema) {
|
|
|
91
91
|
// during serialization.
|
|
92
92
|
schema.method('include', async function include(include) {
|
|
93
93
|
if (include) {
|
|
94
|
-
const { select, populate } =
|
|
94
|
+
const { select, populate } = getDocumentParams(this, include);
|
|
95
95
|
this.$locals.select = select;
|
|
96
96
|
await this.populate(populate);
|
|
97
97
|
}
|
|
@@ -133,18 +133,34 @@ export function checkSelects(doc, ret) {
|
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
// Exported for testing.
|
|
136
|
-
export function
|
|
136
|
+
export function getParams(modelName, arg) {
|
|
137
137
|
const paths = Array.isArray(arg) ? arg : [arg];
|
|
138
138
|
const node = pathsToNode(paths, modelName);
|
|
139
139
|
return nodeToPopulates(node);
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
// Exported for testing.
|
|
143
|
+
export function getDocumentParams(doc, arg) {
|
|
144
|
+
const params = getParams(doc.constructor.modelName, arg);
|
|
145
|
+
params.populate = params.populate.filter((p) => {
|
|
146
|
+
return !isDocumentPopulated(doc, p);
|
|
147
|
+
});
|
|
148
|
+
return params;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function isDocumentPopulated(doc, params) {
|
|
152
|
+
if (doc.populated(params.path)) {
|
|
153
|
+
const sub = doc.get(params.path);
|
|
154
|
+
return params.populate.every((p) => {
|
|
155
|
+
return isDocumentPopulated(sub, p);
|
|
156
|
+
});
|
|
157
|
+
} else {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
144
160
|
}
|
|
145
161
|
|
|
146
|
-
function
|
|
147
|
-
return
|
|
162
|
+
function getQueryParams(query, arg) {
|
|
163
|
+
return getParams(query.model.modelName, arg);
|
|
148
164
|
}
|
|
149
165
|
|
|
150
166
|
// Note that:
|
package/src/serialization.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isPlainObject } from 'lodash';
|
|
2
2
|
|
|
3
3
|
import { checkSelects } from './include';
|
|
4
|
-
import { hasReadAccess } from './access';
|
|
5
4
|
import { getField, getInnerField } from './utils';
|
|
5
|
+
import { hasAccess } from './access';
|
|
6
6
|
|
|
7
7
|
const DISALLOWED_FIELDS = ['deleted', 'deletedRefs'];
|
|
8
8
|
|
|
@@ -60,7 +60,7 @@ function isAllowedField(key, field, options) {
|
|
|
60
60
|
} else {
|
|
61
61
|
const { readAccess } = getField(field, key);
|
|
62
62
|
try {
|
|
63
|
-
return
|
|
63
|
+
return hasAccess(readAccess, options);
|
|
64
64
|
} catch {
|
|
65
65
|
return false;
|
|
66
66
|
}
|
package/src/validation.js
CHANGED
package/types/access.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
export function hasReadAccess(allowed: any, options: any): boolean;
|
|
2
|
-
export function hasWriteAccess(allowed: any, options: any): boolean;
|
|
3
1
|
/**
|
|
4
2
|
* @param {string|string[]} allowed
|
|
5
3
|
*/
|
|
6
|
-
export function hasAccess(
|
|
4
|
+
export function hasAccess(allowed?: string | string[], options?: {}): boolean;
|
|
7
5
|
//# sourceMappingURL=access.d.ts.map
|
package/types/access.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.js"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.js"],"names":[],"mappings":"AAGA;;GAEG;AACH,oCAFW,MAAM,GAAC,MAAM,EAAE,yBA2BzB"}
|
package/types/include.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export function applyInclude(schema: any): void;
|
|
2
2
|
export function checkSelects(doc: any, ret: any): void;
|
|
3
|
-
export function
|
|
3
|
+
export function getParams(modelName: any, arg: any): any;
|
|
4
|
+
export function getDocumentParams(doc: any, arg: any): any;
|
|
4
5
|
export const INCLUDE_FIELD_SCHEMA: {
|
|
5
6
|
setup: any;
|
|
6
7
|
getFields: any;
|
package/types/include.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAuEC;AAMD,uDA4BC;AAGD,
|
|
1
|
+
{"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAuEC;AAMD,uDA4BC;AAGD,yDAIC;AAGD,2DAMC;AAhID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKG"}
|