2021-03-29 16:59:17 +00:00
|
|
|
import { EventsApi, NotificationTransport, NotificationTransportModeEnum } from "authentik-api";
|
2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-03-29 16:59:17 +00:00
|
|
|
import { customElement, property } from "lit-element";
|
|
|
|
import { html, TemplateResult } from "lit-html";
|
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
|
|
|
import { ifDefined } from "lit-html/directives/if-defined";
|
|
|
|
import "../../elements/forms/HorizontalFormElement";
|
2021-04-03 22:36:53 +00:00
|
|
|
import { first } from "../../utils";
|
2021-05-11 09:55:25 +00:00
|
|
|
import { ModelForm } from "../../elements/forms/ModelForm";
|
2021-03-29 16:59:17 +00:00
|
|
|
|
|
|
|
@customElement("ak-event-transport-form")
|
2021-05-11 09:55:25 +00:00
|
|
|
export class TransportForm extends ModelForm<NotificationTransport, string> {
|
2021-03-29 16:59:17 +00:00
|
|
|
|
2021-05-11 09:55:25 +00:00
|
|
|
loadInstance(pk: string): Promise<NotificationTransport> {
|
2021-05-16 12:43:42 +00:00
|
|
|
return new EventsApi(DEFAULT_CONFIG).eventsTransportsRetrieve({
|
2021-05-11 09:55:25 +00:00
|
|
|
uuid: pk,
|
|
|
|
});
|
|
|
|
}
|
2021-03-29 16:59:17 +00:00
|
|
|
|
|
|
|
@property({type: Boolean})
|
|
|
|
showWebhook = false;
|
|
|
|
|
|
|
|
getSuccessMessage(): string {
|
2021-05-11 09:55:25 +00:00
|
|
|
if (this.instance) {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully updated transport.`;
|
2021-03-29 16:59:17 +00:00
|
|
|
} else {
|
2021-04-03 17:26:43 +00:00
|
|
|
return t`Successfully created transport.`;
|
2021-03-29 16:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send = (data: NotificationTransport): Promise<NotificationTransport> => {
|
2021-05-11 09:55:25 +00:00
|
|
|
if (this.instance) {
|
2021-03-29 16:59:17 +00:00
|
|
|
return new EventsApi(DEFAULT_CONFIG).eventsTransportsUpdate({
|
2021-05-11 09:55:25 +00:00
|
|
|
uuid: this.instance.pk || "",
|
2021-05-16 16:24:15 +00:00
|
|
|
notificationTransportRequest: data
|
2021-03-29 16:59:17 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new EventsApi(DEFAULT_CONFIG).eventsTransportsCreate({
|
2021-05-16 16:24:15 +00:00
|
|
|
notificationTransportRequest: data
|
2021-03-29 16:59:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderTransportModes(): TemplateResult {
|
|
|
|
return html`
|
2021-05-11 09:55:25 +00:00
|
|
|
<option value=${NotificationTransportModeEnum.Email} ?selected=${this.instance?.mode === NotificationTransportModeEnum.Email}>
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Email`}
|
2021-03-29 16:59:17 +00:00
|
|
|
</option>
|
2021-05-11 09:55:25 +00:00
|
|
|
<option value=${NotificationTransportModeEnum.Webhook} ?selected=${this.instance?.mode === NotificationTransportModeEnum.Webhook}>
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Webhook (generic)`}
|
2021-03-29 16:59:17 +00:00
|
|
|
</option>
|
2021-05-11 09:55:25 +00:00
|
|
|
<option value=${NotificationTransportModeEnum.WebhookSlack} ?selected=${this.instance?.mode === NotificationTransportModeEnum.WebhookSlack}>
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Webhook (Slack/Discord)`}
|
2021-03-29 16:59:17 +00:00
|
|
|
</option>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdated(): void {
|
2021-05-11 09:55:25 +00:00
|
|
|
if (this.instance) {
|
|
|
|
this.onModeChange(this.instance.mode);
|
2021-03-29 16:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onModeChange(mode: string): void {
|
|
|
|
if (mode === NotificationTransportModeEnum.Webhook || mode === NotificationTransportModeEnum.WebhookSlack) {
|
|
|
|
this.showWebhook = true;
|
|
|
|
} else {
|
|
|
|
this.showWebhook = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderForm(): TemplateResult {
|
|
|
|
return html`<form class="pf-c-form pf-m-horizontal">
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Name`}
|
2021-03-29 16:59:17 +00:00
|
|
|
?required=${true}
|
|
|
|
name="name">
|
2021-05-11 09:55:25 +00:00
|
|
|
<input type="text" value="${ifDefined(this.instance?.name)}" class="pf-c-form-control" required>
|
2021-03-29 16:59:17 +00:00
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Mode`}
|
2021-03-29 16:59:17 +00:00
|
|
|
?required=${true}
|
|
|
|
name="mode">
|
|
|
|
<select class="pf-c-form-control" @change=${(ev: Event) => {
|
|
|
|
const current = (ev.target as HTMLInputElement).value;
|
|
|
|
this.onModeChange(current);
|
|
|
|
}}>
|
|
|
|
${this.renderTransportModes()}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal
|
|
|
|
?hidden=${!this.showWebhook}
|
2021-04-03 17:26:43 +00:00
|
|
|
label=${t`Webhook URL`}
|
2021-03-29 16:59:17 +00:00
|
|
|
name="webhookUrl">
|
2021-05-11 09:55:25 +00:00
|
|
|
<input type="text" value="${ifDefined(this.instance?.webhookUrl)}" class="pf-c-form-control">
|
2021-03-29 16:59:17 +00:00
|
|
|
</ak-form-element-horizontal>
|
|
|
|
<ak-form-element-horizontal name="sendOnce">
|
|
|
|
<div class="pf-c-check">
|
2021-05-11 09:55:25 +00:00
|
|
|
<input type="checkbox" class="pf-c-check__input" ?checked=${first(this.instance?.sendOnce, false)}>
|
2021-03-29 16:59:17 +00:00
|
|
|
<label class="pf-c-check__label">
|
2021-04-03 17:26:43 +00:00
|
|
|
${t`Send once`}
|
2021-03-29 16:59:17 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
2021-04-03 17:26:43 +00:00
|
|
|
<p class="pf-c-form__helper-text">${t`Only send notification once, for example when sending a webhook into a chat channel.`}</p>
|
2021-03-29 16:59:17 +00:00
|
|
|
</ak-form-element-horizontal>
|
|
|
|
</form>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|