forked from 352Media/mongoose-authorization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.12 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
// const _ = require('lodash')
const {
FIND
} = require('./lib/constants')
const {
hasPermission,
authIsDisabled,
sanitizeDocumentList
} = require('./lib/helpers')
const PermissionDeniedError = require('./lib/PermissionDeniedError')
module.exports = (schema) => {
/**
* ACL find
* @param query
* @param docs
* @param next
* @returns {*}
* @private
*/
function _find (query, docs, next) {
if (!hasPermission(schema, query.options, FIND)) {
return next(new PermissionDeniedError(FIND))
}
const sanitizedResult = sanitizeDocumentList(schema, query.options, docs)
return next(null, sanitizedResult)
}
/**
* Post find hook
* @param doc
* @param next
* @returns {*}
* @private
*/
function _postFind (doc, next) {
if (authIsDisabled(this.options)) return next()
return _find(this, doc, next)
}
schema.post('find', _postFind)
schema.query.setAuthLevel = _setAuthLevel
}
/**
* Set auth level in query chain
* @param authLevel
* @returns {setAuthLevel}
*/
function _setAuthLevel (authLevel) {
this.options.authLevel = authLevel
return this
}