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

Commit 08bded2

Browse files
authored
Add new permission to ensure a user has the right to accept/refuse a … (#306)
* Add new permission to ensure a user has the right to accept/refuse a membership request * Add missing quote to the example command
1 parent 58310a3 commit 08bded2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/users/can-accept-join-request.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
var _ = require('lodash');
3+
4+
/**
5+
* @param {Object} user
6+
* @param {String} requestId
7+
* @example curl http://localhost:10303/act -H "Content-type: application/json" --data-binary '{"role": "cd-users", "cmd":"can_accept_join_request", "params":{"requestId": "xxxx"}, "user": { "id": "xxxxx" }}'
8+
*/
9+
function canAcceptJoinRequest (args, cb) {
10+
var seneca = this;
11+
var plugin = args.role;
12+
var userId = args.user.id;
13+
var requestId = args.params.requestId;
14+
var membershipRequest = null;
15+
if (_.isUndefined(requestId)) {
16+
requestId = args.params.id;
17+
}
18+
// Could check upon profile, but seems like an overkill to me
19+
seneca.act({ role: 'cd-users', domain: 'join_requests', cmd: 'search', query: { id: requestId } }, (err, res) => {
20+
if (err) return cb(null, { allowed: false }); // Force the authorisation to return falsy
21+
if (res.length === 1) {
22+
membershipRequest = res[0];
23+
seneca.act({ role: 'cd-dojos', cmd: 'have_permissions_on_dojo', params: { dojoId: membershipRequest.dojoId }, user: args.user, perm: 'dojo-admin' }, (err, res) => {
24+
if (err) return cb(null, { allowed: false });
25+
return cb(null, { allowed: res.allowed });
26+
});
27+
} else {
28+
// More than one result for a single id
29+
// That's not supposed to happen..
30+
return cb(null, { allowed: false });
31+
}
32+
});
33+
}
34+
35+
module.exports = canAcceptJoinRequest;

users.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = function (options) {
3838
seneca.add({role: plugin, cmd: 'kpi_number_of_youth_females_registered'}, cmd_kpi_number_of_youth_females_registered);
3939
seneca.add({role: plugin, cmd: 'is_self'}, require('./lib/users/is-self'));
4040
seneca.add({role: plugin, cmd: 'is_parent_of'}, require('./lib/users/is-parent-of'));
41+
seneca.add({role: plugin, cmd: 'can_accept_join_request'}, require('./lib/users/can-accept-join-request'));
4142
seneca.add({role: plugin, ctrl: 'user', cmd: 'load'}, require('./lib/controllers/users/load'));
4243
// LMS Integration
4344
seneca.add({role: plugin, cmd: 'get_lms_link'}, require('./lib/users/lms/get-lms-link'));

0 commit comments

Comments
 (0)