added delete admin user and list admin users

This commit is contained in:
mrjvs
2018-10-14 19:58:34 +02:00
parent 9e7626ffa1
commit e649a165b2
3 changed files with 78 additions and 0 deletions

View File

@@ -90,6 +90,63 @@ router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationReq
});
});
/*
* /admin/api/v1/removeadmin
* - requires admin auth
*
* registers a new admin user
*
* post {
* id - id of the admin user
* }
* return {
* code: httpcode
* success: boolean - true if delete was successull
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
if (!req.body) {
common.sendApiGenericError(res);
return;
}
const { id } = req.body;
adminUser.adminUserModel.findByIdAndDelete(id, (err) => {
if (err) return common.sendApiError(res, 500, [err]);
// successfull
common.sendApiReturn(res, {});
});
});
/*
* /admin/api/v1/listadmins
* - requires admin auth
*
* gets list of admins
*
* return {
* code: httpcode
* success: boolean - true if delete was successull
* errors: Strings[messages]
* }
*/
router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
adminUser.adminUserModel.find({}, (err, admins) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return common.sendApiError(res, 500, [err]);
const output = [];
for (let i = 0, l = admins.length; i < l; i++) {
admins[i].password = undefined;
output.push(admins[i]);
}
common.sendApiReturn(res, {
admins: output
});
});
});
/*
* /admin/api/v1/check
*

12
setupAdminUser.js Normal file
View File

@@ -0,0 +1,12 @@
const adminUser = require('./models/admin-user');
const newUser = new adminUser.adminUserModel({
username: 'admin',
password: 'root'
});
newUser.save().then(() => {
console.log('success, user admin pass root');
}).catch((rejection) => {
console.log(rejection);
});

View File

@@ -20,6 +20,13 @@
<button>submit login</button>
</form>
<h2>delete admin</h2>
<form action="/admin/api/v1/removeadmin" method="POST">
<p>id</p>
<input type="text" name="id">
<button>delete admin</button>
</form>
<h2>create blog post</h2>
<form action="/admin/api/v1/newpost" method="POST">
<p>content</p>
@@ -102,6 +109,8 @@
<button>edit progress</button>
</form>
<br>
<a href="/admin/api/v1/listadmins">List of admin users</a>
<br>
<a href="/api/v1/listblog">List of blog posts</a>
<br>