Sfoglia il codice sorgente

User promotion/demotion

Pitu 6 anni fa
parent
commit
80732ff90a

+ 27 - 0
src/api/routes/admin/userDemote.js

@@ -0,0 +1,27 @@
+const Route = require('../../structures/Route');
+
+class userDemote extends Route {
+	constructor() {
+		super('/admin/users/demote', 'get', { adminOnly: true });
+	}
+
+	async run(req, res, db) {
+		if (!req.body) return res.status(400).json({ message: 'No body provided' });
+		const { id } = req.body;
+		if (!id) return res.status(400).json({ message: 'No name provided' });
+
+		try {
+			await db.table('users')
+				.where({ id })
+				.update({ isAdmin: false });
+		} catch (error) {
+			return super.error(res, error);
+		}
+
+		return res.json({
+			message: 'Successfully promoted user'
+		});
+	}
+}
+
+module.exports = userDemote;

+ 27 - 0
src/api/routes/admin/userPromote.js

@@ -0,0 +1,27 @@
+const Route = require('../../structures/Route');
+
+class userPromote extends Route {
+	constructor() {
+		super('/admin/users/promote', 'get', { adminOnly: true });
+	}
+
+	async run(req, res, db) {
+		if (!req.body) return res.status(400).json({ message: 'No body provided' });
+		const { id } = req.body;
+		if (!id) return res.status(400).json({ message: 'No name provided' });
+
+		try {
+			await db.table('users')
+				.where({ id })
+				.update({ isAdmin: true });
+		} catch (error) {
+			return super.error(res, error);
+		}
+
+		return res.json({
+			message: 'Successfully promoted user'
+		});
+	}
+}
+
+module.exports = userPromote;

+ 23 - 0
src/api/routes/admin/usersGET.js

@@ -0,0 +1,23 @@
+const Route = require('../../structures/Route');
+
+class usersGET extends Route {
+	constructor() {
+		super('/admin/users', 'get', { adminOnly: true });
+	}
+
+	async run(req, res, db) {
+		try {
+			const users = await db.table('users')
+				.select('id', 'username', 'enabled', 'isAdmin', 'createdAt');
+
+			return res.json({
+				message: 'Successfully retrieved users',
+				users
+			});
+		} catch (error) {
+			return super.error(res, error);
+		}
+	}
+}
+
+module.exports = usersGET;