-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
319 lines (265 loc) · 8.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const mongoose = require('mongoose')
const Filter = require('./filter')
const castArray = require('./helpers').castArray
class MongooseFields {
/**
* Create a new mongoose filter for a given schema
*
* @param {mongoose.Schema} schema
* @param {Object} [options] - A hash of configuration options.
*/
constructor(schema, options) {
this._modelCache = {}
this._pathsCache = []
this._publicPathsCache = null
this._accessPathsCache = {}
this.schema = schema
this.options = options || {}
this.filter = new Filter(this.options)
}
traverse(schema, prefix = '', depth = 0) {
const paths = []
const maxDepth = 'depth' in this.options ? this.options.depth : 3
schema && schema.eachPath((name, path) => {
// refs to another model (i.e: populate)
if(!!path.options.ref) {
const refModel = mongoose.model(path.options.ref)
if(depth < maxDepth) {
paths.push(...this.traverse(refModel.schema, `${prefix + name}.`, depth + 1))
}
}
if(path.instance == 'Array' || path.instance == 'Embedded') {
paths.push(...this.traverse(path.schema, `${prefix + name}.`, depth))
return
}
const isPrivate = 'private' in path.options ? path.options.private : false
return paths.push({
name: prefix + name,
type: path.instance,
isRef: !!path.options.ref,
ref: path.options.ref,
private: isPrivate,
access: this.getAccessKey() in path.options ?
path.options[this.getAccessKey()] :
(isPrivate ? 'private' : 'public')
})
})
schema && Object.keys(schema.virtuals).forEach(key => {
const path = schema.virtuals[key]
const isPrivate = 'private' in path.options ? path.options.private : false
paths.push({
name: prefix + path.path,
type: undefined,
isRef: false,
ref: false,
private: isPrivate,
access: this.getAccessKey() in path.options ?
path.options[this.getAccessKey()] :
(isPrivate ? 'private' : 'public')
})
})
return paths
}
getAccessPaths(access){
const cacheKey = castArray(access).sort().join(':')
if(!(cacheKey in this._accessPathsCache)) {
this._accessPathsCache[cacheKey] = this.getPaths()
.filter(p => {
const permissions = castArray(p[this.getAccessKey()])
return permissions.some(a => access.includes(a))
})
.map(p => p.name)
}
return this._accessPathsCache[cacheKey]
}
getPublicPaths() {
const paths = this.getPaths()
if(this._publicPathsCache == null) {
this._publicPathsCache = paths
.filter(p => p.private == false && p[this.getAccessKey()] == 'public')
.map(p => p.name)
}
return this._publicPathsCache
}
getPaths() {
if(this._pathsCache.length == 0) {
this._pathsCache = this.traverse(this.schema)
}
return this._pathsCache
}
/**
* Apply the mongo fields filter plugin to the given schema.
*
* @returns {MongooseFields}
*/
apply() {
this
.injectApi()
.installMiddleWare()
}
/**
* Return the name of the access getter method.
*
* @returns {*|string}
*/
getAccessKey() {
return this.options.accessKey || 'access'
}
/**
* Return the method name for accessing filtered-bound models.
*
* @returns {*|string}
*/
getAccessorMethod() {
return this.options.accessorMethod || 'byAccess'
}
/**
* Return the name of the access getter method.
*
* @returns {*|string}
*/
getAccessIdGetter() {
return this.options.accessIdGetter || 'getAccess'
}
/**
* Inject the user-space entry point for mongo fields.
* This method adds a static Model method to retrieve access bound sub-classes.
*
* @returns {MongooseFilter}
*/
injectApi() {
let me = this
this.schema.statics[this.getAccessorMethod()] = function(access) {
let modelCache = me._modelCache[this.modelName] || (me._modelCache[this.modelName] = {})
// lookup access-bound model in cache
if (!modelCache[access]) {
let Model = this.model(this.modelName)
// Cache the access bound model class.
modelCache[access] = me.createAccessAwareModel(Model, access)
}
return modelCache[access]
}
return this
}
/**
* Create a model class that is bound to the given access.
* So that all operations on this model are bound to this access
*
* @param BaseModel
* @param access
* @returns {MongoAccessModel}
*/
createAccessAwareModel(BaseModel, access) {
const me = this
let accessIdGetter = this.getAccessIdGetter()
access = castArray(access)
class MongoAccessModel extends BaseModel {
static get hasAccessContext() {
return true
}
static [accessIdGetter]() {
return access
}
}
// inherit all static properties from the mongoose base model
for (let staticProperty of Object.getOwnPropertyNames(BaseModel)) {
if (MongoAccessModel.hasOwnProperty(staticProperty)
|| ['arguments', 'caller'].indexOf(staticProperty) !== -1
) {
continue
}
let descriptor = Object.getOwnPropertyDescriptor(BaseModel, staticProperty)
Object.defineProperty(MongoAccessModel, staticProperty, descriptor)
}
// create access models for discriminators if they exist
if (BaseModel.discriminators) {
MongoAccessModel.discriminators = {}
for (let key in BaseModel.discriminators) {
MongoAccessModel.discriminators[key] = this.createAccessAwareModel(BaseModel.discriminators[key], access)
}
}
return MongoAccessModel
}
/**
* Install schema middleware to apply access filters
*
* @returns {MongooseFilter}
*/
installMiddleWare() {
let
me = this,
accessIdGetter = this.getAccessIdGetter()
this.schema.statics.find = function(conditions, projection, options, callback) {
if (typeof conditions === 'function') {
callback = conditions;
conditions = {};
projection = null;
options = null;
} else if (typeof projection === 'function') {
callback = projection;
projection = null;
options = null;
} else if (typeof options === 'function') {
callback = options;
options = null;
}
return mongoose.Model.find.apply(this, arguments).map(docs => {
if(docs && docs.length == 0) {
return docs
}
if(this.hasAccessContext || (options && options.filter)) {
const paths = me.getPublicPaths()
paths.push(...me.getAccessPaths('public'))
if(this.hasAccessContext) {
const access = this[accessIdGetter]()
paths.push(...me.getAccessPaths(access))
}
return me.filter.pick(docs, paths)
}
return docs
})
}
this.schema.statics.findOne = function(conditions, projection, options, callback) {
if (typeof conditions === 'function') {
callback = conditions;
conditions = {};
projection = null;
options = null;
} else if (typeof projection === 'function') {
callback = projection;
projection = null;
options = null;
} else if (typeof options === 'function') {
callback = options;
options = null;
}
return mongoose.Model.findOne.apply(this, arguments).map(doc => {
if(doc == null) {
return doc
}
if(this.hasAccessContext || (options && options.filter)) {
const paths = me.getPublicPaths()
paths.push(...me.getAccessPaths('public'))
if(this.hasAccessContext) {
const access = this[accessIdGetter]()
paths.push(...me.getAccessPaths(access))
}
return me.filter.pick(doc, paths)
}
return doc
})
}
}
}
/**
* The mongoose-fields-filter plugin.
*
* @param {mongoose.Schema} schema
* @param {Object} options
*/
function mongooseFieldsPlugin(schema, options) {
let mongooseFields = new MongooseFields(schema, options)
mongooseFields.apply()
}
mongooseFieldsPlugin.MongooseFields = MongooseFields
module.exports = mongooseFieldsPlugin