From bdb7e55b003d274423a7be4b926026277f5323d7 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 28 Jul 2020 15:03:18 +0200 Subject: [PATCH] Fix user approval backend --- src/helpers/BackendController.ts | 32 ++++++++++++++++++----------- views/backend/accounts_approval.njk | 4 ++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/helpers/BackendController.ts b/src/helpers/BackendController.ts index ba1c99d..3c55dd7 100644 --- a/src/helpers/BackendController.ts +++ b/src/helpers/BackendController.ts @@ -7,6 +7,7 @@ import {BadRequestError, NotFoundHttpError} from "../HttpError"; import Mail from "../Mail"; import {ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE} from "../Mails"; import UserEmail from "../auth/models/UserEmail"; +import UserApprovedComponent from "../auth/models/UserApprovedComponent"; export default class BackendController extends Controller { private static readonly menu: BackendMenuElement[] = []; @@ -55,22 +56,27 @@ export default class BackendController extends Controller { } protected async getAccountApproval(req: Request, res: Response): Promise { - 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', { - accounts: User.isApprovalMode() ? accounts : 0, + accounts: accounts, }); } protected async postApproveAccount(req: Request, res: Response): Promise { const {account, email} = await this.accountRequest(req); - account.approved = true; + account.as(UserApprovedComponent).approved = true; await account.save(); - await new Mail(ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE, { - approved: true, - link: config.get('base_url') + Controller.route('auth'), - }).send(email!.email!); + if (email) { + await new Mail(ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE, { + approved: true, + link: config.get('base_url') + Controller.route('auth'), + }).send(email.email!); + } req.flash('success', `Account successfully approved.`); res.redirectBack(Controller.route('accounts-approval')); @@ -81,9 +87,11 @@ export default class BackendController extends Controller { await account.delete(); - await new Mail(ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE, { - approved: false, - }).send(email!.email!); + if (email) { + await new Mail(ACCOUNT_REVIEW_NOTICE_MAIL_TEMPLATE, { + approved: false, + }).send(email.email!); + } req.flash('success', `Account successfully deleted.`); res.redirectBack(Controller.route('accounts-approval')); @@ -91,7 +99,7 @@ export default class BackendController extends Controller { protected async accountRequest(req: Request): Promise<{ account: User, - email: UserEmail, + email: UserEmail | null, }> { 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(); @@ -100,7 +108,7 @@ export default class BackendController extends Controller { return { account: account, - email: email!, + email: email, }; } } diff --git a/views/backend/accounts_approval.njk b/views/backend/accounts_approval.njk index 1f21e45..5ace855 100644 --- a/views/backend/accounts_approval.njk +++ b/views/backend/accounts_approval.njk @@ -21,7 +21,7 @@ {{ user.id }} {{ user.name }} - {{ user.mainEmail.getOrFail().email }} + {{ user.mainEmail.getOrFail().email | default('No email') }} {{ user.created_at.toISOString() }}
@@ -32,7 +32,7 @@
+ data-confirm="This will irrevocably delete the {{ user.mainEmail.getOrFail().email | default(user.name | default(user.id)) }} account."> {{ macros.csrf(getCSRFToken) }}