-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add promise support #23
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
const _ = require('lodash'); | ||
|
||
function resolveAuthLevel(schema, options, doc) { | ||
let authLevelCache; | ||
|
||
function resetCache() { | ||
authLevelCache = new Map(); | ||
} | ||
|
||
async function resolveAuthLevel(schema, options, doc) { | ||
// Look into options the options and try to find authLevels. Always prefer to take | ||
// authLevels from the direct authLevel option as opposed to the computed | ||
// ones from getAuthLevel in the schema object. | ||
|
@@ -9,9 +15,21 @@ function resolveAuthLevel(schema, options, doc) { | |
if (options.authLevel) { | ||
authLevels = _.castArray(options.authLevel); | ||
} else if (typeof schema.getAuthLevel === 'function') { | ||
authLevels = _.castArray(schema.getAuthLevel(options.authPayload, doc)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the approach of changes lines 7-17 as opposed to adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise it's ambiguous what exactly is in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few reasons, I'm casting it right after and throwing away authLevelsIn, adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Two things. You don't need an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @makinde learned something new! await works on non-promise values |
||
let arg2Map = authLevelCache.get(options.authLevel) | ||
if (!arg2Map) { | ||
arg2Map = new Map(); | ||
authLevelCache.set(options.authLevel, arg2Map); | ||
} | ||
const cachedValue = arg2Map.get(doc); | ||
if (cachedValue) { | ||
authLevels = cachedValue; | ||
} else { | ||
authLevels = _.castArray(await schema.getAuthLevel(options.authPayload, doc)); | ||
arg2Map.set(doc, authLevels); | ||
} | ||
} | ||
} | ||
|
||
// Add `defaults` to the list of levels since you should always be able to do what's specified | ||
// in defaults. | ||
authLevels.push('defaults'); | ||
|
@@ -23,8 +41,8 @@ function resolveAuthLevel(schema, options, doc) { | |
.value(); | ||
} | ||
|
||
function getAuthorizedFields(schema, options, action, doc) { | ||
const authLevels = resolveAuthLevel(schema, options, doc); | ||
async function getAuthorizedFields(schema, options, action, doc) { | ||
const authLevels = await resolveAuthLevel(schema, options, doc); | ||
|
||
return _.chain(authLevels) | ||
.flatMap(level => schema.permissions[level][action]) | ||
|
@@ -33,8 +51,8 @@ function getAuthorizedFields(schema, options, action, doc) { | |
.value(); | ||
} | ||
|
||
function hasPermission(schema, options, action, doc) { | ||
const authLevels = resolveAuthLevel(schema, options, doc); | ||
async function hasPermission(schema, options, action, doc) { | ||
const authLevels = await resolveAuthLevel(schema, options, doc); | ||
const perms = schema.permissions || {}; | ||
|
||
// look for any permissions setting for this action that is set to true (for these authLevels) | ||
|
@@ -45,21 +63,21 @@ function authIsDisabled(options) { | |
return options && options.authLevel === false; | ||
} | ||
|
||
function embedPermissions(schema, options, doc) { | ||
async function embedPermissions(schema, options, doc) { | ||
if (!options || !options.permissions) { return; } | ||
|
||
const permsKey = options.permissions === true ? 'permissions' : options.permissions; | ||
doc[permsKey] = { | ||
read: getAuthorizedFields(schema, options, 'read', doc), | ||
write: getAuthorizedFields(schema, options, 'write', doc), | ||
remove: hasPermission(schema, options, 'remove', doc), | ||
read: await getAuthorizedFields(schema, options, 'read', doc), | ||
write: await getAuthorizedFields(schema, options, 'write', doc), | ||
remove: await hasPermission(schema, options, 'remove', doc), | ||
}; | ||
} | ||
|
||
function sanitizeDocument(schema, options, doc) { | ||
const authorizedFields = getAuthorizedFields(schema, options, 'read', doc); | ||
async function sanitizeDocument(schema, options, doc) { | ||
const authorizedFields = await getAuthorizedFields(schema, options, 'read', doc); | ||
|
||
if (!doc || getAuthorizedFields.length === 0) { return false; } | ||
if (!doc || authorizedFields.length === 0) { return false; } | ||
|
||
// Check to see if group has the permission to see the fields that came back. | ||
// We must edit the document in place to maintain the right reference | ||
|
@@ -97,36 +115,35 @@ function sanitizeDocument(schema, options, doc) { | |
|
||
// Check to see if we're going to be inserting the permissions info | ||
if (options.permissions) { | ||
embedPermissions(schema, options, doc); | ||
await embedPermissions(schema, options, doc); | ||
} | ||
|
||
// Apply the rules down one level if there are any path specific permissions | ||
_.each(schema.pathsWithPermissionedSchemas, (path, subSchema) => { | ||
await Promise.all(_.map(schema.pathsWithPermissionedSchemas, async (path, subSchema) => { | ||
if (innerDoc[path]) { | ||
// eslint-disable-next-line no-use-before-define | ||
innerDoc[path] = sanitizeDocumentList(subSchema, options, innerDoc[path]); | ||
innerDoc[path] = await sanitizeDocumentList(subSchema, options, innerDoc[path]); | ||
} | ||
}); | ||
})); | ||
|
||
return doc; | ||
} | ||
|
||
function sanitizeDocumentList(schema, options, docs) { | ||
async function sanitizeDocumentList(schema, options, docs) { | ||
const multi = _.isArrayLike(docs); | ||
const docList = _.castArray(docs); | ||
const sanitizeAndAddOptions = (doc) => { | ||
const upgradedOptions = _.isEmpty(schema.pathsWithPermissionedSchemas) | ||
? options | ||
: _.merge({}, options, { authPayload: { originalDoc: doc } }); | ||
return sanitizeDocument(schema, upgradedOptions, doc); | ||
}; | ||
|
||
const filteredResult = _.chain(docList) | ||
.map((doc) => { | ||
const upgradedOptions = _.isEmpty(schema.pathsWithPermissionedSchemas) | ||
? options | ||
: _.merge({}, options, { authPayload: { originalDoc: doc } }); | ||
|
||
return sanitizeDocument(schema, upgradedOptions, doc); | ||
}) | ||
.filter(docList) | ||
.value(); | ||
const filteredResult = ( | ||
await Promise.all(docList.map(sanitizeAndAddOptions)) | ||
).filter(doc => !doc); | ||
|
||
return multi ? filteredResult : filteredResult[0]; | ||
return multi ? (filteredResult) : filteredResult[0]; | ||
} | ||
|
||
function getUpdatePaths(updates) { | ||
|
@@ -143,6 +160,7 @@ function getUpdatePaths(updates) { | |
} | ||
|
||
module.exports = { | ||
resetCache, | ||
resolveAuthLevel, | ||
getAuthorizedFields, | ||
hasPermission, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions would all need to
async
(and useawait
) as well, right? This is a little off since they use thenext()
function as well. Mongoose 5.x (I think) will see that a promise is returned and move along when the promise is resolved. But the code is written to wait fornext()
to be called. And Mongoose 4.x doesn't support promises from middleware, so there might be some inconsistent behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work as is, because we are just forwarding next it doesn't care about the promises.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've got me thinking on overdrive here. I think the issue is that if one of the lower level functions throws an exception, then we'll get an UnresolvedPromiseRejection since the promise isn't tied all the way up.
removeQuery
is an async function that will return a promise, and that promise isn't handled.You could poke at this and test it. I suspect you might need to do
It's a little jank since the non-error path passes the next function down, but the error path is handled up here as a promise. This actually might be a problem in the existing code, so I'm open to you highlighting that and us creating a new task to properly handle exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really appreciate the thought around the error path here. I think it is important and it is something I hadn't considered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I fully understand the problem you are outlining, but I would love to learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see what you are saying, does mongoose do
.catch()
on returned promises?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that
removeQuery
returns a promise, right? It is anasync
function. If that promise rejects (let's say because an error gets thrown), node will stop because the promise rejects, but there's no.catch()
on the promise. Try it out. just insert a throw in one of the helper functions. You'll get the following when you runnpm test
.This sorta the challenge of this change. By adding an async function at the root, we're forced to change every function in the library to handle promises properly when the old code was mostly synchronous.