import { t } from "@lingui/macro"; import { TemplateResult, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { PoliciesApi, PolicyBinding } from "@goauthentik/api"; import { AKResponse } from "../../api/Client"; import { DEFAULT_CONFIG } from "../../api/Config"; import { uiConfig } from "../../common/config"; import { PFColor } from "../../elements/Label"; import { PFSize } from "../../elements/Spinner"; import "../../elements/Tabs"; import "../../elements/forms/DeleteBulkForm"; import "../../elements/forms/ModalForm"; import "../../elements/forms/ProxyForm"; import { Table, TableColumn } from "../../elements/table/Table"; import "../groups/GroupForm"; import "../policies/PolicyWizard"; import "../users/UserForm"; import "./PolicyBindingForm"; @customElement("ak-bound-policies-list") export class BoundPoliciesList extends Table { @property() target?: string; @property({ type: Boolean }) policyOnly = false; checkbox = true; async apiEndpoint(page: number): Promise> { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsList({ target: this.target || "", ordering: "order", page: page, pageSize: (await uiConfig()).pagination.perPage, }); } columns(): TableColumn[] { return [ new TableColumn(t`Policy / User / Group`), new TableColumn(t`Enabled`, "enabled"), new TableColumn(t`Order`, "order"), new TableColumn(t`Timeout`, "timeout"), new TableColumn(t`Actions`), ]; } getPolicyUserGroupRowLabel(item: PolicyBinding): string { if (item.policy) { return t`Policy ${item.policyObj?.name}`; } else if (item.group) { return t`Group ${item.groupObj?.name}`; } else if (item.user) { return t`User ${item.userObj?.name}`; } else { return t`-`; } } getPolicyUserGroupRow(item: PolicyBinding): TemplateResult { const label = this.getPolicyUserGroupRowLabel(item); if (item.user) { return html` ${label} `; } return html`${label}`; } getObjectEditButton(item: PolicyBinding): TemplateResult { if (item.policy) { return html` ${t`Update`} ${t`Update ${item.policyObj?.name}`} `; } else if (item.group) { return html` ${t`Update`} ${t`Update Group`} `; } else if (item.user) { return html` ${t`Update`} ${t`Update User`} `; } else { return html``; } } renderToolbarSelected(): TemplateResult { const disabled = this.selectedElements.length < 1; return html` { return [ { key: t`Order`, value: item.order.toString() }, { key: t`Policy / User / Group`, value: this.getPolicyUserGroupRowLabel(item) }, ]; }} .usedBy=${(item: PolicyBinding) => { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsUsedByList({ policyBindingUuid: item.pk, }); }} .delete=${(item: PolicyBinding) => { return new PoliciesApi(DEFAULT_CONFIG).policiesBindingsDestroy({ policyBindingUuid: item.pk, }); }} > `; } row(item: PolicyBinding): TemplateResult[] { return [ html`${this.getPolicyUserGroupRow(item)}`, html` ${item.enabled ? t`Yes` : t`No`} `, html`${item.order}`, html`${item.timeout}`, html` ${this.getObjectEditButton(item)} ${t`Update`} ${t`Update Binding`} `, ]; } renderEmpty(): TemplateResult { return super.renderEmpty(html`
${t`No policies are currently bound to this object.`}
${t`Create`} ${t`Create Binding`}
`); } renderToolbar(): TemplateResult { return html` ${t`Create`} ${t`Create Binding`} `; } }