2022-09-14 22:05:21 +00:00
|
|
|
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
|
|
|
|
import { first } from "@goauthentik/common/utils";
|
|
|
|
import "@goauthentik/elements/forms/HorizontalFormElement";
|
|
|
|
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";
|
2022-06-25 15:44:17 +00:00
|
|
|
|
2021-09-21 09:31:37 +00:00
|
|
|
import { t } from "@lingui/macro";
|
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { TemplateResult, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
|
|
|
import { ifDefined } from "lit/directives/if-defined.js";
|
|
|
|
import { until } from "lit/directives/until.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-09-11 23:02:51 +00:00
|
|
|
import {
|
|
|
|
EventsApi,
|
|
|
|
NotificationTransport,
|
|
|
|
NotificationTransportModeEnum,
|
|
|
|
PropertymappingsApi,
|
|
|
|
} from "@goauthentik/api";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
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> {
|
|
|
|
loadInstance(pk: string): Promise<NotificationTransport> {
|
2021-09-23 14:16:38 +00:00
|
|
|
return new EventsApi(DEFAULT_CONFIG)
|
|
|
|
.eventsTransportsRetrieve({
|
|
|
|
uuid: pk,
|
|
|
|
})
|
|
|
|
.then((transport) => {
|
|
|
|
this.onModeChange(transport.mode);
|
|
|
|
return transport;
|
|
|
|
});
|
2021-05-11 09:55:25 +00:00
|
|
|
}
|
2021-03-29 16:59:17 +00:00
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ type: Boolean })
|
2021-03-29 16:59:17 +00:00
|
|
|
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-08-03 15:52:21 +00:00
|
|
|
notificationTransportRequest: data,
|
2021-03-29 16:59:17 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new EventsApi(DEFAULT_CONFIG).eventsTransportsCreate({
|
2021-08-03 15:52:21 +00:00
|
|
|
notificationTransportRequest: data,
|
2021-03-29 16:59:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderTransportModes(): TemplateResult {
|
|
|
|
return html`
|
2022-05-30 18:55:05 +00:00
|
|
|
<option
|
|
|
|
value=${NotificationTransportModeEnum.Local}
|
|
|
|
?selected=${this.instance?.mode === NotificationTransportModeEnum.Local}
|
|
|
|
>
|
|
|
|
${t`Local (notifications will be created within authentik)`}
|
|
|
|
</option>
|
2021-08-03 15:52:21 +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-08-03 15:52:21 +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-08-03 15:52:21 +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>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-06-25 15:44:17 +00:00
|
|
|
onModeChange(mode: string | undefined): void {
|
2021-08-03 15:52:21 +00:00
|
|
|
if (
|
|
|
|
mode === NotificationTransportModeEnum.Webhook ||
|
|
|
|
mode === NotificationTransportModeEnum.WebhookSlack
|
|
|
|
) {
|
2021-03-29 16:59:17 +00:00
|
|
|
this.showWebhook = true;
|
|
|
|
} else {
|
|
|
|
this.showWebhook = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderForm(): TemplateResult {
|
|
|
|
return html`<form class="pf-c-form pf-m-horizontal">
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Name`} ?required=${true} name="name">
|
|
|
|
<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>
|
2021-08-03 15:52:21 +00:00
|
|
|
<ak-form-element-horizontal label=${t`Mode`} ?required=${true} name="mode">
|
|
|
|
<select
|
|
|
|
class="pf-c-form-control"
|
|
|
|
@change=${(ev: Event) => {
|
|
|
|
const current = (ev.target as HTMLInputElement).value;
|
|
|
|
this.onModeChange(current);
|
|
|
|
}}
|
|
|
|
>
|
2021-03-29 16:59:17 +00:00
|
|
|
${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-08-03 15:52:21 +00:00
|
|
|
name="webhookUrl"
|
2021-09-23 14:16:38 +00:00
|
|
|
?required=${true}
|
2021-08-03 15:52:21 +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>
|
2021-09-11 23:02:51 +00:00
|
|
|
<ak-form-element-horizontal
|
|
|
|
?hidden=${!this.showWebhook}
|
|
|
|
label=${t`Webhook Mapping`}
|
|
|
|
name="webhookMapping"
|
|
|
|
>
|
|
|
|
<select class="pf-c-form-control">
|
|
|
|
<option value="" ?selected=${this.instance?.webhookMapping === undefined}>
|
|
|
|
---------
|
|
|
|
</option>
|
|
|
|
${until(
|
|
|
|
new PropertymappingsApi(DEFAULT_CONFIG)
|
|
|
|
.propertymappingsNotificationList({})
|
|
|
|
.then((mappings) => {
|
|
|
|
return mappings.results.map((mapping) => {
|
|
|
|
return html`<option
|
|
|
|
value=${ifDefined(mapping.pk)}
|
|
|
|
?selected=${this.instance?.webhookMapping === mapping.pk}
|
|
|
|
>
|
|
|
|
${mapping.name}
|
|
|
|
</option>`;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
html`<option>${t`Loading...`}</option>`,
|
|
|
|
)}
|
|
|
|
</select>
|
|
|
|
</ak-form-element-horizontal>
|
2021-03-29 16:59:17 +00:00
|
|
|
<ak-form-element-horizontal name="sendOnce">
|
|
|
|
<div class="pf-c-check">
|
2021-08-03 15:52:21 +00:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
class="pf-c-check__input"
|
|
|
|
?checked=${first(this.instance?.sendOnce, false)}
|
|
|
|
/>
|
|
|
|
<label class="pf-c-check__label"> ${t`Send once`} </label>
|
2021-03-29 16:59:17 +00:00
|
|
|
</div>
|
2021-08-03 15:52:21 +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>`;
|
|
|
|
}
|
|
|
|
}
|