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.
89 lines
2.5 KiB
89 lines
2.5 KiB
// admin.js
|
|
// Copyright (C) 2021 Digital Telepresence, 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('/host', await this.loadChild(path.join(__dirname, 'admin', 'host')));
|
|
router.use('/job-queue', await this.loadChild(path.join(__dirname, 'admin', 'job-queue')));
|
|
router.use('/link', await this.loadChild(path.join(__dirname, 'admin', 'link')));
|
|
router.use('/log', await this.loadChild(path.join(__dirname, 'admin', 'log')));
|
|
router.use('/newsletter', await this.loadChild(path.join(__dirname, 'admin', 'newsletter')));
|
|
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) {
|
|
const { link: linkService, dashboard: dashboardService } = this.dtp.services;
|
|
res.locals.stats = {
|
|
memberCount: await User.estimatedDocumentCount(),
|
|
linkCount: await linkService.getTotalCount(),
|
|
userSignupHourly: await dashboardService.getUserSignupsPerHour(),
|
|
};
|
|
res.render('admin/index');
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'admin',
|
|
name: 'admin',
|
|
create: async (dtp) => {
|
|
let controller = new AdminController(dtp);
|
|
return controller;
|
|
},
|
|
};
|