// admin.js // Copyright (C) 2022 DTP Technologies, LLC // License: Apache-2.0 'use strict'; 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, module.exports); } 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('/announcement', await this.loadChild(path.join(__dirname, 'admin', 'announcement'))); 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('/core-user', await this.loadChild(path.join(__dirname, 'admin', 'core-user'))); 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('/newsroom', await this.loadChild(path.join(__dirname, 'admin', 'newsroom'))); router.use('/otp', await this.loadChild(path.join(__dirname, 'admin', 'otp'))); 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('/service-node', await this.loadChild(path.join(__dirname, 'admin', 'service-node'))); router.use('/site-link', await this.loadChild(path.join(__dirname, 'admin', 'site-link'))); router.use('/user', await this.loadChild(path.join(__dirname, 'admin', 'user'))); router.use('/venue', await this.loadChild(path.join(__dirname, 'admin', 'venue'))); 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 { coreNode: coreNodeService, dashboard: dashboardService, venue: venueService, } = this.dtp.services; res.locals.stats = { userSignupHourly: await dashboardService.getUserSignupsPerHour(), memberCount: await User.estimatedDocumentCount(), constellation: await coreNodeService.getConstellationStats(), }; res.locals.channels = await venueService.getChannels(); res.locals.pageTitle = `Admin Dashbord for ${this.dtp.config.site.name}`; res.render('admin/index'); } } module.exports = { slug: 'admin', name: 'admin', create: async (dtp) => { return new AdminController(dtp); }, };