The DTP Sites web app development engine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

86 lines
2.3 KiB

// admin.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0
'use strict';
const DTP_COMPONENT_NAME = 'admin';
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const User = mongoose.model('User');
const { SiteError, SiteController } = require('../../lib/site-lib');
class AdminController extends SiteController {
constructor (dtp) {
super(dtp, DTP_COMPONENT_NAME);
}
async start ( ) {
const { otpAuth: otpAuthService } = this.dtp.services;
const router = express.Router();
this.dtp.app.use('/admin', router);
router.use(
async (req, res, next) => {
res.locals.currentView = 'admin';
res.locals.adminView = 'home';
if (!req.user || !req.user.flags.isAdmin) {
return next(new SiteError(403, 'Administrative privileges required'));
}
return next();
},
otpAuthService.middleware('Admin', {
adminRequired: true,
otpRequired: true,
otpRedirectURL: '/admin',
}),
);
router.use('/content-report',await this.loadChild(path.join(__dirname, 'admin', 'content-report')));
router.use('/core-node',await this.loadChild(path.join(__dirname, 'admin', 'core-node')));
router.use('/host',await this.loadChild(path.join(__dirname, 'admin', 'host')));
router.use('/job-queue', await this.loadChild(path.join(__dirname, 'admin', 'job-queue')));
router.use('/log', await this.loadChild(path.join(__dirname, 'admin', 'log')));
router.use('/settings', await this.loadChild(path.join(__dirname, 'admin', 'settings')));
router.use('/user', await this.loadChild(path.join(__dirname, 'admin', 'user')));
router.get('/diagnostics', this.getDiagnostics.bind(this));
router.get('/', this.getHomeView.bind(this));
return router;
}
async getDiagnostics (req, res) {
res.status(200).json({
success: true,
url: req.url,
ip: req.ip,
headers: req.headers,
});
}
async getHomeView (req, res) {
res.locals.stats = {
memberCount: await User.estimatedDocumentCount(),
};
res.render('admin/index');
}
}
module.exports = {
slug: 'admin',
name: 'admin',
create: async (dtp) => {
let controller = new AdminController(dtp);
return controller;
},
};