From df651f2661d8a02d722b4c99fce3a6e5b38feb8f Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 28 Jul 2020 10:04:15 +0200 Subject: [PATCH] Make BackendController helper accept external backend links for its view --- src/helpers/BackendController.ts | 62 ++++++++++++++++++++++++++------ views/backend/index.njk | 15 +++++--- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/helpers/BackendController.ts b/src/helpers/BackendController.ts index 775e364..ba1c99d 100644 --- a/src/helpers/BackendController.ts +++ b/src/helpers/BackendController.ts @@ -9,11 +9,33 @@ import {ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE} from "../Mails"; import UserEmail from "../auth/models/UserEmail"; export default class BackendController extends Controller { - getRoutesPrefix(): string { + 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 { return '/backend'; } - routes(): void { + public routes(): void { 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); @@ -22,21 +44,24 @@ export default class BackendController extends Controller { } } - public async getIndex(req: Request, res: Response): Promise { + protected async getIndex(req: Request, res: Response): Promise { res.render('backend/index', { - approval_mode: User.isApprovalMode(), - accounts_to_approve: User.isApprovalMode() ? await User.select().count() : 0, + menu: await Promise.all(BackendController.menu.map(async m => ({ + link: await m.getLink(), + display_string: await m.getDisplayString(), + display_icon: await m.getDisplayIcon(), + }))), }); } - public async getAccountApproval(req: Request, res: Response): Promise { + protected async getAccountApproval(req: Request, res: Response): Promise { const accounts = await User.select().where('approved', 0).with('mainEmail').get(); res.render('backend/accounts_approval', { accounts: User.isApprovalMode() ? accounts : 0, }); } - public async postApproveAccount(req: Request, res: Response): Promise { + protected async postApproveAccount(req: Request, res: Response): Promise { const {account, email} = await this.accountRequest(req); account.approved = true; @@ -51,7 +76,7 @@ export default class BackendController extends Controller { res.redirectBack(Controller.route('accounts-approval')); } - public async postRejectAccount(req: Request, res: Response): Promise { + protected async postRejectAccount(req: Request, res: Response): Promise { const {account, email} = await this.accountRequest(req); await account.delete(); @@ -64,7 +89,7 @@ export default class BackendController extends Controller { res.redirectBack(Controller.route('accounts-approval')); } - private async accountRequest(req: Request): Promise<{ + protected async accountRequest(req: Request): Promise<{ account: User, email: UserEmail, }> { @@ -78,4 +103,21 @@ export default class BackendController extends Controller { email: email!, }; } -} \ No newline at end of file +} + +export interface BackendMenuElement { + /** + * Returns the link of this menu element (usually using {@code Controller.route}) + */ + getLink(): Promise; + + /** + * The string part of the link display + */ + getDisplayString(): Promise; + + /** + * An optional feather icon name + */ + getDisplayIcon(): Promise; +} diff --git a/views/backend/index.njk b/views/backend/index.njk index 14299da..1455ab9 100644 --- a/views/backend/index.njk +++ b/views/backend/index.njk @@ -9,11 +9,16 @@