import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { first, groupBy } from "@goauthentik/common/utils"; import "@goauthentik/elements/forms/HorizontalFormElement"; import { ModelForm } from "@goauthentik/elements/forms/ModelForm"; import "@goauthentik/elements/forms/SearchSelect"; import { t } from "@lingui/macro"; import { CSSResult, css } from "lit"; import { TemplateResult, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import PFContent from "@patternfly/patternfly/components/Content/content.css"; import PFToggleGroup from "@patternfly/patternfly/components/ToggleGroup/toggle-group.css"; import { CoreApi, CoreGroupsListRequest, CoreUsersListRequest, Group, PoliciesAllListRequest, PoliciesApi, Policy, PolicyBinding, User, } from "@goauthentik/api"; enum target { policy, group, user, } @customElement("ak-policy-binding-form") export class PolicyBindingForm extends ModelForm { async loadInstance(pk: string): Promise { const binding = await new PoliciesApi(DEFAULT_CONFIG).policiesBindingsRetrieve({ policyBindingUuid: pk, }); if (binding?.policyObj) { this.policyGroupUser = target.policy; } if (binding?.groupObj) { this.policyGroupUser = target.group; } if (binding?.userObj) { this.policyGroupUser = target.user; } this.defaultOrder = await this.getOrder(); return binding; } @property() targetPk?: string; @property({ type: Number }) policyGroupUser: target = target.policy; @property({ type: Boolean }) policyOnly = false; @state() defaultOrder = 0; getSuccessMessage(): string { if (this.instance?.pk) { 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?.pk) { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsUpdate({ policyBindingUuid: this.instance.pk, policyBindingRequest: data, }); } else { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsCreate({ policyBindingRequest: data, }); } }; async getOrder(): Promise { if (this.instance?.pk) { return this.instance.order; } const bindings = await new PoliciesApi(DEFAULT_CONFIG).policiesBindingsList({ target: this.targetPk || "", }); 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.`}

`; } }