// 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('/content-report',await this.loadChild(path.join(__dirname, 'admin', 'content-report'))); 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('/newsletter', await this.loadChild(path.join(__dirname, 'admin', 'newsletter'))); router.use('/page', await this.loadChild(path.join(__dirname, 'admin', 'page'))); router.use('/post', await this.loadChild(path.join(__dirname, 'admin', 'post'))); 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; }, };