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

Commit ad3e1f0

Browse files
WardormeurDanielBrierton
authored andcommitted
Allow parents to upload profile images for their child
1 parent 2cb7f08 commit ad3e1f0

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

config/perm/profiles.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ module.exports = function(){
8585
customValidator: [{
8686
role: 'cd-profiles',
8787
cmd: 'is_own_profile'
88-
}]}],
88+
}]
89+
},
90+
{ role: 'basic-user',
91+
customValidator: [{
92+
role: 'cd-users',
93+
cmd: 'is_parent_of'
94+
}]
95+
}],
8996
'get_avatar': [{
9097
role: 'none',
9198
}],

lib/users/is-parent-of.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,39 @@ function isParentOf (args, cb) {
88
var plugin = args.role;
99
var userId = args.user.id;
1010
var childrenId = args.params.userId;
11+
var childrenProfileId;
1112
if(_.isUndefined(childrenId) && args.params.profile) childrenId = args.params.profile.userId;
1213
if(_.isUndefined(childrenId) && args.params.query) childrenId = args.params.query.userId;
14+
if(_.isUndefined(childrenId) && args.params.profileId) childrenProfileId = args.params.profileId;
1315
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+
1439
// 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+
]);
2444
}
2545

2646
module.exports = isParentOf;

0 commit comments

Comments
 (0)