wikijs-fork/controllers/admin.js

89 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-08-17 03:56:08 +00:00
"use strict";
var express = require('express');
var router = express.Router();
2016-11-21 01:09:50 +00:00
const Promise = require('bluebird');
2017-01-04 05:07:15 +00:00
const validator = require('validator');
2016-08-17 03:56:08 +00:00
/**
* Admin
*/
router.get('/', (req, res) => {
2016-11-02 03:02:11 +00:00
res.redirect('/admin/profile');
});
router.get('/profile', (req, res) => {
if(res.locals.isGuest) {
return res.render('error-forbidden');
}
2016-11-21 01:09:50 +00:00
res.render('pages/admin/profile', { adminTab: 'profile' });
2016-11-21 01:09:50 +00:00
});
router.get('/stats', (req, res) => {
if(res.locals.isGuest) {
return res.render('error-forbidden');
}
2016-11-21 01:09:50 +00:00
Promise.all([
db.Entry.count(),
db.UplFile.count(),
db.User.count()
]).spread((totalEntries, totalUploads, totalUsers) => {
return res.render('pages/admin/stats', {
totalEntries, totalUploads, totalUsers,
adminTab: 'stats'
}) || true;
}).catch((err) => {
throw err;
});
2016-11-21 01:09:50 +00:00
});
router.get('/users', (req, res) => {
if(!res.locals.rights.manage) {
return res.render('error-forbidden');
}
2017-01-04 05:07:15 +00:00
db.User.find({})
.select('-password -rights')
.sort('name email')
.exec().then((usrs) => {
res.render('pages/admin/users', { adminTab: 'users', usrs });
});
});
router.get('/users/:id', (req, res) => {
if(!res.locals.rights.manage) {
return res.render('error-forbidden');
}
if(!validator.isMongoId(req.params.id)) {
return res.render('error-forbidden');
}
db.User.findById(req.params.id)
.select('-password')
.exec().then((usr) => {
res.render('pages/admin/users-edit', { adminTab: 'users', usr });
});
2016-11-21 01:09:50 +00:00
});
router.get('/settings', (req, res) => {
if(!res.locals.rights.manage) {
return res.render('error-forbidden');
}
2016-11-21 01:09:50 +00:00
res.render('pages/admin/settings', { adminTab: 'settings' });
2016-08-17 03:56:08 +00:00
});
module.exports = router;