1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 09:08:06 -05:00
shimapan/app/routes/api/users.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-08-13 05:52:54 -04:00
const express = require('express');
const router = express.Router();
const ModelPath = '../../models/';
const User = require(ModelPath + 'User.js');
const verifyBody = require('../../util/verifyBody');
2019-01-02 16:47:18 -05:00
const authenticate = require('../../util/auth/authenticateRequest');
2018-08-13 05:52:54 -04:00
const getParams = [
{name: 'username', type: 'string', optional: true},
{name: 'displayname', type: 'string', optional: true}];
2019-01-02 16:47:18 -05:00
router.get('/get', authenticate('user.get'), verifyBody(getParams), async (req, res) => {
2018-08-13 05:52:54 -04:00
let query = {};
2018-01-15 11:45:52 -05:00
if (req.body.username)
query.username = req.body.username;
2018-08-13 05:52:54 -04:00
if (req.body.displayname)
query.displayname = req.body.displayname;
//const users = User.find(query, 'username displayname scope uploadCount uploadSize date banned');
const users = await User.find(query);
res.status(200).json(users);
2019-01-02 14:25:51 -05:00
});
2018-08-13 05:52:54 -04:00
const banParams = [{name: 'username', type: 'string'}];
2019-01-02 16:47:18 -05:00
router.post('/ban', authenticate('user.ban'), verifyBody(banParams), async (req, res) => {
2018-08-13 05:52:54 -04:00
const user = await User.findOne({username: req.body.username});
if (!user)
return res.status(422).json({message: 'User not found.'});
if (user.banned)
return res.status(422).json({message: 'User already banned.'});
user.banned = true;
await user.save();
res.status(200).json({message: 'User banned.'});
2019-01-02 14:25:51 -05:00
});
2018-08-13 05:52:54 -04:00
const unbanParams = [{name: 'username', type: 'string'}];
2019-01-02 16:47:18 -05:00
router.post('/unban', authenticate('user.unban'), verifyBody(unbanParams), async (req, res) => {
2018-08-13 05:52:54 -04:00
const user = await User.findOne({username: req.body.username});
if (!user)
return res.status(422).json({message: 'User not found.'});
if (!user.banned)
return res.status(422).json({message: 'User not banned.'});
user.banned = false;
await user.save();
res.status(200).json({message: 'User unbanned.'});
2019-01-02 14:25:51 -05:00
});
2018-01-15 11:45:52 -05:00
module.exports = router;