Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit b8216b7

Browse files
DanielBriertonWardormeur
authored andcommitted
Check parent/child relationship both ways (#238)
* Check parent/child relationship both ways
1 parent ad3e1f0 commit b8216b7

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

lib/users/is-parent-of.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function isParentOf (args, cb) {
1212
if(_.isUndefined(childrenId) && args.params.profile) childrenId = args.params.profile.userId;
1313
if(_.isUndefined(childrenId) && args.params.query) childrenId = args.params.query.userId;
1414
if(_.isUndefined(childrenId) && args.params.profileId) childrenProfileId = args.params.profileId;
15-
var isParent = false;
1615
var getChildrenUserId = function (wfCb) {
1716
if (!childrenId && childrenProfileId) {
1817
seneca.act({role: 'cd-profiles', cmd: 'load', id: childrenProfileId}, function (err, profile) {
@@ -24,19 +23,37 @@ function isParentOf (args, cb) {
2423
wfCb();
2524
}
2625
};
26+
// Checks relationship both ways - parent has child, and child has parent
2727
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+
})
3755
};
3856

39-
// Could also check the opposite way, from child to Parent
4057
async.waterfall([
4158
getChildrenUserId,
4259
checkChildrenBelongsToUser

0 commit comments

Comments
 (0)