Implemented permabans
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
const { accounts, tokens } = require('../database/models');
|
||||
const Sequelize = require('sequelize');
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
//admin/banuser
|
||||
const route = async (req, res) => {
|
||||
const updated = await accounts.update({
|
||||
banned: true
|
||||
}, {
|
||||
where: {
|
||||
username: {
|
||||
[Op.eq]: req.body.username
|
||||
},
|
||||
admin: {
|
||||
[Op.not]: true
|
||||
},
|
||||
mod: {
|
||||
[Op.not]: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!updated[0]) {
|
||||
return res.status(500).send('Failed to set banned status');
|
||||
}
|
||||
|
||||
//forcibly logout
|
||||
tokens.destroy({
|
||||
where: {
|
||||
username: req.body.username
|
||||
}
|
||||
});
|
||||
|
||||
res.status(200).end();
|
||||
};
|
||||
|
||||
module.exports = route;
|
||||
@@ -1,10 +1,46 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const { accounts } = require('../database/models');
|
||||
|
||||
//middleware
|
||||
const tokenAuth = require('../utilities/token-auth');
|
||||
|
||||
router.use(tokenAuth);
|
||||
|
||||
//handle ban stuff
|
||||
router.use(async (req, res, next) => {
|
||||
const record = await accounts.findOne({
|
||||
where: {
|
||||
username: req.user.username
|
||||
}
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
return res.status(500).send('Account not found in banning middleware');
|
||||
}
|
||||
|
||||
if (record.banned) {
|
||||
return res.status(403).send('This account has been banned');
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
//handle mod stuff
|
||||
router.use((req, res, next) => {
|
||||
//check the user's mod status
|
||||
if (!req.user.mod) {
|
||||
return res.status(401).send('Mods only');
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
//routes
|
||||
router.post('/banuser', require('./ban-user'));
|
||||
|
||||
//handle admin stuff
|
||||
router.use((req, res, next) => {
|
||||
//check the user's admin status
|
||||
if (!req.user.admin) {
|
||||
|
||||
Reference in New Issue
Block a user