2018-02-17 11:17:07 +09:00
import Users from '../modules/index.js' ;
2018-06-16 08:56:44 +09:00
import { runCallbacks , runCallbacksAsync , Utils , debug , debugGroup , debugGroupEnd } from 'meteor/vulcan:lib' ; // import from vulcan:lib because vulcan:core isn't loaded yet
2017-04-29 12:31:40 +09:00
2018-03-03 15:04:33 +09:00
// TODO: the following should use async/await, but async/await doesn't seem to work with Accounts.onCreateUser
2018-06-16 08:56:44 +09:00
function onCreateUserCallback ( options , user ) {
debug ( '' ) ;
debugGroup ( ` --------------- start \x 1b[35m onCreateUser --------------- ` ) ;
debug ( ` Options: ${ JSON . stringify ( options ) } ` ) ;
debug ( ` User: ${ JSON . stringify ( user ) } ` ) ;
2017-04-29 12:31:40 +09:00
const schema = Users . simpleSchema ( ) . _schema ;
delete options . password ; // we don't need to store the password digest
delete options . username ; // username is already in user object
2018-06-15 10:18:20 +09:00
options = runCallbacks ( ` user.new.validate.before ` , options ) ;
// OpenCRUD backwards compatibility
2017-12-20 09:43:30 +09:00
options = runCallbacks ( ` users.new.validate.before ` , options ) ;
2017-11-22 16:39:00 +02:00
2017-04-29 12:31:40 +09:00
// validate options since they can't be trusted
Users . simpleSchema ( ) . validate ( options ) ;
// check that the current user has permission to insert each option field
_ . keys ( options ) . forEach ( fieldName => {
var field = schema [ fieldName ] ;
2018-06-21 14:27:20 +02:00
if ( ! field || ! Users . canCreateField ( user , field ) ) {
2018-06-16 08:56:44 +09:00
throw new Error ( Utils . encodeIntlError ( { id : 'app.disallowed_property_detected' , value : fieldName } ) ) ;
2017-04-29 12:31:40 +09:00
}
} ) ;
// extend user with options
user = Object . assign ( user , options ) ;
// run validation callbacks
2018-06-15 10:18:20 +09:00
user = runCallbacks ( ` user.new.validate ` , user ) ;
// OpenCRUD backwards compatibility
2017-04-29 12:31:40 +09:00
user = runCallbacks ( ` users.new.validate ` , user ) ;
// run onInsert step
2018-06-16 08:56:44 +09:00
// note: cannot use forEach with async/await.
// note 2: don't use async/await here for now. TODO: fix this.
2018-03-03 15:04:33 +09:00
// See https://stackoverflow.com/a/37576787/649299
2018-06-16 08:56:44 +09:00
for ( let fieldName of _ . keys ( schema ) ) {
2017-04-29 12:31:40 +09:00
if ( ! user [ fieldName ] && schema [ fieldName ] . onInsert ) {
const autoValue = schema [ fieldName ] . onInsert ( user , options ) ;
2018-03-03 15:04:33 +09:00
if ( typeof autoValue !== 'undefined' ) {
2017-04-29 12:31:40 +09:00
user [ fieldName ] = autoValue ;
}
}
2018-03-03 15:04:33 +09:00
}
2018-06-16 08:56:44 +09:00
user = runCallbacks ( 'users.new.sync' , user ) ;
2017-04-29 12:31:40 +09:00
2018-06-15 10:18:20 +09:00
runCallbacksAsync ( "user.new.async" , user ) ;
// OpenCRUD backwards compatibility
2017-04-29 12:31:40 +09:00
runCallbacksAsync ( "users.new.async" , user ) ;
// check if all required fields have been filled in. If so, run profile completion callbacks
if ( Users . hasCompletedProfile ( user ) ) {
2018-06-15 10:18:20 +09:00
runCallbacksAsync ( "user.profileCompleted.async" , user ) ;
// OpenCRUD backwards compatibility
2017-04-29 12:31:40 +09:00
runCallbacksAsync ( "users.profileCompleted.async" , user ) ;
}
2018-06-16 08:56:44 +09:00
debug ( ` Modified User: ${ JSON . stringify ( user ) } ` ) ;
debugGroupEnd ( ) ;
debug ( ` --------------- end \x 1b[35m onCreateUser --------------- ` ) ;
debug ( '' ) ;
2017-04-29 12:31:40 +09:00
return user ;
}
2018-06-16 08:56:44 +09:00
Meteor . startup ( ( ) => {
if ( typeof Accounts !== 'undefined' ) {
Accounts . onCreateUser ( onCreateUserCallback ) ;
}
} ) ;