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"; import UserEmail from "../auth/models/UserEmail";
export default class BackendController extends Controller { 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'; return '/backend';
} }
routes(): void { public routes(): void {
this.get('/', this.getIndex, 'backend', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE); this.get('/', this.getIndex, 'backend', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE);
if (User.isApprovalMode()) { if (User.isApprovalMode()) {
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval', REQUIRE_AUTH_MIDDLEWARE, REQUIRE_ADMIN_MIDDLEWARE); 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', { res.render('backend/index', {
approval_mode: User.isApprovalMode(), menu: await Promise.all(BackendController.menu.map(async m => ({
accounts_to_approve: User.isApprovalMode() ? await User.select().count() : 0, 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(); const accounts = await User.select().where('approved', 0).with('mainEmail').get();
res.render('backend/accounts_approval', { res.render('backend/accounts_approval', {
accounts: User.isApprovalMode() ? accounts : 0, 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); const {account, email} = await this.accountRequest(req);
account.approved = true; account.approved = true;
@ -51,7 +76,7 @@ export default class BackendController extends Controller {
res.redirectBack(Controller.route('accounts-approval')); 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); const {account, email} = await this.accountRequest(req);
await account.delete(); await account.delete();
@ -64,7 +89,7 @@ export default class BackendController extends Controller {
res.redirectBack(Controller.route('accounts-approval')); res.redirectBack(Controller.route('accounts-approval'));
} }
private async accountRequest(req: Request): Promise<{ protected async accountRequest(req: Request): Promise<{
account: User, account: User,
email: UserEmail, email: UserEmail,
}> { }> {
@ -78,4 +103,21 @@ export default class BackendController extends Controller {
email: email!, email: email!,
}; };
} }
} }
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"> <div class="panel">
<nav> <nav>
<ul> <ul>
{% if approval_mode %} {% for element in menu %}
<li> <li>
<a href="{{ route('accounts-approval') }}">Accounts approval ({{ accounts_to_approve }})</a> <a href="{{ element.link }}">
</li> {% if element.display_icon !== null %}
{% endif %} <i data-feather="{{ element.display_icon }}"></i>
{% endif %}
{{ element.display_string }}
</a>
</li>
{% endfor %}
</ul> </ul>
</nav> </nav>
</div> </div>