|
| 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; |
0 commit comments