2020-07-20 17:32:32 +02:00
|
|
|
import config from "config";
|
|
|
|
import Controller from "../Controller";
|
|
|
|
import {REQUIRE_ADMIN_MIDDLEWARE, REQUIRE_AUTH_MIDDLEWARE} from "../auth/AuthComponent";
|
|
|
|
import User from "../auth/models/User";
|
|
|
|
import {Request, Response} from "express";
|
2020-07-24 13:00:20 +02:00
|
|
|
import {BadRequestError, NotFoundHttpError} from "../HttpError";
|
2020-07-20 17:32:32 +02:00
|
|
|
import Mail from "../Mail";
|
|
|
|
import {ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE} from "../Mails";
|
2020-07-24 13:00:20 +02:00
|
|
|
import UserEmail from "../auth/models/UserEmail";
|
2020-07-28 15:03:18 +02:00
|
|
|
import UserApprovedComponent from "../auth/models/UserApprovedComponent";
|
2020-07-20 17:32:32 +02:00
|
|
|
|
|
|
|
export default class BackendController extends Controller {
|
2020-07-28 10:04:15 +02:00
|
|
|
private static readonly menu: BackendMenuElement[] = [];
|
|
|
|
|
|
|
|
public static registerMenuElement(element: BackendMenuElement): void {
|
|
|
|
this.menu.push(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
super();
|
|
|
|
if (User.isApprovalMode()) {
|
|
|
|
BackendController.registerMenuElement({
|
|
|
|
getLink: async () => Controller.route('accounts-approval'),
|
|
|
|
getDisplayString: async () => {
|
|
|
|
const pendingUsersCount = (await User.select()
|
|
|
|
.where('approved', false)
|
|
|
|
.get()).length;
|
|
|
|
return `Accounts approval (${pendingUsersCount})`;
|
|
|
|
},
|
|
|
|
getDisplayIcon: async () => 'user-check',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getRoutesPrefix(): string {
|
2020-07-20 17:32:32 +02:00
|
|
|
return '/backend';
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:04:15 +02:00
|
|
|
public routes(): void {
|
2020-07-20 17:32:32 +02:00
|
|
|
this.get('/', this.getIndex, 'backend', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE);
|
|
|
|
if (User.isApprovalMode()) {
|
|
|
|
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE);
|
2020-07-24 13:00:20 +02:00
|
|
|
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE);
|
|
|
|
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE);
|
2020-07-20 17:32:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:04:15 +02:00
|
|
|
protected async getIndex(req: Request, res: Response): Promise<void> {
|
2020-07-20 17:32:32 +02:00
|
|
|
res.render('backend/index', {
|
2020-07-28 10:04:15 +02:00
|
|
|
menu: await Promise.all(BackendController.menu.map(async m => ({
|
|
|
|
link: await m.getLink(),
|
|
|
|
display_string: await m.getDisplayString(),
|
|
|
|
display_icon: await m.getDisplayIcon(),
|
|
|
|
}))),
|
2020-07-20 17:32:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:04:15 +02:00
|
|
|
protected async getAccountApproval(req: Request, res: Response): Promise<void> {
|
2020-07-28 15:03:18 +02:00
|
|
|
const accounts = await User.select()
|
|
|
|
.where('approved', 0)
|
|
|
|
.with('mainEmail')
|
|
|
|
.get();
|
2020-07-20 17:32:32 +02:00
|
|
|
res.render('backend/accounts_approval', {
|
2020-07-28 15:03:18 +02:00
|
|
|
accounts: accounts,
|
2020-07-20 17:32:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:04:15 +02:00
|
|
|
protected async postApproveAccount(req: Request, res: Response): Promise<void> {
|
2020-07-24 13:00:20 +02:00
|
|
|
const {account, email} = await this.accountRequest(req);
|
2020-07-20 17:32:32 +02:00
|
|
|
|
2020-07-28 15:03:18 +02:00
|
|
|
account.as(UserApprovedComponent).approved = true;
|
2020-07-20 17:32:32 +02:00
|
|
|
await account.save();
|
|
|
|
|
2020-07-28 15:03:18 +02:00
|
|
|
if (email) {
|
|
|
|
await new Mail(ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE, {
|
|
|
|
approved: true,
|
|
|
|
link: config.get<string>('base_url') + Controller.route('auth'),
|
|
|
|
}).send(email.email!);
|
|
|
|
}
|
2020-07-20 17:32:32 +02:00
|
|
|
|
|
|
|
req.flash('success', `Account successfully approved.`);
|
|
|
|
res.redirectBack(Controller.route('accounts-approval'));
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:04:15 +02:00
|
|
|
protected async postRejectAccount(req: Request, res: Response): Promise<void> {
|
2020-07-24 13:00:20 +02:00
|
|
|
const {account, email} = await this.accountRequest(req);
|
2020-07-20 17:32:32 +02:00
|
|
|
|
|
|
|
await account.delete();
|
|
|
|
|
2020-07-28 15:03:18 +02:00
|
|
|
if (email) {
|
|
|
|
await new Mail(ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE, {
|
|
|
|
approved: false,
|
|
|
|
}).send(email.email!);
|
|
|
|
}
|
2020-07-20 17:32:32 +02:00
|
|
|
|
|
|
|
req.flash('success', `Account successfully deleted.`);
|
|
|
|
res.redirectBack(Controller.route('accounts-approval'));
|
|
|
|
}
|
2020-07-24 13:00:20 +02:00
|
|
|
|
2020-07-28 10:04:15 +02:00
|
|
|
protected async accountRequest(req: Request): Promise<{
|
2020-07-24 13:00:20 +02:00
|
|
|
account: User,
|
2020-07-28 15:03:18 +02:00
|
|
|
email: UserEmail | null,
|
2020-07-24 13:00:20 +02:00
|
|
|
}> {
|
|
|
|
if (!req.body.user_id) throw new BadRequestError('Missing user_id field', 'Check your form', req.url);
|
|
|
|
const account = await User.select().where('id', req.body.user_id).with('mainEmail').first();
|
|
|
|
if (!account) throw new NotFoundHttpError('User', req.url);
|
|
|
|
const email = await account.mainEmail.get();
|
|
|
|
|
|
|
|
return {
|
|
|
|
account: account,
|
2020-07-28 15:03:18 +02:00
|
|
|
email: email,
|
2020-07-24 13:00:20 +02:00
|
|
|
};
|
|
|
|
}
|
2020-07-28 10:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface BackendMenuElement {
|
|
|
|
/**
|
|
|
|
* Returns the link of this menu element (usually using {@code Controller.route})
|
|
|
|
*/
|
|
|
|
getLink(): Promise<string>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The string part of the link display
|
|
|
|
*/
|
|
|
|
getDisplayString(): Promise<string>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An optional feather icon name
|
|
|
|
*/
|
|
|
|
getDisplayIcon(): Promise<string | null>;
|
|
|
|
}
|