@@ -8,19 +8,39 @@ function isParentOf (args, cb) {
8
8
var plugin = args . role ;
9
9
var userId = args . user . id ;
10
10
var childrenId = args . params . userId ;
11
+ var childrenProfileId ;
11
12
if ( _ . isUndefined ( childrenId ) && args . params . profile ) childrenId = args . params . profile . userId ;
12
13
if ( _ . isUndefined ( childrenId ) && args . params . query ) childrenId = args . params . query . userId ;
14
+ if ( _ . isUndefined ( childrenId ) && args . params . profileId ) childrenProfileId = args . params . profileId ;
13
15
var isParent = false ;
16
+ var getChildrenUserId = function ( wfCb ) {
17
+ if ( ! childrenId && childrenProfileId ) {
18
+ seneca . act ( { role : 'cd-profiles' , cmd : 'load' , id : childrenProfileId } , function ( err , profile ) {
19
+ if ( err ) return cb ( null , { 'allowed' : false } ) ;
20
+ childrenId = profile . userId ;
21
+ wfCb ( ) ;
22
+ } ) ;
23
+ } else {
24
+ wfCb ( ) ;
25
+ }
26
+ } ;
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
+ } ) ;
37
+ } ;
38
+
14
39
// Could also check the opposite way, from child to Parent
15
- seneca . act ( { role : 'cd-profiles' , cmd : 'load_user_profile' , userId : userId } ,
16
- function ( err , user ) {
17
- if ( err ) {
18
- seneca . log . error ( seneca . customValidatorLogFormatter ( 'cd-profiles' , 'isParentOf' , err , { userId : userId , childrenId : childrenId } ) ) ;
19
- return cb ( null , { 'allowed' : false } ) ;
20
- }
21
- if ( _ . includes ( user . children , childrenId ) ) isParent = true ;
22
- return cb ( null , { 'allowed' : isParent } ) ;
23
- } ) ;
40
+ async . waterfall ( [
41
+ getChildrenUserId ,
42
+ checkChildrenBelongsToUser
43
+ ] ) ;
24
44
}
25
45
26
46
module . exports = isParentOf ;
0 commit comments