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 { 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"; import { ModelForm } from "../../elements/forms/ModelForm"; enum target { policy, group, user } @customElement("ak-policy-binding-form") export class PolicyBindingForm extends ModelForm { loadInstance(pk: string): Promise { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsRetrieve({ policyBindingUuid: pk }).then(binding => { if (binding?.policyObj) { this.policyGroupUser = target.policy; } if (binding?.groupObj) { this.policyGroupUser = target.group; } if (binding?.userObj) { this.policyGroupUser = target.user; } return binding; }); } @property() targetPk?: string; @property({type: Number}) policyGroupUser: target = target.policy; @property({type: Boolean}) policyOnly = false; getSuccessMessage(): string { if (this.instance) { 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; } `); } send = (data: PolicyBinding): Promise => { if (this.instance) { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsUpdate({ policyBindingUuid: this.instance.pk || "", policyBindingRequest: data }); } else { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsCreate({ policyBindingRequest: data }); } }; groupPolicies(policies: Policy[]): TemplateResult { return html` ${groupBy(policies, (p => p.verboseName || "")).map(([group, policies]) => { return html` ${policies.map(p => { const selected = (this.instance?.policy === p.pk); return html``; })} `; })} `; } getOrder(): Promise { if (this.instance) { return Promise.resolve(this.instance.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; }); } renderModeSelector(): TemplateResult { if (this.policyOnly) { this.policyGroupUser = target.policy; return html`
`; } return html`
`; } renderForm(): TemplateResult { return html`
${this.renderModeSelector()}

${t`Negates the outcome of the binding. Messages are unaffected.`}

`; } }