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.
121 lines
3.5 KiB
121 lines
3.5 KiB
// admin/user.js
|
|
// Copyright (C) 2022 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
|
|
const { SiteController } = require('../../../lib/site-lib');
|
|
|
|
class UserAdminController extends SiteController {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
async start ( ) {
|
|
const router = express.Router();
|
|
router.use(async (req, res, next) => {
|
|
res.locals.currentView = 'admin';
|
|
res.locals.adminView = 'user';
|
|
return next();
|
|
});
|
|
|
|
router.param('localUserId', this.populateLocalUserId.bind(this));
|
|
router.post('/local/:localUserId', this.postUpdateLocalUser.bind(this));
|
|
router.get('/local/:localUserId', this.getLocalUserView.bind(this));
|
|
|
|
router.get('/', this.getHomeView.bind(this));
|
|
|
|
return router;
|
|
}
|
|
|
|
async populateLocalUserId (req, res, next, localUserId) {
|
|
const { user: userService } = this.dtp.services;
|
|
try {
|
|
res.locals.userAccount = await userService.getLocalUserAccount(localUserId);
|
|
if (!res.locals.userAccount) {
|
|
throw new SiteError(404, 'User not found');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async postUpdateLocalUser (req, res, next) {
|
|
const {
|
|
logan: loganService,
|
|
user: userService,
|
|
} = this.dtp.services;
|
|
try {
|
|
this.log.debug('local user update', { action: req.body.action });
|
|
switch (req.body.action) {
|
|
case 'update':
|
|
await userService.updateLocalForAdmin(res.locals.userAccount, req.body);
|
|
loganService.sendRequestEvent(module.exports, req, {
|
|
level: 'info',
|
|
event: 'postUpdateLocalUser',
|
|
message: 'local user account updated',
|
|
data: {
|
|
userAccount: {
|
|
_id: res.locals.userAccount._id,
|
|
username: res.locals.userAccount.username,
|
|
},
|
|
},
|
|
});
|
|
break;
|
|
|
|
case 'ban':
|
|
await userService.ban(res.locals.userAccount);
|
|
loganService.sendRequestEvent(module.exports, req, {
|
|
level: 'info',
|
|
event: 'postUpdateLocalUser',
|
|
message: 'local user banned from the app',
|
|
data: {
|
|
userAccount: {
|
|
_id: res.locals.userAccount._id,
|
|
username: res.locals.userAccount.username,
|
|
},
|
|
},
|
|
});
|
|
break;
|
|
}
|
|
res.redirect('/admin/user');
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getLocalUserView (req, res, next) {
|
|
const { comment: commentService } = this.dtp.services;
|
|
try {
|
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
|
res.locals.recentComments = await commentService.getForAuthor(res.locals.userAccount, res.locals.pagination);
|
|
res.render('admin/user/form');
|
|
} catch (error) {
|
|
this.log.error('failed to produce user view', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getHomeView (req, res, next) {
|
|
const { user: userService } = this.dtp.services;
|
|
try {
|
|
res.locals.pagination = this.getPaginationParameters(req, 10);
|
|
res.locals.userAccounts = await userService.searchLocalUserAccounts(res.locals.pagination, req.query.u);
|
|
res.locals.totalUserCount = await userService.getTotalCount();
|
|
res.render('admin/user/index');
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
name: 'adminUser',
|
|
slug: 'admin-user',
|
|
className: 'UserAdminController',
|
|
create: async (dtp) => { return new UserAdminController(dtp); },
|
|
};
|