@@ -9,6 +9,8 @@ const getEmbeddedPermission = require('./src/getEmbeddedPermission');
99const PermissionDeniedError = require ( './src/PermissionDeniedError' ) ;
1010const IncompatibleMethodError = require ( './src/IncompatibleMethodError' ) ;
1111
12+ const docOptionsSymbol = Symbol ( 'documentOptions' ) ;
13+
1214module . exports = ( schema , installationOptions ) => {
1315 async function save ( doc , options ) {
1416 let authorizedFields = getEmbeddedPermission ( doc , options , 'write' ) ;
@@ -110,11 +112,25 @@ module.exports = (schema, installationOptions) => {
110112 // is to have arguments to the middleware function. If we have arguments, mongoose
111113 // assume we want to use a `next()` function. FML
112114 schema . pre ( 'save' , function preSave ( next , options ) {
115+ // Embed the options into the doc so we have access to them in post save hooks
116+ this [ docOptionsSymbol ] = options ;
113117 if ( authIsDisabled ( options ) ) { return next ( ) ; }
118+
114119 return save ( this , options )
115120 . then ( ( ) => next ( ) )
116121 . catch ( next ) ;
117122 } ) ;
123+ schema . post ( 'save' , ( doc , next ) => {
124+ const options = doc [ docOptionsSymbol ] ;
125+ if ( authIsDisabled ( options ) ) { return next ( ) ; }
126+
127+ // Nothing will likely be removed, but this allows people to specify that
128+ // permissions should be returned, so this will recalculate permissions
129+ // with the new document data (after changes) and embed it if asked for
130+ return sanitizeDocumentList ( schema , options , doc )
131+ . then ( ( ) => next ( ) )
132+ . catch ( next ) ;
133+ } ) ;
118134 // TODO, WTF, how to prevent someone from Model.find().remove().exec(); That doesn't
119135 // fire any remove hooks. Does it fire a find hook?
120136 schema . pre ( 'remove' , function preRemove ( next , options ) {
@@ -149,13 +165,6 @@ module.exports = (schema, installationOptions) => {
149165 return this ;
150166 } ;
151167
152- // TODO add tests for this function
153- schema . statics . canCreate = async function canCreate ( options ) {
154- // Check just the blank document since nothing exists yet
155- const authLevels = await resolveAuthLevel ( schema , options , { } ) ;
156- return hasPermission ( this . schema , authLevels , 'create' ) ;
157- } ;
158-
159168 const allowedMethods = _ . get ( installationOptions , 'allowedMethods' ) ;
160169 if ( ! _ . includes ( allowedMethods , 'create' ) ) {
161170 schema . statics . create = function cannotCreate ( ) {
0 commit comments