import { CoreApi, PoliciesApi, Policy, PolicyBinding } from "authentik-api"; import { t } from "@lingui/macro"; import { css, CSSResult, 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"; import PFToggleGroup from "@patternfly/patternfly/components/ToggleGroup/toggle-group.css"; import PFContent from "@patternfly/patternfly/components/Content/content.css"; enum target { policy, group, user } @customElement("ak-policy-binding-form") export class PolicyBindingForm extends Form { @property({attribute: false}) set binding(value: PolicyBinding | undefined) { this._binding = value; if (value?.policyObj) { this.policyGroupUser = target.policy; } if (value?.groupObj) { this.policyGroupUser = target.group; } if (value?.userObj) { this.policyGroupUser = target.user; } } get binding(): PolicyBinding | undefined { return this._binding; } _binding?: PolicyBinding; @property() targetPk?: string; @property({type: Number}) policyGroupUser: target = target.policy; getSuccessMessage(): string { if (this.binding) { return t`Successfully updated binding.`; } else { return t`Successfully created binding.`; } } static get styles(): CSSResult[] { return super.styles.concat(PFToggleGroup, PFContent, css` .pf-c-toggle-group { justify-content: center; } `); } 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); if (orders.length < 1) { return 0; } return Math.max(...orders) + 1; }); } renderForm(): TemplateResult { return html`
`; } }