@@ -58,17 +58,34 @@ function create ({
5858 return document
5959 } )
6060
61+ let { hooks} = this . settings
62+ let originalDocuments = documents
63+
64+ // If an ACL check is performed, this variable will contain the resulting
65+ // access matrix.
66+ let aclAccess
67+
6168 return this . validateAccess ( {
6269 client,
70+ documents,
6371 type : 'create'
64- } ) . then ( ( { schema} ) => {
72+ } ) . then ( ( { access , documents : newDocuments , fields , schema} ) => {
6573 if ( ! validate ) return
6674
75+ // Storing the access matrix in a variable that is global to the method.
76+ aclAccess = access
77+
78+ // This is now the filtered documents object, containing only the fields
79+ // which the client has access to.
80+ documents = newDocuments
81+
6782 return this . validator . validateDocuments ( {
6883 documents,
6984 schema
7085 } ) . catch ( errors => {
71- let error = this . _createValidationError ( 'Validation Failed' , errors )
86+ let error = this . _createValidationError ( 'Validation Failed' , errors , {
87+ originalDocuments
88+ } )
7289
7390 return Promise . reject ( error )
7491 } )
@@ -113,16 +130,16 @@ function create ({
113130 return transformQueue
114131 } ) . then ( documents => {
115132 // Run any `beforeCreate` hooks.
116- if ( this . settings . hooks && this . settings . hooks . beforeCreate ) {
133+ if ( hooks && hooks . beforeCreate ) {
117134 return new Promise ( ( resolve , reject ) => {
118135 let processedDocuments = 0
119136
120137 documents . forEach ( ( doc , docIndex ) => {
121- async . reduce ( this . settings . hooks . beforeCreate , doc , ( current , hookConfig , callback ) => {
138+ async . reduce ( hooks . beforeCreate , doc , ( current , hookConfig , callback ) => {
122139 let hook = new Hook ( hookConfig , 'beforeCreate' )
123140
124141 Promise . resolve ( hook . apply ( current , this . schema , this . name , req ) )
125- . then ( ( newDoc ) => {
142+ . then ( newDoc => {
126143 callback ( ( newDoc === null ) ? { } : null , newDoc )
127144 } )
128145 . catch ( err => {
@@ -151,34 +168,31 @@ function create ({
151168 schema : this . schema ,
152169 settings : this . settings
153170 } ) . then ( results => {
154- let returnData = {
155- results
156- }
157-
158171 // Asynchronous search index.
159- this . searchHandler . index ( returnData . results )
172+ this . searchHandler . index ( results )
160173
161174 // Run any `afterCreate` hooks.
162- if ( this . settings . hooks && ( typeof this . settings . hooks . afterCreate === 'object' ) ) {
163- returnData . results . forEach ( document => {
164- this . settings . hooks . afterCreate . forEach ( ( hookConfig , index ) => {
165- let hook = new Hook ( this . settings . hooks . afterCreate [ index ] , 'afterCreate' )
175+ if ( hooks && Array . isArray ( hooks . afterCreate ) ) {
176+ results . forEach ( document => {
177+ hooks . afterCreate . forEach ( ( hookConfig , index ) => {
178+ let hook = new Hook ( hooks . afterCreate [ index ] , 'afterCreate' )
166179
167180 return hook . apply ( document , this . schema , this . name )
168181 } )
169182 } )
170183 }
171184
172- // Prepare result set for output.
173- if ( ! rawOutput ) {
174- return this . formatForOutput (
175- returnData . results ,
176- {
177- composeOverride : compose
178- } ) . then ( results => ( { results} ) )
185+ // If `rawOutput` is truthy, we don't need to worry about formatting
186+ // the result set for output. We return it as is.
187+ if ( rawOutput ) {
188+ return { results}
179189 }
180190
181- return returnData
191+ return this . formatForOutput ( results , {
192+ access : aclAccess && aclAccess . read ,
193+ client,
194+ composeOverride : compose
195+ } ) . then ( results => ( { results} ) )
182196 } )
183197 } )
184198}
0 commit comments