@@ -12,7 +12,6 @@ function isParentOf (args, cb) {
12
12
if ( _ . isUndefined ( childrenId ) && args . params . profile ) childrenId = args . params . profile . userId ;
13
13
if ( _ . isUndefined ( childrenId ) && args . params . query ) childrenId = args . params . query . userId ;
14
14
if ( _ . isUndefined ( childrenId ) && args . params . profileId ) childrenProfileId = args . params . profileId ;
15
- var isParent = false ;
16
15
var getChildrenUserId = function ( wfCb ) {
17
16
if ( ! childrenId && childrenProfileId ) {
18
17
seneca . act ( { role : 'cd-profiles' , cmd : 'load' , id : childrenProfileId } , function ( err , profile ) {
@@ -24,19 +23,37 @@ function isParentOf (args, cb) {
24
23
wfCb ( ) ;
25
24
}
26
25
} ;
26
+ // Checks relationship both ways - parent has child, and child has parent
27
27
var checkChildrenBelongsToUser = function ( wfCb ) {
28
- seneca . act ( { role : 'cd-profiles' , cmd : 'load_user_profile' , userId : userId } ,
29
- function ( err , user ) {
30
- if ( err ) {
31
- seneca . log . error ( seneca . customValidatorLogFormatter ( 'cd-profiles' , 'isParentOf' , err , { userId : userId , childrenId : childrenId } ) ) ;
32
- return cb ( null , { 'allowed' : false } ) ;
33
- }
34
- if ( _ . includes ( user . children , childrenId ) ) isParent = true ;
35
- return cb ( null , { 'allowed' : isParent } ) ;
36
- } ) ;
28
+ async . parallel ( {
29
+ parentHasChild : function ( done ) {
30
+ seneca . act ( { role : 'cd-profiles' , cmd : 'load_user_profile' , userId : userId } ,
31
+ function ( err , user ) {
32
+ if ( err ) {
33
+ seneca . log . error ( seneca . customValidatorLogFormatter ( 'cd-profiles' , 'isParentOf' , err , { userId : userId , childrenId : childrenId } ) ) ;
34
+ return done ( null , false ) ;
35
+ }
36
+ return done ( null , _ . includes ( user . children , childrenId ) ) ;
37
+ } ) ;
38
+ } ,
39
+ childHasParent : function ( done ) {
40
+ seneca . act ( { role : 'cd-profiles' , cmd : 'load_user_profile' , userId : childrenId } ,
41
+ function ( err , user ) {
42
+ if ( err ) {
43
+ seneca . log . error ( seneca . customValidatorLogFormatter ( 'cd-profiles' , 'isParentOf' , err , { userId : userId , childrenId : childrenId } ) ) ;
44
+ return done ( null , false ) ;
45
+ }
46
+ return done ( null , _ . includes ( user . parents , userId ) ) ;
47
+ } ) ;
48
+ }
49
+ } , function ( err , results ) {
50
+ if ( err ) {
51
+ return cb ( null , { 'allowed' : false } ) ;
52
+ }
53
+ return cb ( null , { 'allowed' : results . parentHasChild && results . childHasParent } ) ;
54
+ } )
37
55
} ;
38
56
39
- // Could also check the opposite way, from child to Parent
40
57
async . waterfall ( [
41
58
getChildrenUserId ,
42
59
checkChildrenBelongsToUser
0 commit comments