import { CoreApi, PoliciesApi, Policy, PolicyBinding } from "authentik-api"; import { t } from "@lingui/macro"; import { customElement, property } from "lit-element"; import { html, TemplateResult } from "lit-html"; import { DEFAULT_CONFIG } from "../../api/Config"; import { Form } from "../../elements/forms/Form"; import { until } from "lit-html/directives/until"; import { ifDefined } from "lit-html/directives/if-defined"; import { first, groupBy } from "../../utils"; import "../../elements/forms/HorizontalFormElement"; @customElement("ak-policy-binding-form") export class PolicyBindingForm extends Form { @property({attribute: false}) binding?: PolicyBinding; @property() targetPk?: string; getSuccessMessage(): string { if (this.binding) { return t`Successfully updated binding.`; } else { return t`Successfully created binding.`; } } async customValidate(form: PolicyBinding): Promise { return form; } send = (data: PolicyBinding): Promise => { if (this.binding) { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsUpdate({ policyBindingUuid: this.binding.pk || "", data: data }); } else { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsCreate({ data: data }); } }; groupPolicies(policies: Policy[]): TemplateResult { return html` ${groupBy(policies, (p => p.verboseName || "")).map(([group, policies]) => { return html` ${policies.map(p => { const selected = (this.binding?.policy === p.pk); return html``; })} `; })} `; } getOrder(): Promise { if (this.binding) { return Promise.resolve(this.binding.order); } return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsList({ target: this.targetPk || "", }).then(bindings => { const orders = bindings.results.map(binding => binding.order); return Math.max(...orders) + 1; }); } renderForm(): TemplateResult { return html`
`; } }