MailboxBackend: add set main identity action

This commit is contained in:
Alice Gaudon 2021-03-01 13:43:29 +01:00
parent 6478d77733
commit 272e506281
2 changed files with 35 additions and 1 deletions

View File

@ -3,7 +3,7 @@ import {Request, Response} from "express";
import User from "swaf/auth/models/User";
import {WhereTest} from "swaf/db/ModelQuery";
import UserMailIdentityComponent from "../../models/UserMailIdentityComponent";
import {NotFoundHttpError} from "swaf/HttpError";
import {NotFoundHttpError, ServerError} from "swaf/HttpError";
import MailDomain from "../../models/MailDomain";
import BackendController from "swaf/helpers/BackendController";
import MailIdentity from "../../models/MailIdentity";
@ -34,6 +34,7 @@ export default class MailboxBackendController extends Controller {
this.post('/remove-domain', this.postRemoveDomain, 'backend-remove-domain', RequireAuthMiddleware, RequireAdminMiddleware);
this.post('/:id/create-mail-identity', this.postCreateMailIdentity, 'backend-create-mail-identity', RequireAuthMiddleware, RequireAdminMiddleware);
this.post('/set-main-mail-identity', this.postSetMainMailIdentity, 'backend-set-main-mail-identity', RequireAuthMiddleware, RequireAdminMiddleware);
this.post('/delete-mail-identity', this.postDeleteMailIdentity, 'backend-delete-mail-identity', RequireAuthMiddleware, RequireAdminMiddleware);
}
@ -207,4 +208,26 @@ export default class MailboxBackendController extends Controller {
req.flash('success', 'Identity ' + await identity.toEmail() + ' successfully deleted.');
res.redirect(Controller.route('backend-mailbox', user.id));
}
protected async postSetMainMailIdentity(req: Request, res: Response): Promise<void> {
const identity = await MailIdentity.select()
.where('id', req.body.id)
.with('user.mainMailIdentity')
.first();
if (!identity) throw new NotFoundHttpError('Mail identity', req.url);
const user = await identity.user.getOrFail();
if (!user) throw new NotFoundHttpError('Mail identity owner', req.url);
const mailIdentityComponent = user.as(UserMailIdentityComponent);
const mainMailIdentity = mailIdentityComponent.mainMailIdentity.getOrFail();
if (!mainMailIdentity) throw new ServerError('Could not find this users main mail identity.');
mailIdentityComponent.main_mail_identity_id = identity.id;
await user.save();
req.flash('success', 'User ' + user.id + ' main mail identity set to ' + await identity.toEmail());
req.flash('warning', 'Please rename user\'s mailbox folder to correspond to changing from ' + await mainMailIdentity.toEmail() + ' to ' + await identity.toEmail());
res.redirect(Controller.route('backend-mailbox', user.id));
}
}

View File

@ -29,6 +29,17 @@
<td>{{ identity.email }}</td>
<td class="actions">
{% if mailbox.name != identity.email %}
<form action="{{ route('backend-set-main-mail-identity') }}" method="POST">
<input type="hidden" name="id" value="{{ identity.id }}">
<button class=""
onclick="return confirm('Are you sure you want to set {{ identity.email }} as this mailbox\'s identity? This requires moving existing emails on the mail server afterwards.')">
<i data-feather="tag"></i> <span class="tip">Set as main identity</span>
</button>
{{ macros.csrf(getCsrfToken) }}
</form>
<form action="{{ route('backend-delete-mail-identity') }}" method="POST">
<input type="hidden" name="id" value="{{ identity.id }}">