Make BackendController helper accept external backend links for its view

This commit is contained in:
Alice Gaudon 2020-07-28 10:04:15 +02:00
parent 3a4755ce98
commit df651f2661
2 changed files with 62 additions and 15 deletions

View File

@ -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<void> {
protected async getIndex(req: Request, res: Response): Promise<void> {
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<void> {
protected async getAccountApproval(req: Request, res: Response): Promise<void> {
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<void> {
protected async postApproveAccount(req: Request, res: Response): Promise<void> {
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<void> {
protected async postRejectAccount(req: Request, res: Response): Promise<void> {
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,
}> {
@ -79,3 +104,20 @@ export default class BackendController extends Controller {
};
}
}
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>;
}

View File

@ -9,11 +9,16 @@
<div class="panel">
<nav>
<ul>
{% if approval_mode %}
<li>
<a href="{{ route('accounts-approval') }}">Accounts approval ({{ accounts_to_approve }})</a>
</li>
{% endif %}
{% for element in menu %}
<li>
<a href="{{ element.link }}">
{% if element.display_icon !== null %}
<i data-feather="{{ element.display_icon }}"></i>
{% endif %}
{{ element.display_string }}
</a>
</li>
{% endfor %}
</ul>
</nav>
</div>