import { t } from "@lingui/macro"; import { CSSResult } from "lit"; import { TemplateResult, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { until } from "lit/directives/until.js"; import PFDropdown from "@patternfly/patternfly/components/Dropdown/dropdown.css"; import { Application, CapabilitiesEnum, CoreApi, PolicyEngineMode, Provider, ProvidersApi, } from "@goauthentik/api"; import { DEFAULT_CONFIG, config } from "../../api/Config"; import "../../elements/Spinner"; import "../../elements/buttons/Dropdown"; import "../../elements/forms/FormGroup"; import "../../elements/forms/HorizontalFormElement"; import "../../elements/forms/ModalForm"; import { ModelForm } from "../../elements/forms/ModelForm"; import "../../elements/forms/ProxyForm"; import { first } from "../../utils"; @customElement("ak-application-form") export class ApplicationForm extends ModelForm { loadInstance(pk: string): Promise { return new CoreApi(DEFAULT_CONFIG).coreApplicationsRetrieve({ slug: pk, }); } @property({ attribute: false }) provider?: number; @property({ type: Boolean }) clearIcon = false; getSuccessMessage(): string { if (this.instance) { return t`Successfully updated application.`; } else { return t`Successfully created application.`; } } static get styles(): CSSResult[] { return super.styles.concat(PFDropdown); } send = async (data: Application): Promise => { let app: Application; if (this.instance) { app = await new CoreApi(DEFAULT_CONFIG).coreApplicationsUpdate({ slug: this.instance.slug, applicationRequest: data, }); } else { app = await new CoreApi(DEFAULT_CONFIG).coreApplicationsCreate({ applicationRequest: data, }); } const c = await config(); if (c.capabilities.includes(CapabilitiesEnum.SaveMedia)) { const icon = this.getFormFile(); if (icon || this.clearIcon) { await new CoreApi(DEFAULT_CONFIG).coreApplicationsSetIconCreate({ slug: app.slug, file: icon, clear: this.clearIcon, }); } } else { await new CoreApi(DEFAULT_CONFIG).coreApplicationsSetIconUrlCreate({ slug: app.slug, filePathRequest: { url: data.metaIcon || "", }, }); } return app; }; groupProviders(providers: Provider[]): TemplateResult { const m = new Map(); providers.forEach((p) => { if (!m.has(p.verboseName || "")) { m.set(p.verboseName || "", []); } const tProviders = m.get(p.verboseName || "") || []; tProviders.push(p); }); return html` ${Array.from(m).map(([group, providers]) => { return html` ${providers.map((p) => { const selected = this.instance?.provider === p.pk || this.provider === p.pk; return html``; })} `; })} `; } renderForm(): TemplateResult { return html`

${t`Application's display Name.`}

${t`Internal application name, used in URLs.`}

${t`Select a provider that this application should use. Alternatively, create a new provider.`}

${t`UI settings`}

${t`If left empty, authentik will try to extract the launch URL based on the selected provider.`}

${until( config().then((c) => { if (c.capabilities.includes(CapabilitiesEnum.SaveMedia)) { return html` ${this.instance?.metaIcon ? html`

${t`Currently set to:`} ${this.instance?.metaIcon}

` : html``}
${this.instance?.metaIcon ? html`
{ const target = ev.target as HTMLInputElement; this.clearIcon = target.checked; }} />

${t`Delete currently set icon.`}

` : html``}`; } return html` `; }), )}
`; } }