import { t } from "@lingui/macro"; import { CSSResult, css } 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 PFContent from "@patternfly/patternfly/components/Content/content.css"; import PFToggleGroup from "@patternfly/patternfly/components/ToggleGroup/toggle-group.css"; import { CoreApi, PoliciesApi, Policy, PolicyBinding } from "@goauthentik/api"; import { DEFAULT_CONFIG } from "../../api/Config"; import "../../elements/forms/HorizontalFormElement"; import { ModelForm } from "../../elements/forms/ModelForm"; import { UserOption } from "../../elements/user/utils"; import { first, groupBy } from "../../utils"; 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 { return html`
`; } renderForm(): TemplateResult { return html`
${this.renderModeSelector()}

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

`; } }