@@ -148,8 +148,9 @@ module.exports = function (options) {
148
148
function saveProfile ( done ) {
149
149
var profileKeys = _ . keys ( profile ) ;
150
150
var missingKeys = _ . difference ( requiredProfileFields , profileKeys ) ;
151
+ var userId = args . user ? args . user . id : null ;
151
152
if ( _ . isEmpty ( missingKeys ) ) profile . requiredFieldsComplete = true ;
152
- if ( args . user . id !== profile . userId ) return done ( null , new Error ( 'Profiles can only be saved by the profile user.' ) ) ;
153
+ if ( userId !== profile . userId ) return done ( null , new Error ( 'Profiles can only be saved by the profile user.' ) ) ;
153
154
if ( profile . id ) {
154
155
profile = _ . omit ( profile , immutableFields ) ;
155
156
}
@@ -235,8 +236,9 @@ module.exports = function (options) {
235
236
236
237
function cmd_save_youth_profile ( args , done ) {
237
238
var profile = args . profile ;
239
+ var userId = args . user ? args . user . id : null ;
238
240
profile . parents = [ ] ;
239
- profile . parents . push ( args . user . id ) ;
241
+ profile . parents . push ( userId ) ;
240
242
241
243
if ( profile . id ) {
242
244
profile = _ . omit ( profile , immutableFields ) ;
@@ -270,8 +272,8 @@ module.exports = function (options) {
270
272
profile . userType = data && data . user && data . user . initUserType && data . user . initUserType . name ;
271
273
272
274
profile = _ . omit ( profile , [ 'userTypes' , 'password' ] ) ;
273
-
274
- saveChild ( profile , args . user . id , done ) ;
275
+ var userId = args . user ? args . user . id : null ;
276
+ saveChild ( profile , userId , done ) ;
275
277
} ) ;
276
278
}
277
279
@@ -326,7 +328,8 @@ module.exports = function (options) {
326
328
}
327
329
328
330
function cmd_update_youth ( args , done ) {
329
- if ( ! _ . contains ( args . profile . parents , args . user . id ) ) {
331
+ var userId = args . user ? args . user . id : null ;
332
+ if ( ! _ . contains ( args . profile . parents , userId ) ) {
330
333
return done ( new Error ( 'Not authorized to update profile' ) ) ;
331
334
}
332
335
var profile = args . profile ;
@@ -476,8 +479,9 @@ module.exports = function (options) {
476
479
}
477
480
478
481
function addFlags ( profile , done ) {
479
- profile . ownProfileFlag = profile && profile . userId === args . user . id ;
480
- profile . myChild = _ . contains ( profile . parents , args . user . id ) ;
482
+ var userId = args . user ? args . user . id : null ;
483
+ profile . ownProfileFlag = profile && profile . userId === userId ;
484
+ profile . myChild = _ . contains ( profile . parents , userId ) ;
481
485
profile . isTicketingAdmin = _ . find ( profile . userPermissions , function ( profileUserPermission ) {
482
486
return profileUserPermission . name === 'ticketing-admin' ;
483
487
} ) ;
@@ -488,13 +492,13 @@ module.exports = function (options) {
488
492
seneca . act ( { role : 'cd-users' , cmd : 'load_champions_for_user' , userId : profile . userId } , function ( err , champions ) {
489
493
if ( err ) return done ( err ) ;
490
494
profile . requestingUserIsChampion = _ . find ( champions , function ( champion ) {
491
- return champion . id === args . user . id ;
495
+ return champion . id === args . user ? args . user . id : null ;
492
496
} ) ;
493
497
494
498
seneca . act ( { role : 'cd-users' , cmd : 'load_dojo_admins_for_user' , userId : profile . userId , user : args . user } , function ( err , dojoAdmins ) {
495
499
if ( err ) return done ( err ) ;
496
500
profile . requestingUserIsDojoAdmin = _ . find ( dojoAdmins , function ( dojoAdmin ) {
497
- return dojoAdmin . id === args . user . id ;
501
+ return dojoAdmin . id === args . user ? args . user . id : null ;
498
502
} ) ;
499
503
500
504
var allowedFields = [ ] ;
@@ -568,7 +572,8 @@ module.exports = function (options) {
568
572
569
573
function under13Filter ( profile , done ) {
570
574
// Ensure that only parents of children can retrieve their full public profile
571
- if ( _ . contains ( profile . userTypes , 'attendee-u13' ) && ! _ . contains ( profile . parents , args . user . id ) && ! profile . requestingUserIsChampion && ! profile . requestingUserIsDojoAdmin ) {
575
+ var userId = args . user ? args . user . id : null ;
576
+ if ( _ . contains ( profile . userTypes , 'attendee-u13' ) && ! _ . contains ( profile . parents , userId ) && ! profile . requestingUserIsChampion && ! profile . requestingUserIsDojoAdmin ) {
572
577
profile = { } ;
573
578
return done ( null , profile ) ;
574
579
}
@@ -727,7 +732,7 @@ module.exports = function (options) {
727
732
var data = args . data ;
728
733
var inviteTokenId = data . inviteToken ;
729
734
var childProfileId = data . childProfileId ;
730
- var requestingUserId = args . user . id ;
735
+ var requestingUserId = args . user ? args . user . id : null ;
731
736
732
737
async . waterfall ( [
733
738
validateRequestingUserIsParent ,
@@ -1027,17 +1032,19 @@ module.exports = function (options) {
1027
1032
seneca . act ( { role : plugin , cmd : 'list' , query : { email : ninjaEmail } } , function ( err , ninjaProfiles ) {
1028
1033
if ( err ) return done ( err ) ;
1029
1034
var ninjaProfile = ninjaProfiles [ 0 ] ;
1030
- if ( ninjaProfile && _ . contains ( ninjaProfile . parents , args . user . id ) ) return done ( new Error ( 'User is already a parent of this Ninja' ) ) ;
1035
+ var userId = args . user ? args . user . id : null ;
1036
+ if ( ninjaProfile && _ . contains ( ninjaProfile . parents , userId ) ) return done ( new Error ( 'User is already a parent of this Ninja' ) ) ;
1031
1037
return done ( ) ;
1032
1038
} ) ;
1033
1039
}
1034
1040
1035
1041
function validateRequestingUserIsParent ( done ) {
1036
- seneca . act ( { role : 'cd-dojos' , cmd : 'load_usersdojos' , query : { userId : args . user . id } } , function ( err , usersDojos ) {
1042
+ var userId = args . user ? args . user . id : null ;
1043
+ seneca . act ( { role : 'cd-dojos' , cmd : 'load_usersdojos' , query : { userId : userId } } , function ( err , usersDojos ) {
1037
1044
if ( err ) return done ( err ) ;
1038
1045
if ( _ . isEmpty ( usersDojos ) ) {
1039
1046
// Not yet a member of any Dojo, check the user type in their profile.
1040
- seneca . act ( { role : plugin , cmd : 'list' } , { query : { userId : args . user . id } } , function ( err , parentProfiles ) {
1047
+ seneca . act ( { role : plugin , cmd : 'list' } , { query : { userId : userId } } , function ( err , parentProfiles ) {
1041
1048
if ( err ) return done ( err ) ;
1042
1049
var parentProfile = parentProfiles [ 0 ] ;
1043
1050
if ( parentProfile . userType === 'parent-guardian' ) return done ( ) ;
@@ -1075,7 +1082,8 @@ module.exports = function (options) {
1075
1082
}
1076
1083
1077
1084
function loadParentProfile ( validationResponse , done ) {
1078
- seneca . act ( { role : plugin , cmd : 'list' } , { query : { userId : args . user . id } } , done ) ;
1085
+ var userId = args . user ? args . user . id : null ;
1086
+ seneca . act ( { role : plugin , cmd : 'list' } , { query : { userId : userId } } , done ) ;
1079
1087
}
1080
1088
1081
1089
function addTokenToParentProfile ( parentProfiles , done ) {
@@ -1137,7 +1145,8 @@ module.exports = function (options) {
1137
1145
return ninjaInvite . id === inviteData . inviteTokenId ;
1138
1146
} ) ;
1139
1147
if ( ! inviteTokenFound ) return done ( new Error ( 'Invalid token' ) ) ;
1140
- seneca . act ( { role : plugin , cmd : 'list' , query : { userId : args . user . id } } , function ( err , ninjaProfiles ) {
1148
+ var userId = args . user ? args . user . id : null ;
1149
+ seneca . act ( { role : plugin , cmd : 'list' , query : { userId : userId } } , function ( err , ninjaProfiles ) {
1141
1150
if ( err ) return done ( err ) ;
1142
1151
ninjaProfile = ninjaProfiles [ 0 ] ;
1143
1152
if ( ninjaProfile . email !== inviteTokenFound . ninjaEmail ) return done ( new Error ( 'You cannot approve invite Ninja requests for other users.' ) ) ;
0 commit comments