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"; import "@goauthentik/elements/forms/Radio"; import "@goauthentik/elements/forms/SearchSelect"; import { msg } from "@lit/localize"; import { TemplateResult, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { EventsApi, NotificationTransport, NotificationTransportModeEnum, NotificationWebhookMapping, PropertymappingsApi, PropertymappingsNotificationListRequest, } from "@goauthentik/api"; @customElement("ak-event-transport-form") export class TransportForm extends ModelForm { loadInstance(pk: string): Promise { return new EventsApi(DEFAULT_CONFIG) .eventsTransportsRetrieve({ uuid: pk, }) .then((transport) => { this.onModeChange(transport.mode); return transport; }); } @property({ type: Boolean }) showWebhook = false; getSuccessMessage(): string { return this.instance ? msg("Successfully updated transport.") : msg("Successfully created transport."); } async send(data: NotificationTransport): Promise { if (this.instance) { return new EventsApi(DEFAULT_CONFIG).eventsTransportsUpdate({ uuid: this.instance.pk || "", notificationTransportRequest: data, }); } return new EventsApi(DEFAULT_CONFIG).eventsTransportsCreate({ notificationTransportRequest: data, }); } onModeChange(mode: string | undefined): void { if ( mode === NotificationTransportModeEnum.Webhook || mode === NotificationTransportModeEnum.WebhookSlack ) { this.showWebhook = true; } else { this.showWebhook = false; } } renderForm(): TemplateResult { return html` ) => { this.onModeChange(ev.detail.value); }} .options=${[ { label: msg("Local (notifications will be created within authentik)"), value: NotificationTransportModeEnum.Local, default: true, }, { label: msg("Email"), value: NotificationTransportModeEnum.Email, }, { label: msg("Webhook (generic)"), value: NotificationTransportModeEnum.Webhook, }, { label: msg("Webhook (Slack/Discord)"), value: NotificationTransportModeEnum.WebhookSlack, }, ]} .value=${this.instance?.mode} > => { const args: PropertymappingsNotificationListRequest = { ordering: "name", }; if (query !== undefined) { args.search = query; } const items = await new PropertymappingsApi( DEFAULT_CONFIG, ).propertymappingsNotificationList(args); return items.results; }} .renderElement=${(item: NotificationWebhookMapping): string => { return item.name; }} .value=${(item: NotificationWebhookMapping | undefined): string | undefined => { return item?.pk; }} .selected=${(item: NotificationWebhookMapping): boolean => { return this.instance?.webhookMappingBody === item.pk; }} blankable > => { const args: PropertymappingsNotificationListRequest = { ordering: "name", }; if (query !== undefined) { args.search = query; } const items = await new PropertymappingsApi( DEFAULT_CONFIG, ).propertymappingsNotificationList(args); return items.results; }} .renderElement=${(item: NotificationWebhookMapping): string => { return item.name; }} .value=${(item: NotificationWebhookMapping | undefined): string | undefined => { return item?.pk; }} .selected=${(item: NotificationWebhookMapping): boolean => { return this.instance?.webhookMappingHeaders === item.pk; }} blankable >

${msg( "Only send notification once, for example when sending a webhook into a chat channel.", )}

`; } } declare global { interface HTMLElementTagNameMap { "ak-event-transport-form": TransportForm; } }