1
0
mirror of https://github.com/Foltik/Shimapan synced 2025-01-22 23:00:37 -05:00
shimapan/app/routes/api/invites.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

const express = require('express');
const router = express.Router();
const crypto = require('crypto');
2018-08-01 11:54:35 -04:00
const ModelPath = '../../models/';
const Invite = require(ModelPath + 'Invite.js');
const User = require(ModelPath + 'User.js');
2019-01-02 16:47:18 -05:00
const authenticate = require('../../util/auth/authenticateRequest');
const verifyBody = require('../../util/verifyBody');
2019-01-02 16:47:18 -05:00
const createParams = [{name: 'scope', instance: Array}];
2019-01-02 16:47:18 -05:00
router.post('/create', authenticate('invite.create'), verifyBody(createParams), async (req, res, next) => {
// Make sure the user has all the request scope
const inviteScope = req.body.scope;
if (!inviteScope.every(s => req.scope.includes(s)))
return res.status(403).json({message: 'Requested scope exceeds own scope.'});
const invite = {
code: crypto.randomBytes(12).toString('hex'),
2019-01-02 16:47:18 -05:00
scope: inviteScope,
issuer: req.username,
issued: Date.now(),
expires: req.body.expires
};
await Promise.all([
Invite.create(invite).catch(next),
2018-08-01 12:20:35 -04:00
User.updateOne({username: req.username}, {$inc: {inviteCount: 1}})
]);
res.status(200).json({
message: 'Invite created.',
code: invite.code
});
2019-01-02 14:25:51 -05:00
});
2019-01-02 16:47:18 -05:00
const deleteParams = [{name: 'code', type: 'string'}];
2019-01-02 16:47:18 -05:00
router.post('/delete', authenticate('invite.delete'), verifyBody(deleteParams), async (req, res, next) => {
let query = {code: req.body.code};
// Users need a permission to delete invites other than their own
2019-01-02 16:47:18 -05:00
if (!req.scope.includes('invite.delete.others'))
query.issuer = req.username;
// Find the invite
const invite = await Invite.findOne(query).catch(next);
if (!invite)
return res.status(422).json({message: 'Invite not found.'});
// Users need a permission to delete invites that have been used
2019-01-02 16:47:18 -05:00
if (!req.scope.includes('invite.delete.used') && invite.used != null && invite.recipient != null)
return res.status(403).json({message: 'Forbidden to delete used invites.'});
await Invite.deleteOne({_id: invite._id}).catch(next);
res.status(200).json({message: 'Invite deleted.'});
2019-01-02 14:25:51 -05:00
});
2019-01-02 16:47:18 -05:00
const getParams = [
{name: 'code', type: 'string', optional: true},
{name: 'issuer', type: 'string', optional: true}];
router.get('/get', authenticate('invite.get'), verifyBody(getParams), async (req, res, next) => {
let query = {};
// Users need a permission to list invites other than their own
2019-01-02 16:47:18 -05:00
if (!req.scope.includes('invite.get.others'))
query.issuer = req.username;
else if (req.body.issuer)
query.issuer = req.body.issuer;
// Narrow down the query by code if specified
if (req.body.code)
query.code = req.body.code;
const invites = await Invite.find(query).catch(next);
res.status(200).json(invites);
2019-01-02 14:25:51 -05:00
});
2019-01-02 16:47:18 -05:00
module.exports = router;